[
https://issues.apache.org/jira/browse/PDFBOX-4583?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16876303#comment-16876303
]
Tilman Hausherr commented on PDFBOX-4583:
-----------------------------------------
It works for me. However I noticed that you restore the transform. I think we
violated some rule by not restoring this (and many other things) ourselves. So
I changed the code to this:
{code}
final int r = 20;
final AffineTransform transform = g2.getTransform();
Stroke stroke = g2.getStroke();
Composite composite = g2.getComposite();
RenderingHints renderingHints = g2.getRenderingHints();
Color background = g2.getBackground();
Paint paint = g2.getPaint();
g2.clip(new Rectangle(r, r));
Shape clip = g2.getClip();
final PDDocument pdDocument =
PDDocument.load(PDFBox4583Clip2.class.getResourceAsStream("/de/abm/bug/good.pdf"));
// I did a direct loading here because I didn't set up an extra project
final PDFRenderer pdfRenderer = new
org.apache.pdfbox.rendering.PDFRenderer(pdDocument);
pdfRenderer.renderPageToGraphics(0, g2);
g2.setTransform(transform);
g2.setStroke(stroke);
g2.setComposite(composite);
g2.setRenderingHints(renderingHints);
g2.setClip(clip);
g2.setBackground(background);
g2.setPaint(paint);
g2.setColor(Color.black);
g2.drawRect(0, 0, r-1, r-1); // -1 is important or it won't be seen
{code}
> Drawing only a portion of a PDF (Graphics.clip is ignored/overwritten)
> ----------------------------------------------------------------------
>
> Key: PDFBOX-4583
> URL: https://issues.apache.org/jira/browse/PDFBOX-4583
> Project: PDFBox
> Issue Type: Bug
> Components: Rendering
> Affects Versions: 2.0.15
> Reporter: Jan Buschtöns
> Assignee: Tilman Hausherr
> Priority: Major
> Fix For: 2.0.17, 3.0.0 PDFBox
>
> Attachments: clip_PDFBox_FORMGEN-2184-master.zip
>
>
> I want to use PDFBox to draw PDFs in a swing application.
> 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 whole PDF is drawn.
> 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, whereas the former resets the current clip
> I've attached an example maven project, but the gist is:
> {code:java}
> @Override
> public void paint(final Graphics g) {
>
> final Graphics2D g2 = (Graphics2D) g;
> try {
> PDDocument pdDocument =
> DDocument.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);
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]