首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Itext pdf Merge :文档在pdf (文本截断)页面外溢出,不显示

Itext pdf Merge :文档在pdf (文本截断)页面外溢出,不显示
EN

Stack Overflow用户
提问于 2013-02-08 18:53:48
回答 2查看 3.5K关注 0票数 0

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

合并文档后,如下所示

Java代码如下:

代码语言:javascript
复制
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;
        }

请帮帮忙。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-08 19:34:44

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

票数 2
EN

Stack Overflow用户

发布于 2013-02-14 13:49:30

我使用了"PdfCopyFields“片段,如下所示:

代码语言:javascript
复制
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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14770942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档