两个问题,
public void addLinkedtoTOC(String src, String dest,String IMG,int linkedPageNumber,int linkDisplayTOCPageNumber) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Rectangle linkLocation = new Rectangle(320, 695, 560, 741);
linkLocation.setBorderColorLeft(BaseColor.WHITE);
linkLocation.setBorderColorRight(BaseColor.RED);
linkLocation.setBorderColorTop(BaseColor.RED);
PdfDestination destination = new PdfDestination(PdfDestination.FIT);
PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(), linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,linkedPageNumber, destination);
stamper.addAnnotation(link, linkDisplayTOCPageNumber);
stamper.close();
reader.close();
}
答案 0 :(得分:1)
你的第一个问题很简单。这是iText - How to stamp image on existing PDF and create an anchor
的副本创建表示链接注释的PdfAnnotation
对象时,默认情况下会定义边框。您可以在注释级别使用setBorder()
方法删除此边框,如AddImageLink示例中所示:
link.setBorder(new PdfBorderArray(0, 0, 0));
之前你的第二个问题也得到了回答。例如,见:
我在AddLinkAnnotation5示例中结合了两者:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
// Here we define the location:
Rectangle linkLocation = new Rectangle(320, 695, 560, 741);
// here we add the actual content at this location:
ColumnText ct = new ColumnText(stamper.getOverContent(1));
ct.setSimpleColumn(linkLocation);
ct.addElement(new Paragraph("This is a link. Click it and you'll be forwarded to another page in this document."));
ct.go();
// now we create the link that will jump to a specific destination:
PdfDestination destination = new PdfDestination(PdfDestination.FIT);
PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
3, destination);
// If you don't want a border, here's where you remove it:
link.setBorder(new PdfBorderArray(0, 0, 0));
// We add the link (that is the clickable area, not the text!)
stamper.addAnnotation(link, 1);
stamper.close();
reader.close();
}
但是,还有另一种方法可以同时添加文本和链接注释。我在回答重复问题时解释了这一点:How to add overlay text with link annotations to existing pdf?
在这个答案中,我参考AddLinkAnnotation2示例,我们使用ColumnText
添加内容,如上所述,但我们引入了可点击的Chunk
:
Chunk chunk = new Chunk("The Best iText Questions on StackOverflow", bold);
chunk.setAnchor("http://developers.itextpdf.com/frequently-asked-developer-questions");
将chunk
对象包裹在Paragraph
内,使用Paragraph
添加ColumnText
并获得无边界链接。