Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Jul 15, 2024 8:06 am

Currently, our application uses Java and Spire.PDF 5.1.0.
The application is running on AWS Lambda.
We add images as layers to the PDF and generate a new PDF.
When adding layers, we want the reference point to be the top-left corner.
Therefore, we rotate each page before adding the layer.
Code: Select all
// Perform the following for each page and create a new PDF
PdfDocument basePdf = ...;

PdfDocument srcPdf = ...;

PdfPageBase page = srcPdf.getPages().get(pageNumber);
Dimension2D size = page.getSize();
if (page.getRotation() == PdfPageRotateAngle.Rotate_Angle_90
        || page.getRotation() == PdfPageRotateAngle.Rotate_Angle_270) {
    // width <- height, height <- width
    size.setSize(page.getSize().getHeight(), page.getSize().getWidth());
}

PdfDocument newPdfPage = new PdfDocument();
newPdfPage.getPages().add(size, new PdfMargins(0));
page.createTemplate().draw(newPdfPage.getPages().get(0).getCanvas(), new Point2D.Float(0, 0));

basePdf.appendPage(newPdfPage);

// Add a layer to the new PDF
PdfLayer pdfLayer = basePdf.getLayers().addLayer(layerName);
PdfCanvas canvas = pdfLayer.createGraphics(basePdf.getPages().get(pageNumber).getCanvas());
PdfImage pngImage = ...;
canvas.drawImage(pngImage, 0, 0, (float) pngImage.getWidth(), (float) pngImage.getHeight());


1 Is this method correct?
2 With this method, if there are links in the PDF, they are not carried over to the new PDF. Is there a way to carry over the links?

team_ssb
 
Posts: 24
Joined: Fri Nov 26, 2021 1:30 pm

Mon Jul 15, 2024 9:13 am

Hello,

Thanks for your inquiry.
Yes, you can add layers to pdf using the current method. Please note that createGraphics() method does not keep the links in the original page. If you want to keep it, you can refer to the code snippet below for testing. If your issue still exists, please provide us with your test file and complete test code to help us investigate further. You can upload them to the attachment here or send them to this email: support@e-iceblue.com. Thanks in advance.
Code: Select all
//        PdfDocument newPdfPage = new PdfDocument();
//        newPdfPage.getPages().add(size, new PdfMargins(0));
//        page.createTemplate().draw(newPdfPage.getPages().get(0).getCanvas(), new Point2D.Float(0, 0));
//        basePdf.appendPage(newPdfPage);

// to keep the links
basePdf.insertPage(srcPdf,0);


Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 454
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.PDF