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?