You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Jan Buschtoens <jb...@abm.de> on 2019/06/26 07:29:33 UTC

Drawing only a portion of a PDF (Graphics.clip is ignored/overwritten)

Hi.


I want to use PDFBox to draw PDFs in a swing application.


I already created a  new issue for a problem I had on Linux (PDFBOX-4581). But unrelated to that I had another problem (on windows and  linux), and I wanted to ask first if I'm doing something wrong before I create a new issue.


I want to draw only a portion of the PDF. As far as I know, the way to do this in swing is with the "clip"-method on the Graphics object. However, no matter what clip I set, the wohle PDF is drawn. I believe that PDFRenderer calls "setClip", when it probably should use "clip". The latter sets the current clip to the intersection of the old current clip and the provided shape, wheras the former resets the current clip.



My code is essentially the following:


        @Override
        public void paint(final Graphics g) {

            final Graphics2D g2 = (Graphics2D) g;
            try {
                PDDocument pdDocument = PDDocument.load(App.class.getResourceAsStream("somepdf.pdf"));
                g2.clip(new Rectangle(20, 20));
                final PDFRenderer pdfRenderer = new org.apache.pdfbox.rendering.PDFRenderer(pdDocument);
                pdfRenderer.renderPageToGraphics(0, g2);
            } catch (final IOException e) {
                throw new RuntimeException(e);
            }
        }


--

{ Geschaeftsfuehrer: Dipl.-Inform. Christopher Asp,  Amtsgericht: Mannheim,  HRB: 105845 }

Re: Drawing only a portion of a PDF (Graphics.clip is ignored/overwritten)

Posted by Jan Buschtoens <jb...@abm.de>.
I've created the issue: PDFBOX-4583



 From:   Tilman Hausherr <TH...@t-online.de> 
 To:   <us...@pdfbox.apache.org> 
 Sent:   2019-06-26 18:16 
 Subject:   Re: Drawing only a portion of a PDF (Graphics.clip is ignored/overwritten) 

Very good question. Yes, PageDrawer uses setClip(). We can still do  
this, but should remember the initial clipping path and intersect that  
one. Please create an issue tomorrow if there isn't a different opinion. 
 
Tilman 
 

--

{ Geschaeftsfuehrer: Dipl.-Inform. Christopher Asp,  Amtsgericht: Mannheim,  HRB: 105845 }

Re: Drawing only a portion of a PDF (Graphics.clip is ignored/overwritten)

Posted by Tilman Hausherr <TH...@t-online.de>.
Very good question. Yes, PageDrawer uses setClip(). We can still do 
this, but should remember the initial clipping path and intersect that 
one. Please create an issue tomorrow if there isn't a different opinion.

Tilman

Am 26.06.2019 um 09:29 schrieb Jan Buschtoens:
> Hi.
>
>
> I want to use PDFBox to draw PDFs in a swing application.
>
>
> I already created a  new issue for a problem I had on Linux (PDFBOX-4581). But unrelated to that I had another problem (on windows and  linux), and I wanted to ask first if I'm doing something wrong before I create a new issue.
>
>
> I want to draw only a portion of the PDF. As far as I know, the way to do this in swing is with the "clip"-method on the Graphics object. However, no matter what clip I set, the wohle PDF is drawn. I believe that PDFRenderer calls "setClip", when it probably should use "clip". The latter sets the current clip to the intersection of the old current clip and the provided shape, wheras the former resets the current clip.
>
>
>
> My code is essentially the following:
>
>
>          @Override
>          public void paint(final Graphics g) {
>
>              final Graphics2D g2 = (Graphics2D) g;
>              try {
>                  PDDocument pdDocument = PDDocument.load(App.class.getResourceAsStream("somepdf.pdf"));
>                  g2.clip(new Rectangle(20, 20));
>                  final PDFRenderer pdfRenderer = new org.apache.pdfbox.rendering.PDFRenderer(pdDocument);
>                  pdfRenderer.renderPageToGraphics(0, g2);
>              } catch (final IOException e) {
>                  throw new RuntimeException(e);
>              }
>          }
>
>
> --
>
> { Geschaeftsfuehrer: Dipl.-Inform. Christopher Asp,  Amtsgericht: Mannheim,  HRB: 105845 }



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org