itext 7: 添加 link 到 PDF 无边框或下划线
itex7: Add link to PDF without border or underline
我想在没有下划线或边框的文档中添加 link。
根据文档,我预计下面的第一个示例会导致 link 周围没有边框。那没有用,所以我找到了 ,我将其改编为第二个示例。使用这种方法,我可以用下划线替换边框,但不能同时去掉两者。
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("links.pdf"));
Document d = new Document(pdfDoc);
// example 1
Paragraph p = new Paragraph()
.Add(new Link("Link with border", PdfAction.CreateURI("http://www.google.com")).SetBorder(Border.NO_BORDER))
.SetBorder(Border.NO_BORDER); // no border on the paragraph
d.Add(p);
// example 2
PdfLinkAnnotation linkA = new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0));
linkA.SetHighlightMode(PdfLinkAnnotation.HIGHLIGHT_INVERT);
//This would give underline instead of borders
//linkA.SetBorderStyle(PdfLinkAnnotation.STYLE_UNDERLINE);
linkA.SetAction(PdfAction.CreateURI("http://www.google.com"));
Link link = new Link("Please no borders", linkA);
d.Add(new Paragraph(link));
pdfDoc.Close();
为了在第二个示例中没有边框,您需要通过 PdfLinkAnnotation#SetBorder(PdfAnnotationBorder)
方法而不是 SetBorderStyle
.
方法指定边框
这是您的代码片段,稍作修改:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("links.pdf"));
Document d = new Document(pdfDoc);
Link link = new Link("Please no borders", PdfAction.CreateURI("http://www.google.com"));
link.GetLinkAnnotation().SetBorder(new PdfAnnotationBorder(0, 0, 0));
d.Add(new Paragraph(link));
pdfDoc.Close();
您可以在 javadocs 中找到有关此方法调用的更多信息:http://itextsupport.com/apidocs/itext7/latest/com/itextpdf/kernel/pdf/annot/PdfAnnotation.html#getBorder--
一些背景
默认情况下,iText 不会为 link 注释指定任何特定的边框属性。根据 PDF 规范,注释的默认值是有边框的:
ISO32000-1 12.5.2 "Annotation Dictionaries" Table 164 – "Entries common to all annotation dictionaries":
Border - An array specifying the characteristics of the annotation’s border, which shall be drawn as a rounded rectangle.
The array consists of three numbers defining the horizontal corner radius, vertical corner radius, and border width, all in default user space units. If the corner radii are 0, the border has square (not rounded) corners; if the border width is 0, no border is drawn.
[...]
Default value: [0 0 1].
我想在没有下划线或边框的文档中添加 link。
根据文档,我预计下面的第一个示例会导致 link 周围没有边框。那没有用,所以我找到了
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("links.pdf"));
Document d = new Document(pdfDoc);
// example 1
Paragraph p = new Paragraph()
.Add(new Link("Link with border", PdfAction.CreateURI("http://www.google.com")).SetBorder(Border.NO_BORDER))
.SetBorder(Border.NO_BORDER); // no border on the paragraph
d.Add(p);
// example 2
PdfLinkAnnotation linkA = new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0));
linkA.SetHighlightMode(PdfLinkAnnotation.HIGHLIGHT_INVERT);
//This would give underline instead of borders
//linkA.SetBorderStyle(PdfLinkAnnotation.STYLE_UNDERLINE);
linkA.SetAction(PdfAction.CreateURI("http://www.google.com"));
Link link = new Link("Please no borders", linkA);
d.Add(new Paragraph(link));
pdfDoc.Close();
为了在第二个示例中没有边框,您需要通过 PdfLinkAnnotation#SetBorder(PdfAnnotationBorder)
方法而不是 SetBorderStyle
.
这是您的代码片段,稍作修改:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("links.pdf"));
Document d = new Document(pdfDoc);
Link link = new Link("Please no borders", PdfAction.CreateURI("http://www.google.com"));
link.GetLinkAnnotation().SetBorder(new PdfAnnotationBorder(0, 0, 0));
d.Add(new Paragraph(link));
pdfDoc.Close();
您可以在 javadocs 中找到有关此方法调用的更多信息:http://itextsupport.com/apidocs/itext7/latest/com/itextpdf/kernel/pdf/annot/PdfAnnotation.html#getBorder--
一些背景
默认情况下,iText 不会为 link 注释指定任何特定的边框属性。根据 PDF 规范,注释的默认值是有边框的:
ISO32000-1 12.5.2 "Annotation Dictionaries" Table 164 – "Entries common to all annotation dictionaries":
Border - An array specifying the characteristics of the annotation’s border, which shall be drawn as a rounded rectangle. The array consists of three numbers defining the horizontal corner radius, vertical corner radius, and border width, all in default user space units. If the corner radii are 0, the border has square (not rounded) corners; if the border width is 0, no border is drawn.
[...]
Default value: [0 0 1].