更改现有 PDF 文档的方向 (portrait/landscape)

Change orientation of existing PDF document (portrait/landscape)

是否可以将现有文档的方向从 'portrait' 更改为 'landscape',反之亦然?

我试过使用 iText 将页面(纵向模式)复制到新创建的页面(横向模式),但没有用,因为使用了复制页面的页面方向。

这是我使用的代码:

PdfReader originalFileReader = new PdfReader(src);

Document landscapeDoc = new Document(PageSize.A4.rotate());

PdfCopy copy = new PdfCopy(landscapeDoc, new FileOutputStream("/home/user/landscape.pdf"));

landscapeDoc.open();

for (int i = 1; i <= originalFileReader.getNumberOfPages(); i++) {
     copy.addPage(copy.getImportedPage(originalFileReader, i));
}

landscapeDoc.close();

从原始文件中获取页面并将其添加到副本中不会重新布局页面。如果您获得横向页面,它只会包含裁剪到横向页面高度的原始页面的副本。

正在查看 iText site it appears that the 2 closest use-cases to what you want are extracting data fields (marked up using a template) from a PDF to an XML structure (pdf2Data) and adding content (watermarks, images, annotations, etc.) to an existing PDF. (Lots of examples here。)

智能地从 PDF 中提取内容和格式并在不同的 PDF 中重新布局是没有意义的。 (无论如何,这将是一个非常的难题。)