【问题标题】:Itext pdf Merge : Document overflow outside pdf (Text truncated) page and not displayingItext pdf合并:pdf(文本被截断)页面外的文档溢出并且不显示
【发布时间】:2013-02-08 10:53:48
【问题描述】:

我正在尝试将 2 个 pdf 合并为一个。合并工作正常,但内容从 pdf 页面溢出。 A如附件所示。原文件pdf如下。

Merge Document 之后是这样的

Java代码如下:

BaseFont bf = BaseFont.createFont(BaseFont.TIMES_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED);
        //BaseFont bf= BaseFont.createFont();
        PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
        // data
        PdfImportedPage page;
        int currentPageNumber = 0;
        int pageOfCurrentReaderPDF = 0;
        Iterator<PdfReader> iteratorPDFReader = readers.iterator();

        // Loop through the PDF files and add to the output.
        while (iteratorPDFReader.hasNext()) {
            PdfReader pdfReader = iteratorPDFReader.next();

            // Create a new page in the target for each source page.
            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.newPage();
                pageOfCurrentReaderPDF++;
                currentPageNumber++;
                page = writer.getImportedPage(pdfReader,
                        pageOfCurrentReaderPDF);
                cb.addTemplate(page, 0, 0);

                // Code for pagination.
                if (paginate) {
                    cb.beginText();
                    cb.setFontAndSize(bf, 9);
                    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
                            + currentPageNumber + " of " + totalPages, 520,
                            5, 0);
                    cb.endText();
                }
            }
            pageOfCurrentReaderPDF = 0;
        }

请帮忙。

【问题讨论】:

  • 原始或目标 PDf 的一些缩放属性??
  • 您使用PdfWriter 而不是PdfCopy,并且忽略了您导入的页面的尺寸。因此,要么遵循布鲁诺在他的回答中的建议(复制页面原样),要么(如果前者由于调用您的方法的软件设计不当而无法实现)获取 all 页面尺寸数据通过创建所需大小的页面或缩放导入的页面来考虑导入的页面。

标签: java pdf-generation itext


【解决方案1】:

请下载chapter 6 of my book查看表6.1。您在使用 PdfWriter 而不是使用 PdfCopy 合并两个文档时犯了错误。查看清单 6.22,了解如何在使用 PdfCopy 时添加页码。

【讨论】:

    【解决方案2】:

    我使用“PdfCopyFields”片段如下:

    public static boolean concatPDFFiles(List<String> listOfFiles,
            String outputfilepath) throws FileNotFoundException, DocumentException {
        PdfCopyFields copy = null;
        try {
            copy = new PdfCopyFields(new FileOutputStream(outputfilepath));
        } catch (DocumentException ex) {
            Logger.getLogger(MergerGoogleDocsToPDF.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            for (String fileName : listOfFiles) {
                PdfReader reader1 = new PdfReader(fileName);
                copy.addDocument(reader1);
            }
        } catch (IOException ex) {
            Logger.getLogger(MergerGoogleDocsToPDF.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            copy.close();
        }
        if (new File(outputfilepath).exists()) {
            double bytes = new File(outputfilepath).length();
            //double kilobytes = (bytes / 1024);
            if (bytes != 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 2023-04-06
      • 1970-01-01
      • 2012-06-16
      • 1970-01-01
      • 2017-04-20
      相关资源
      最近更新 更多