如何在java中使用itext创建多页内容表
我使用示例MergeWithToc2.java创建TOC。 我尝试了几件事来解决这个问题,但都没有成功如何在java中使用itext创建多页内容表,itext,Itext,我使用示例MergeWithToc2.java创建TOC。 我尝试了几件事来解决这个问题,但都没有成功 public class MergeWithToc2 { public static final String SRC1 = "PositionPdf.pdf"; public static final String SRC2 = "concatenated1.pdf"; public static final String SRC3 = "new_page.pdf"
public class MergeWithToc2 {
public static final String SRC1 = "PositionPdf.pdf";
public static final String SRC2 = "concatenated1.pdf";
public static final String SRC3 = "new_page.pdf";
public static final String DEST = "test/merge_with_toc2.pdf";
public Map<String, PdfReader> filesToMerge;
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
MergeWithToc2 app = new MergeWithToc2();
app.createPdf(DEST);
}
public MergeWithToc2() throws IOException {
filesToMerge = new TreeMap<String, PdfReader>();
for(int i=0 ; i <50 ; i++ ){
filesToMerge.put(i + "Hello World", new PdfReader(SRC1));
//filesToMerge.put("02 Movies / Countries", new PdfReader(SRC2));
}
}
public void createPdf(String filename) throws IOException, DocumentException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Map<Integer, String> toc = new TreeMap<Integer, String>();
Document document = new Document();
PdfCopy copy = new PdfCopy(document, baos);
PageStamp stamp;
document.open();
int n;
int pageNo = 0;
PdfImportedPage page;
Chunk chunk;
for (Map.Entry<String, PdfReader> entry : filesToMerge.entrySet()) {
n = entry.getValue().getNumberOfPages();
toc.put(pageNo + 1, entry.getKey());
for (int i = 0; i < n; ) {
pageNo++;
page = copy.getImportedPage(entry.getValue(), ++i);
stamp = copy.createPageStamp(page);
chunk = new Chunk(String.format("Page %d", pageNo));
if (i == 1)
chunk.setLocalDestination("p" + pageNo);
ColumnText.showTextAligned(stamp.getUnderContent(),
Element.ALIGN_RIGHT, new Phrase(chunk),
559, 810, 0);
stamp.alterContents();
copy.addPage(page);
}
}
PdfReader reader = new PdfReader(SRC3);
page = copy.getImportedPage(reader, 1);
stamp = copy.createPageStamp(page);
Paragraph p;
PdfAction action;
PdfAnnotation link;
float y = 770;
ColumnText ct = new ColumnText(stamp.getOverContent());
ct.setSimpleColumn(36, 36, 559, y);
for (Map.Entry<Integer, String> entry : toc.entrySet()) {
p = new Paragraph(entry.getValue());
p.add(new Chunk(new DottedLineSeparator()));
p.add(String.valueOf(entry.getKey()));
ct.addElement(p);
ct.go();
action = PdfAction.gotoLocalPage("p" + entry.getKey(), false);
link = new PdfAnnotation(copy, 36, ct.getYLine(), 559, y, action);
stamp.addAnnotation(link);
y = ct.getYLine();
}
ct.go();
stamp.alterContents();
copy.addPage(page);
document.close();
for (PdfReader r : filesToMerge.values()) {
r.close();
}
reader.close();
reader = new PdfReader(baos.toByteArray());
n = reader.getNumberOfPages();
reader.selectPages(String.format("%d, 1-%d", n, n-1));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
stamper.close();
}
}
public类与toc2合并{
公共静态最终字符串SRC1=“PositionPdf.pdf”;
公共静态最终字符串SRC2=“concatenated1.pdf”;
公共静态最终字符串SRC3=“new_page.pdf”;
公共静态最终字符串DEST=“test/merge_with_toc2.pdf”;
公共地图文件合并;
公共静态void main(字符串[]args)引发IOException、DocumentException{
文件文件=新文件(DEST);
文件.getParentFile().mkdirs();
MergeWithToc2 app=新的MergeWithToc2();
app.createPdf(DEST);
}
public MergeWithToc2()引发IOException{
filesToMerge=newtreemap();
对于(int i=0;i)问题到底是什么?是否有错误,输出是否不是您想要的?(如果是,请说明您想要什么和得到什么)我猜你没有读过,因为这个疏忽,你不知道。如果这个猜测是错误的,请更具体一些,在提问之前请阅读文档。请不要复制/粘贴你不理解的代码。