rototor / pdfbox-graphics2d

Graphics2D Bridge for pdfbox
61 stars 22 forks source link

Scale an existing page #24

Closed MichelMelhem closed 4 years ago

MichelMelhem commented 4 years ago

Hello, I'm quite new to pdfbox and i'm countering an issue. I want to convert an existing page to a vector graphics picture using your lib and then rescale it down to a4 and redraw it on a new page. I think i should be possible whith your lib but i couldn't find how i can achieve that. Do you have an idea how i can achieve that ? Or even if it is possible ? Thank you !

This is my actual code that do not work :

    public void scale() throws IOException {
        PDDocument document = PDDocument.load(pdf);

            PDPage page =  document.getPage(0);
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, page.getMediaBox().getWidth(), page.getMediaBox().getHeight());
            pdfBoxGraphics2D.dispose();

            PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
            Matrix matrix = new Matrix();
            float xScale = PDRectangle.A4.getWidth() / page.getMediaBox().getWidth();
            float yScale = PDRectangle.A4.getHeight() / page.getMediaBox().getHeight();
            matrix.scale(xScale, yScale);

            contentStream.saveGraphicsState();
            contentStream.transform(matrix);
            contentStream.drawForm(appearanceStream);
            contentStream.restoreGraphicsState();

            contentStream.close();

        document.save(pdf);
        document.close();
    }
rototor commented 4 years ago

What you want to do has nothing to do with this Graphics2D adapter. You "just" want to resize an existing page.

More something like this

 public void scale() throws IOException {
        PDDocument document = PDDocument.load(pdf);

            PDPage page =  document.getPage(0);
            PDPageContentStream contentStreamBefore = new PDPageContentStream(document, page, AppendMode.Prepend, true);

            Matrix matrix = new Matrix();
            float xScale = PDRectangle.A4.getWidth() / page.getMediaBox().getWidth();
            float yScale = PDRectangle.A4.getHeight() / page.getMediaBox().getHeight();
            matrix.scale(xScale, yScale);

            contentStreamBefore.transform(matrix);
            contentStreamBefore.close();

        document.save(pdf);
        document.close();
    }

This will add a transform to the whole page by adding an additional content stream with the transform matrix before the existing content.

I did not test this, but should work. (Maybe you need to invert xScale and yScale, as said did not test, so you may need to do a matrix.scale(1/xScale,1/yScale)).

This question is something for the PDFBox user mailing list or StackOverflow, as it's not related to this project.

MichelMelhem commented 4 years ago

Thank you, I thought I could be related to this project but no