早上好, 我一直在跟踪合并TOC示例,并且没有任何运气让PdfActions做出回应。这是我的片段:
PdfReader finalReader = new PdfReader(viewableStream.toByteArray());
Document finalDoc = new Document(PageSize.LETTER);
PdfCopy finalPdfCopy = new PdfCopy(finalDoc,stream);
PdfCopy.PageStamp finalPdfStamp;
finalDoc.open();
int finalNumPages = finalReader.getNumberOfPages();
for(int finalPageIndex = 1;finalPageIndex <= finalNumPages;finalPageIndex++) {
PdfImportedPage finalImpPage = finalPdfCopy.getImportedPage(finalReader,finalPageIndex);
finalPdfStamp = finalPdfCopy.createPageStamp(finalImpPage);
if (finalPageIndex == tocPage) {
new TOCCustomizer().generateLinks(finalDoc, finalPdfCopy, finalPdfStamp, finalImpPage, vApp.getBookBuildContext(), book);
}
finalPdfStamp.alterContents();
finalPdfCopy.addPage(finalImpPage);
}
finalDoc.close();
finalPdfCopy.close();
stream.flush();
stream.close();
generateLinks方法:
public void generateLinks(Document finalDoc,PdfCopy finalPdfCopy,PdfCopy.PageStamp stamp,PdfImportedPage srcPage,BookBuilderContext context, Book book) throws Exception
{
finalDoc.setMargins(0.75f * 72, 0.75f * 72, 0.75f * 72, 0.75f * 72);
finalDoc.add(new Paragraph(" "));
File fontFile = new File(context.getResourcePath(), "ProximaNova-Reg.otf");
BaseFont bfRegular = BaseFont.createFont(
fontFile.getAbsolutePath(),
BaseFont.CP1252,
BaseFont.EMBEDDED
);
Font font1 = new Font(bfRegular,14, Font.NORMAL, Colors.SLATE_GREY);
// create a table for the content
PdfPTable table = new PdfPTable(3);
table.setTotalWidth(7.5f*72);
table.setWidths(new float[]{32f,458f,50f});
int currentPage = 1;
for (int i = 0; i < book.getNumSections(); i++)
{
v.bookmodel.Section s = book.getSection(i);
String itemNbr = s.getItemNbr();
// don't add excluded sections
if (s.isIncluded() && !TOC_EXCLUDE.contains(itemNbr))
{
PdfPCell sectNumCell = new PdfPCell(new Phrase(i+". ",font1));
sectNumCell.setBorder(0);
sectNumCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
sectNumCell.setPaddingLeft(5);
sectNumCell.setPaddingBottom(10);
sectNumCell.setPaddingTop(15);
table.addCell(sectNumCell);
Chunk sectChunk = new Chunk(s.getSectionName());
sectChunk.setFont(font1);
// sectChunk.setAction(PdfAction.gotoLocalPage(currentPage, new PdfDestination(PdfDestination.FITH), finalPdfCopy));
sectChunk.setAction(new PdfAction(PdfAction.FIRSTPAGE));
Phrase sectPhrase = new Phrase(sectChunk);
PdfPCell sectDescCell = new PdfPCell(sectPhrase);
sectDescCell.setBorder(0);
sectDescCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
sectDescCell.setPaddingLeft(5);
sectDescCell.setPaddingBottom(10);
sectDescCell.setPaddingTop(15);
table.addCell(sectDescCell);
PdfPCell sectPageCell = new PdfPCell(new Phrase(currentPage+"",font1));
sectPageCell.setBorder(0);
sectPageCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
sectPageCell.setPaddingLeft(5);
sectPageCell.setPaddingBottom(10);
sectPageCell.setPaddingTop(15);
table.addCell(sectPageCell);
int sectionPages = s.getSectionTotalPages();
currentPage+=sectionPages;
} else if (s.isIncluded() && TOC_SECTIONS.contains(itemNbr)) {
int sectionPages = s.getSectionTotalPages();
currentPage+=sectionPages;
}
}
正在创建文档,但TOC链接不会返回到第一页。我确实将其转换为使用PdfWriter而不是PdfCopy,这适用于链接,但由于原始的pdf拳击不同,我无法使用PdfWriter。我认为有一些我想念的简单事物,但却在努力去看它是什么。任何帮助将不胜感激。我目前在这个项目上使用的是2.0.7版本。
先谢谢, -Jeff