I have a pdf that has been passed to me with a signature (annotation) and I have to scale the pdf, but when I scale the pdf the annotation does not scale, and it comes out out out of shape. Any suggestions? I'll show you the code of the method and the pdf before and after
PDF BEFORE
PDF AFTER
Method
public static byte[] adjustPages(byte[] content) {
try (PDDocument pdf = PDDocument.load(content)) {
float letterWidth = PDRectangle.LETTER.getWidth();
float letterHeight = PDRectangle.LETTER.getHeight();
PDPageTree tree = pdf.getDocumentCatalog().getPages();
for (PDPage page : tree) {
if (page.getMediaBox().getWidth() > letterWidth || page.getMediaBox().getHeight() > letterHeight) {
float fWidth = letterWidth / page.getMediaBox().getWidth();
float fHeight = letterHeight / page.getMediaBox().getHeight();
float factor = Math.min(fWidth, fHeight);
PDPageContentStream contentStream = new PDPageContentStream(pdf, page, PDPageContentStream.AppendMode.PREPEND, false);
contentStream.transform(Matrix.getScaleInstance(factor, factor));
contentStream.close();
page.setMediaBox(PDRectangle.LETTER);
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pdf.save(baos);
return baos.toByteArray();
}catch (IOException ioe) {
return content;
}
}