我正在使用iText7(java)并尝试将纵向文档缩放到横向。该文档包含链接并将旋转设置为270.由于旋转,在文档上设置mediabox和cropbox不起作用。 iText Examples中提到的解决方案无法正常工作,文档中的超链接停止工作。也可能丢失其他元数据。
有没有办法在不使用copyAsFormXObject的情况下缩放文档。 在itext7中使用以下方法来操作文档非常适合添加页脚,水印等。有没有办法在下面的代码中扩展文档?
PdfDocument pdfDoc = new PdfDocument(new PdfReader(srcDoc), new PdfWriter(outPut));
for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) {
PdfPage page = pdfDoc.getPage(i);
page.setIgnorePageRotationForContent(true);
// change page scaling here
}
pdfDoc.close();
我有以下工作代码来复制链接并将文档缩放到全宽。任何人都可以使用以下代码,如果他们需要的话。
boolean isLandscape = CommonConstants.LAYOUT_LANDSCAPE.equalsIgnoreCase(vo.getLayout());
ByteArrayOutputStream resizedPdfBytes = new ByteArrayOutputStream();
PdfWriter pdfWriter = new PdfWriter(resizedPdfBytes);
PdfReader pdfReader = new PdfReader(new ByteArrayInputStream(mergedPdfBytes.toByteArray()));
PdfDocument writerDoc = new PdfDocument(pdfWriter);
PdfDocument readerDoc = new PdfDocument(pdfReader);
Rectangle targetSize = new Rectangle(PageSize.A4.getWidth(), PageSize.A4.getHeight());
if(isLandscape){
targetSize = new Rectangle(PageSize.A4.getHeight(), PageSize.A4.getWidth());
}
for (int i = 1; i <= readerDoc.getNumberOfPages(); i++) {
PdfPage origPage = readerDoc.getPage(i);
PdfPage page = writerDoc.addNewPage(new PageSize(targetSize));
Rectangle orig = origPage.getPageSizeWithRotation();
double heightScale = 1, widthScale = 1;
widthScale = targetSize.getHeight() / orig.getHeight();
heightScale = targetSize.getWidth() / orig.getWidth();
for (int k = 0; k < origPage.getAnnotsSize(); k++) {
PdfAnnotation annotation = origPage.getAnnotations().get(k);
PdfAnnotation annotation1 = new PdfLinkAnnotation(annotation.getPdfObject().copyTo(writerDoc));
PdfArray array = new PdfArray();
Rectangle oldArray = annotation.getRectangle().toRectangle();
array.add(new PdfNumber(heightScale * oldArray.getLeft()));
array.add(new PdfNumber(widthScale * oldArray.getTop()));
array.add(new PdfNumber(heightScale * oldArray.getRight()));
array.add(new PdfNumber(widthScale * oldArray.getBottom()));
annotation1.setRectangle(array);
page.addAnnotation(annotation1);
}
PdfCanvas canvas = new PdfCanvas(page);
AffineTransform transformationMatrix = AffineTransform.getScaleInstance(heightScale, widthScale);
canvas.concatMatrix(transformationMatrix);
PdfFormXObject pageCopy = origPage.copyAsFormXObject(writerDoc);
canvas.addXObject(pageCopy, 0, 0);
}
writerDoc.close();
readerDoc.close();
这与itext2非常相似,我希望有更好的方法来做到这一点。