pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan Buschtoens <jbuschto...@abm.de>
Subject Drawing only a portion of a PDF (Graphics.clip is ignored/overwritten)
Date Wed, 26 Jun 2019 07:29:33 GMT


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:

        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 }
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message