1

I have read both Can I tell iText how to clip text to fit in a cell and http://osdir.com/ml/java.lib.itext.general/2005-01/msg00112.html, and I'm still confused about how to do what I want. I want to clip the text but still have the anchor reference work. Putting an anchor inside a template is no good (as per the second link). Here's the code I was using (I understand why it doesn't work, just not what to do instead):

public static void drawTextClipped(PdfContentByte canvas, Rectangle rect, float clipHeight, Phrase p, int horizontalAlignment)
{
    PdfTemplate tmp = canvas.createTemplate(rect.getWidth(), clipHeight);
    drawColumnText(tmp, new Rectangle(0, clipHeight - rect.getHeight(), rect.getWidth(), clipHeight), p, horizontalAlignment, false);
    canvas.addTemplate(tmp, rect.getLeft(), rect.getTop() - clipHeight);
}

public static float drawColumnText(PdfContentByte parent, Rectangle rect, Phrase p, int horizontalAlignment, boolean simulate)
{
    try
    {
        ColumnText ct = new ColumnText(parent);
        ct.setLeading(0, 1);
        ct.setSimpleColumn(rect);
        ct.setText(p);
        ct.setAlignment(horizontalAlignment);
        ct.go(simulate);
        return ct.getYLine();
    }
    catch (DocumentException de)
    {
        throw new ExceptionConverter(de);
    }
}

Bear in mind that this displays correctly, but when Phrase p is an Anchor, it is unclickable. Thanks!

Community
  • 1
  • 1

1 Answers1

0

The PdfTemplate class can be used to create Form XObjects. These are reusable PDF content streams.

A link is never part of the content stream. A link is an annotation that is defined at the page level. You can never define a link at the level of a Form XObject. For the point of view of a PDF expert, it is a logical error to think that your code would work.

For your rectangle to be clickable, you need to add a method addLinkAnnotation(). You need to pass a PdfWriter and a Rectangle parameter to this method, and do something like this:

PdfAnnotation annotation = PdfAnnotation.createLink(
    writer, rect, PdfAnnotation.HIGHLIGHT_INVERT, new PdfAction("http://itextpdf.com"));
writer.addAnnotation(annotation);

This will make the rectangle clickable.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks. I must be missing something, though, because I'm seeing a line around the link, even if I set no border on the rect and PdfAnnotation.HIGHLIGHT_NONE as the highlight. – user3760601 Jun 20 '14 at 19:45
  • Nevermind. For anyone else who reads this question, the answer is [here](http://stackoverflow.com/questions/14967188/itext-rectangle-cant-remove-border). – user3760601 Jun 20 '14 at 19:58