我正在尝试创建合并三个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页。

最佳答案

合并:

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();
}

10-07 13:15