TOC内容超过写入问题

时间:2017-08-07 04:50:20

标签: java itext

我试图从合并文件创建TOC并需要TOC作为我的第一页。当toc有更多数据时,现在面临一个问题,它被写入同一个文件。我期望在现有的toc中创建一个新页面.pdf文件并写下剩余的TOC内容。

PdfReader tocReader = new PdfReader("toc.pdf");
page = copy.getImportedPage(tocReader, 1);
stamp = copy.createPageStamp(page);
int tocPageCount = 1;
Paragraph paragraph;
PdfAction action;
PdfAnnotation link;
float y = 770;
PdfImportedPage newPage = null;
Rectangle pagesize = tocReader.getPageSize(1);
ColumnText colTxt = new ColumnText(stamp.getOverContent());
colTxt.setSimpleColumn(36, 36, 559, y);
for (TocModel tocModel : toc) {
    paragraph = new Paragraph(tocModel.getTitle());
    paragraph.add(new Chunk(new DottedLineSeparator()));
    paragraph.add(String.valueOf(tocModel.getPageNo()));
    colTxt.addElement(paragraph);
    colTxt.go();
    // seting toc action
    action = PdfAction.gotoLocalPage("p" + tocModel.getPageNo(), false);
    link = new PdfAnnotation(copy, 36, colTxt.getYLine(), 559, y,action);
    stamp.addAnnotation(link);
    y = colTxt.getYLine();
}

int status = colTxt.go();
status = colTxt.go();
if (ColumnText.hasMoreText(status)) {
    PdfContentByte canvas = stamp.getOverContent();
    canvas.addTemplate(page, 0, 0);
    colTxt.setCanvas(canvas);
    colTxt.setSimpleColumn(new Rectangle(36, 36, 559, 806));
    colTxt.go();
}

stamp.alterContents();
copy.addPage(page);
document.close();
logger.info("Finished TOC !!!");

tocReader = new PdfReader(bos.toByteArray());
noOfPages =tocReader.getNumberOfPages();
tocReader.selectPages(String.format("%d, 1-%d", noOfPages, noOfPages - 1));
PdfStamper stamper = new PdfStamper(tocReader, new FileOutputStream(outPutDirectory + "merge.pdf"));

stamper.close();
logger.info("merging completed!!!");

我参考以下代码 Adding table to existing PDF on the same page - ITEXT

当前输出文件:

current output file

1 个答案:

答案 0 :(得分:0)

  

当toc有更多数据时,它会被写入同一个文件。我期望在现有的toc.pdf文件中创建一个新页面并写下剩余的TOC内容。

由于您的代码在溢出时创建其他页面, 无。

在您的代码中,您选择不使用最高级别的iText API(在该级别iText为您执行布局和分页功能),但是使用中级API(在此级别您可以自行进行布局和分页)。

她的两个可能的解决方案,一个与现有代码一致,仅修复它,另一个使用高级API。

修复代码

colTxt.go()尝试返回代码表明无法将该行添加到colTxt时,您实际上只需切换到下一页。

请替换

部分
PdfReader tocReader = new PdfReader("toc.pdf");
...
logger.info("Finished TOC !!!");

由此:

PdfReader tocReader = new PdfReader("toc.pdf");
page = copy.getImportedPage(tocReader, 1);
stamp = copy.createPageStamp(page);
int tocPageCount = 1;
Paragraph paragraph;
PdfAction action;
PdfAnnotation link;
float y = 770;
ColumnText colTxt = new ColumnText(stamp.getOverContent());
colTxt.setSimpleColumn(36, 36, 559, y);
for (TocModel tocModel : toc) {
    paragraph = new Paragraph(tocModel.getTitle());
    paragraph.add(new Chunk(new DottedLineSeparator()));
    paragraph.add(String.valueOf(tocModel.getPageNo()));
    colTxt.addElement(paragraph);
    if (ColumnText.hasMoreText(colTxt.go()))
    {
        stamp.alterContents();
        copy.addPage(page);
        tocReader = new PdfReader("toc.pdf");
        page = copy.getImportedPage(tocReader, 1);
        tocPageCount++;
        stamp = copy.createPageStamp(page);
        y = 770;
        colTxt = new ColumnText(stamp.getOverContent());
        colTxt.setSimpleColumn(36, 36, 559, y);
        colTxt.go();
    }
    // seting toc action
    action = PdfAction.gotoLocalPage("p" + tocModel.getPageNo(), false);
    link = new PdfAnnotation(copy, 36, colTxt.getYLine(), 559, y,action);
    stamp.addAnnotation(link);
    y = colTxt.getYLine();
}

stamp.alterContents();
copy.addPage(page);

document.close();
logger.info("Finished TOC !!!");

TableOfContents test testAddTocLikeArjImproved

如您所见,只要我们检测到ColumnText.hasMoreText(colTxt.go()),我们就会引入新页面。由于页面标记实际上操纵了源页面(据我记忆,也就是说,我几乎没有做过这些...),我们需要从一个新的阅读器导入。

(可以通过在不再需要读取器时关闭读取器来改进代码;在这里我们允许它们一直延续到垃圾收集释放它们。)

以不同方式执行

可以像这样使用iText高级API。

请替换

部分
PdfReader tocReader = new PdfReader("toc.pdf");
...
logger.info("Finished TOC !!!");

来自你的代码:

final PdfReader tocBackgroundReader = new PdfReader("toc.pdf");
Document tocDocument = new Document(tocBackgroundReader.getCropBox(1));
ByteArrayOutputStream tocBaos = new ByteArrayOutputStream();
PdfWriter tocWriter = PdfWriter.getInstance(tocDocument, tocBaos);
tocWriter.setPageEvent(new PdfPageEventHelper() {
    PdfImportedPage stationary = tocWriter.getImportedPage(tocBackgroundReader, 1);
    @Override
    public void onEndPage(PdfWriter writer, Document document)
    {
        writer.getDirectContentUnder().addTemplate(stationary, 0, 0);
    }
});
tocDocument.open();
for (TocModel tocModel : toc) {
    PdfAction action = PdfAction.gotoLocalPage("p" + tocModel.getPageNo(), false);

    Paragraph paragraph = new Paragraph();
    Chunk chunk = new Chunk(tocModel.getTitle());
    chunk.setAction(action);
    paragraph.add(chunk);
    chunk = new Chunk(new DottedLineSeparator());
    chunk.setAction(action);
    paragraph.add(chunk);
    chunk = new Chunk(String.valueOf(tocModel.getPageNo()));
    chunk.setAction(action);
    paragraph.add(chunk);
    tocDocument.add(paragraph);
}
tocDocument.close();

PdfReader tocReader = new PdfReader(tocBaos.toByteArray());
int tocPageCount = tocReader.getNumberOfPages();
copy.addDocument(tocReader);

document.close();
logger.info("Finished TOC !!!");

TableOfContents test testAddTocLikeArjAlternative

进行重新订购工作

您的页面重新排序仅支持单页。

请替换

tocReader.selectPages(String.format("%d, 1-%d", noOfPages, noOfPages - 1));

通过

tocReader.selectPages(String.format("%d-%d, 1-%d", noOfPages - tocPageCount + 1, noOfPages, noOfPages - tocPageCount));