iTextPDF - 不能在已插入锚点的表上使用writeSelectedRows() [英] iTextPDF - Cannot use writeSelectedRows() on a table where an anchor has been inserted
问题描述
我正在将我的PDF的内容页面从使用页码作为超链接转换为锚点,因为有一些间接限制,并且链接需要更加动态。
I am in the process of converting the contents page of my PDF from using page numbers as a hyperlink, to anchors because of a few circumstantial limitations and the linking needs to be more dynamic.
我省略了外循环代码,但我试图使用以下内容在内容页面中创建一个超链接条目:
I have omitted outer-loop code, but I am attempting to create a hyperlinked entry in the contents page using the following:
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
Anchor anchor = new Anchor("page18 link");
anchor.setReference("#page18");
p.add(anchor);
cell.addElement(p);
table.addCell(cell);
一旦生成了内容页面(即已添加所有行),我就会使用表上的 writeSelectedRows
:
Once the contents page has been generated (i.e. all the rows have been added), I then use the writeSelectedRows
on the table:
table.writeSelectedRows(0, -1, PageSize.A4.getWidth()*.05f, PageSize.A4.getHeight()-100, stamper.getOverContent(prevSectionPageCount+currentIndexPage+1));
执行此操作时,我收到以下异常:
On doing this, I get the following exception:
原因异常是:StamperPDFPlugin出错。 null
java.lang.NullPointerException
com.itextpdf.text.pdf.internal.PdfAnnotationsImp.addPlainAnnotation(PdfAnnotationsImp.java:125)
at com.itextpdf.text.pdf.PdfDocument。 localGoto(PdfDocument.java:2115)
at
com.itextpdf.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:1612)
at com.itextpdf.text.pdf.ColumnText。 go(ColumnText.java:1025)at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:877)at
com.itextpdf.text.pdf.ColumnText.goComposite(ColumnText。 java:1381)at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:882)at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:877) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:866)at
com.itextpdf.text.pdf.PdfPRow.writeCells(PdfPRow.java:549)at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:767)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:897)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:845)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:823)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin.transform(StamperPDFPlugin.java:584)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin。 transform(StamperPDFPlugin.java:328)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin.executeProfile(StamperPDFPlugin.java:171)
Cause Exception was: Error in StamperPDFPlugin. null java.lang.NullPointerException at com.itextpdf.text.pdf.internal.PdfAnnotationsImp.addPlainAnnotation(PdfAnnotationsImp.java:125) at com.itextpdf.text.pdf.PdfDocument.localGoto(PdfDocument.java:2115) at com.itextpdf.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:1612) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:1025) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:877) at com.itextpdf.text.pdf.ColumnText.goComposite(ColumnText.java:1381) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:882) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:877) at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:866) at com.itextpdf.text.pdf.PdfPRow.writeCells(PdfPRow.java:549) at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:767) at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:897) at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:845) at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:823) at com.ems.rendition.cts.plugin.StamperPDFPlugin.transform(StamperPDFPlugin.java:584) at com.ems.rendition.cts.plugin.StamperPDFPlugin.transform(StamperPDFPlugin.java:328) at com.ems.rendition.cts.plugin.StamperPDFPlugin.executeProfile(StamperPDFPlugin.java:171)
在看到 localGoto
的堆栈跟踪条目时,我拿出了行 anchor.setReference(#18 .pdf);
并且没有错误就完成了(但很明显没有超链接 - 只有纯文本)。
On seeing the stack trace entry for localGoto
, I took out the line anchor.setReference("#18.pdf");
and it completed fine without error (but obviously with the absence of the hyperlinks - only plain text).
什么是这里出错?我是否错误地将锚点添加到单元格中?
What is going wrong here? Am I adding the anchor to the cell incorrectly?
谢谢
推荐答案
请查看 LinkInPositionedTable :
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Anchor target = new Anchor("top");
target.setName("page18");
document.add(target);
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(500);
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
Anchor anchor = new Anchor("page18 link");
anchor.setReference("#page18");
p.add(anchor);
cell.addElement(p);
table.addCell(cell);
table.writeSelectedRows(0, -1, 36, 700, writer.getDirectContent());
document.close();
}
在这个例子中,我创建了一个名为的锚第18页
(虽然它只是指向页面的顶部),并使用您的代码片段将对该锚点的引用添加到 PdfPTable
。
In this example, I create an anchor with name page18
(although it just refers to the top of the page) and a reference to that anchor added to a PdfPTable
using your code snippet.
您可以在此处找到结果: link_in_positioned_table .pdf
You can find the result here: link_in_positioned_table.pdf
当使用iText 5.5.7(这是最新版本)时,这对我有用。
This works for me, when using iText 5.5.7 (which is the most recent version).
这篇关于iTextPDF - 不能在已插入锚点的表上使用writeSelectedRows()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!