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!