将 Link 添加到 Canvas 时出现 iText 错误
iText error when adding Link onto Canvas
我 运行 在 iText 7 中遇到了一个非常奇怪的问题,我希望其他人过去处理过这个问题。我本质上只是试图通过将一系列 Link 对象添加到 Paragraph 对象中,然后将 Paragraph 放入 Canvas 对象中来创建 table 内容。这是简化代码的示例:
PdfCanvas pdfCanvas = new PdfCanvas(document.getPdfDocument().addNewPage());
Rectangle rectangle = new Rectangle(36, 650, 100, 100);
pdfCanvas.rectangle(rectangle);
pdfCanvas.stroke();
Canvas canvas = new Canvas(pdfCanvas, document.getPdfDocument(), rectangle);
canvas.add(new Paragraph(new Link("Google", PdfAction.createGoToR("HELLO", "www.google.com"))));
如您所见,这非常简单。然而,当我这样做时,我得到一个空指针异常。我可以毫无问题地添加简单的文本,但是当我添加 link 时,事情就乱套了。任何帮助将不胜感激。
这是在 PdfCanvas
:
上绘制矩形(或在本例中为正方形)的方法
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfPage page = pdf.addNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rectangle = new Rectangle(36, 650, 100, 100);
pdfCanvas.rectangle(rectangle);
pdfCanvas.stroke();
pdf.close();
您不需要 Document
对象。您只需创建一个 PdfCanvas
并绘制一个矩形,其左下角 x = 36; y = 360
尺寸为 100 x 100 个用户单位。
你引入了一个Document
对象,因为你还想创建一个Link
。那也没有必要。你可以试试这个(但那是错误的):
Canvas canvas = new Canvas(pdfCanvas, pdf, rectangle);
canvas.add(new Paragraph().add("Google"));
canvas.add(new Paragraph(new Link("Google", PdfAction.createGoToR("HELLO", "www.google.com"))));
pdf.close();
如您所见,我们仅使用 pdf
(一个 PdfDocument
)和 pdf
。没有 Document
涉及。但是,您正在尝试将 link 添加到 Canvas
对象。如果要将 PDF 语法添加到 内容流 ,可以使用 Canvas
。 link 不是内容流的一部分。 link 是存储在页面字典的 /Annots
条目中的注释。简而言之:您将 Canvas
用于无法使用的东西。
我认为您正试图将 link 放在绝对位置,并且您想在 link 周围放置一个矩形。这不是使用 Canvas
完成的。如果你只是把一个 Paragraph
放在绝对位置就容易多了。
例如:
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
Paragraph p = new Paragraph()
.add(new Link("Google", PdfAction.createGoToR("HELLO", "www.google.com")))
.setFixedPosition(36, 650, 80)
.setBorder(new SolidBorder(0.5f));
document.add(p);
document.close();
}
这会在位置 (x = 36; y = 650)
处添加一个 Paragraph
,宽度为 80 个用户单位。我们在 Paragraph
.
周围添加了一个 0.5 粗的边框
这也不行,因为 link 都是错误的。您正在使用 GoToR
(转到远程)操作,该操作旨在转到另一个 PDF 文件中的特定目标。我认为您需要一个 URI 操作:
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
Paragraph p = new Paragraph()
.add(new Link("Google", PdfAction.createURI("www.google.com")))
.setFixedPosition(36, 650, 80)
.setBorder(new SolidBorder(0.5f));
document.add(p);
document.close();
}
如果您想在文本周围获得更多 space,您可以更改填充:
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
Paragraph p = new Paragraph()
.add(new Link("Google", PdfAction.createURI("www.google.com")))
.setFixedPosition(36, 650, 80)
.setPadding(10)
.setBorder(new SolidBorder(0.5f));
document.add(p);
document.close();
}
这比您试图实现的要直观得多。
我 运行 在 iText 7 中遇到了一个非常奇怪的问题,我希望其他人过去处理过这个问题。我本质上只是试图通过将一系列 Link 对象添加到 Paragraph 对象中,然后将 Paragraph 放入 Canvas 对象中来创建 table 内容。这是简化代码的示例:
PdfCanvas pdfCanvas = new PdfCanvas(document.getPdfDocument().addNewPage());
Rectangle rectangle = new Rectangle(36, 650, 100, 100);
pdfCanvas.rectangle(rectangle);
pdfCanvas.stroke();
Canvas canvas = new Canvas(pdfCanvas, document.getPdfDocument(), rectangle);
canvas.add(new Paragraph(new Link("Google", PdfAction.createGoToR("HELLO", "www.google.com"))));
如您所见,这非常简单。然而,当我这样做时,我得到一个空指针异常。我可以毫无问题地添加简单的文本,但是当我添加 link 时,事情就乱套了。任何帮助将不胜感激。
这是在 PdfCanvas
:
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfPage page = pdf.addNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rectangle = new Rectangle(36, 650, 100, 100);
pdfCanvas.rectangle(rectangle);
pdfCanvas.stroke();
pdf.close();
您不需要 Document
对象。您只需创建一个 PdfCanvas
并绘制一个矩形,其左下角 x = 36; y = 360
尺寸为 100 x 100 个用户单位。
你引入了一个Document
对象,因为你还想创建一个Link
。那也没有必要。你可以试试这个(但那是错误的):
Canvas canvas = new Canvas(pdfCanvas, pdf, rectangle);
canvas.add(new Paragraph().add("Google"));
canvas.add(new Paragraph(new Link("Google", PdfAction.createGoToR("HELLO", "www.google.com"))));
pdf.close();
如您所见,我们仅使用 pdf
(一个 PdfDocument
)和 pdf
。没有 Document
涉及。但是,您正在尝试将 link 添加到 Canvas
对象。如果要将 PDF 语法添加到 内容流 ,可以使用 Canvas
。 link 不是内容流的一部分。 link 是存储在页面字典的 /Annots
条目中的注释。简而言之:您将 Canvas
用于无法使用的东西。
我认为您正试图将 link 放在绝对位置,并且您想在 link 周围放置一个矩形。这不是使用 Canvas
完成的。如果你只是把一个 Paragraph
放在绝对位置就容易多了。
例如:
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
Paragraph p = new Paragraph()
.add(new Link("Google", PdfAction.createGoToR("HELLO", "www.google.com")))
.setFixedPosition(36, 650, 80)
.setBorder(new SolidBorder(0.5f));
document.add(p);
document.close();
}
这会在位置 (x = 36; y = 650)
处添加一个 Paragraph
,宽度为 80 个用户单位。我们在 Paragraph
.
这也不行,因为 link 都是错误的。您正在使用 GoToR
(转到远程)操作,该操作旨在转到另一个 PDF 文件中的特定目标。我认为您需要一个 URI 操作:
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
Paragraph p = new Paragraph()
.add(new Link("Google", PdfAction.createURI("www.google.com")))
.setFixedPosition(36, 650, 80)
.setBorder(new SolidBorder(0.5f));
document.add(p);
document.close();
}
如果您想在文本周围获得更多 space,您可以更改填充:
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
Paragraph p = new Paragraph()
.add(new Link("Google", PdfAction.createURI("www.google.com")))
.setFixedPosition(36, 650, 80)
.setPadding(10)
.setBorder(new SolidBorder(0.5f));
document.add(p);
document.close();
}
这比您试图实现的要直观得多。