【问题标题】:How merge and rotate two pdf pages to one page itext如何将两个pdf页面合并并旋转到一页itext
【发布时间】:2019-10-30 19:17:16
【问题描述】:

我正在尝试创建合并三个 pdf 的 java 应用程序。前两个pdf我合并正常而不调整大小和旋转。我要旋转的最后一个 pdf 并将两个 pdf 页面放入一个 pdf 页面中。

我使用了 iText 5.5.13。我尝试合并两个 pdf,它的作品。

public void mergePdf(List<File> pdfFiles, File outputFile) {
    try {
        Document document = new Document();
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        PdfCopy copy = new PdfSmartCopy(document, outputStream);
        document.open();
        for (File inFile : pdfFiles) {
            PdfReader reader = new PdfReader(inFile.getAbsolutePath());
            copy.addDocument(reader);
            reader.close();
        }
        document.close();
    } catch (DocumentException | IOException ex) {
        ex.printStackTrace();
    }
}

我不知道如何将两个页面合并为一个 pdf 页面。

【问题讨论】:

    标签: java pdf itext


    【解决方案1】:

    合并:

    public void mergeTwoPagesIntoOne(String originalPdfFile, String outputPdfFile) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(originalPdfFile);
        Document doc = new Document(new RectangleReadOnly(842f, 595f), 0, 0, 0, 0);
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(outputPdfFile));
        doc.open();
        int totalPages = reader.getNumberOfPages();
        for (int i = 1; i <= totalPages; i = i + 2) {
            doc.newPage();
            PdfContentByte cb = writer.getDirectContent();
            PdfImportedPage page = writer.getImportedPage(reader, i); // page #1
    
            float documentWidth = doc.getPageSize().getWidth() / 2;
            float documentHeight = doc.getPageSize().getHeight();
            if (i > 1)
                documentHeight = documentHeight - 50f;
    
            float pageWidth = page.getWidth();
            float pageHeight = page.getHeight();
    
            float widthScale = documentWidth / pageWidth;
            float heightScale = documentHeight / pageHeight;
            float scale = Math.min(widthScale, heightScale);
    
            float offsetX = (documentWidth - (pageWidth * scale)) / 2;
            float offsetY = 0f;
    
            cb.addTemplate(page, scale, 0, 0, scale, offsetX, offsetY);
    
            if (i+1 <= totalPages) {
                PdfImportedPage page2 = writer.getImportedPage(reader, i+1); // page #2
    
                pageWidth = page.getWidth();
                pageHeight = page.getHeight();
    
                widthScale = documentWidth / pageWidth;
                heightScale = documentHeight / pageHeight;
                scale = Math.min(widthScale, heightScale);
    
                offsetX = ((documentWidth - (pageWidth * scale)) / 2) + documentWidth;
                cb.addTemplate(page2, scale, 0, 0, scale, offsetX, offsetY);
            }
        }
        doc.close();
    }
    

    旋转:

        public void rotatePdf(String originalPdfFile, String outputPdfFile, int degrees) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(originalPdfFile);
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            PdfDictionary dictionary = reader.getPageN(i);
            dictionary.put(PdfName.ROTATE, new PdfNumber(degrees));
        }
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputPdfFile));
        stamper.close();
        reader.close();
    }
    

    【讨论】:

      【解决方案2】:

      通常,您希望将每个页面变成一个 XObject(@mflorczak 的答案中的 PdfImportedPage),并将其中两个绘制到一个页面中。

      如果您事先知道所有页面的大小和形状都相同,那么您可以提前决定并硬编码您希望它们的显示方式。如果没有,您需要能够以一般方式处理可能大量的组合。

      这称为“N-up”打印,如果您想了解更多信息,互联网上有很多关于它的文章。您的特定示例是“2-up”打印。

      1. 获取要合并的页面大小
      2. 在您的输出文档中创建一个足以容纳两者的新页面
      3. 为您要导入的页面创建PdfImportedPages
      4. 在输出页面的PdfContentByte 上绘制具有所需对齐和旋转的导入页面。
      5. 转到 1
      6. 无法访问
      7. 利润!

      您可能想要尝试与@mflorczak 的答案中提供的比例和偏移(可能还有旋转)相比,上述一般理论应该有助于指导您。 PdfImportedPage 应该使您与源页面的轮换[s] 隔离,这样您只需要考虑它们的效果边界。

      【讨论】:

        猜你喜欢
        • 2013-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-24
        • 2011-05-04
        • 1970-01-01
        • 2015-12-02
        • 1970-01-01
        相关资源
        最近更新 更多