我已阅读 Can I tell iText how to clip text to fit in a cell和 http://osdir.com/ml/java.lib.itext.general/2005-01/msg00112.html ,我仍然对如何做我想做的事情感到困惑。我想剪辑文本,但仍然有 anchor 引用工作。将 anchor 放在模板内是不好的(根据第二个链接)。这是我正在使用的代码(我理解为什么它不起作用,只是不知道该怎么做):
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);
}
}
请记住,这显示正确,但当短语 p 是 anchor 时,它是不可单击的。谢谢!
最佳答案
PdfTemplate
类可用于创建Form XObjects。这些是可重复使用的 PDF 内容流。
链接永远不是内容流的一部分。链接是在页面级别定义的注释。您永远无法在 Form XObject 级别定义链接。从 PDF 专家的角度来看,认为您的代码可以工作是一个逻辑错误。
为了使矩形可点击,您需要添加一个方法 addLinkAnnotation()
。您需要将 PdfWriter
和 Rectangle
参数传递给此方法,并执行如下操作:
PdfAnnotation annotation = PdfAnnotation.createLink(
writer, rect, PdfAnnotation.HIGHLIGHT_INVERT, new PdfAction("http://itextpdf.com"));
writer.addAnnotation(annotation);
这将使矩形可点击。
关于java - 如何在 iText (Java) 中垂直剪辑文本 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24330150/