1

Two questions,

  1. How to hide or set color white to Rectangle box border
  2. How to add text to Rectangle

    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();
}
Sanka
  • 1,294
  • 1
  • 11
  • 20
  • API reference http://api.itextpdf.com/itext/com/itextpdf/text/Rectangle.html – Sanka Jan 02 '16 at 16:21
  • Borders defined for `Rectangle` are rendered when you add a rectangle to the actual content. Annotations (such as a link annotation) aren't actual content. The `Rectangle` class is used for annotations to define the location, *not* to define the border. Borders for an annotation are defined at the level of the annotation. – Bruno Lowagie Jan 02 '16 at 16:36
  • You question is very similar to [How to add overlay text with link annotations to existing pdf?](http://stackoverflow.com/questions/28554413/how-to-add-overlay-text-with-link-annotations-to-existing-pdf) – Bruno Lowagie Jan 02 '16 at 16:43

1 Answers1

1

Your first question is easy. It's a duplicate of iText - How to stamp image on existing PDF and create an anchor

When you create a PdfAnnotation object that represents a link annotation, a border is defined by default. You can remove this border using the setBorder() method at the level of the annotation as is done in the AddImageLink example:

link.setBorder(new PdfBorderArray(0, 0, 0));

Your second question has also been answered before. See for instance:

I have combined both in the AddLinkAnnotation5 example:

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();
}

However, there's also another way to add the text and the link annotation at the same time. That's explained in my answer to the duplicate question: How to add overlay text with link annotations to existing pdf? In this answer, I refer to the AddLinkAnnotation2 example, where we add the content using ColumnText as described above, but we introduce a clickable Chunk:

Chunk chunk = new Chunk("The Best iText Questions on StackOverflow", bold);
chunk.setAnchor("http://developers.itextpdf.com/frequently-asked-developer-questions");

Wrap the chunk object inside a Paragraph, add the Paragraph using ColumnText and you have your borderless link.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165