使用哪种注释类型
我知道 iTextSharp 有一个专门用于绘制矩形的函数,但我尝试仅使用一种方法来绘制我需要的各种标记线,而且 CeateInk 似乎应该能够做到这一点。
请注意,iText 不仅针对不同的表单具有单独的功能,这些不同的功能还创建不同类型的 PDF 注释。
特别是墨迹注释-- 您希望将其用于所有表单 -- 指定为
An 墨迹注释(PDF 1.3) 代表由一条或多条不相交路径组成的手绘“涂鸦”。
(第 12.5.6.13 节 - 墨水注释 - ISO 32000-1)
As a 徒手“涂鸦”通常不被认为是一系列直线和尖角,而是更柔和和圆润;因此,PDF 查看器自然会显示由矩形角坐标给出的墨迹注释用曲线而不是角.
当然,您可以使用外观流来提供外观的可视化,但这将是对此 PDF 功能的一个小误用。
相反,我建议您使用不同类型的注释绘制您需要的各种标记线:折线注释。它们被指定为:
多边形注释 (PDF 1.5) 在页面上显示闭合多边形。这样的多边形可以具有由直线连接的任意数量的顶点。折线注释 (PDF 1.5) 与多边形类似,但第一个和最后一个顶点不是隐式连接的。
(第 12.5.6.9 节 - 多边形和折线注释 - ISO 32000-1)
iTextSharp)也提供了这种注释的方法:
/**
* Creates a polygon or -line annotation
* @param writer the PdfWriter
* @param rect the annotation position
* @param contents the textual content of the annotation
* @param polygon if true, the we're creating a polygon annotation, if false, a polyline
* @param vertices an array with the vertices of the polygon or -line
* @since 5.0.2
*/
public static PdfAnnotation CreatePolygonPolyline(
PdfWriter writer, Rectangle rect, String contents, bool polygon, PdfArray vertices)
您最终可能仍然需要创建注释,因为并非所有 PDF 查看器(特别是所谓的“预览器”)都会生成外观,而是依赖它们预显示的 PDF 中提供的外观...
Examples
没有自己的外表
using (PdfReader pdfReader = new PdfReader(inputPath))
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
{
PdfArray vertices = new PdfArray(new int[]{ 100, 100, 300, 300, 100, 300, 300, 100 });
PdfAnnotation polyLine = PdfAnnotation.CreatePolygonPolyline(pdfStamper.Writer, pdfReader.GetPageSize(1),
"", false, vertices);
polyLine.Color = BaseColor.GREEN;
pdfStamper.AddAnnotation(polyLine, 1);
}
添加这个:
![Screenshot](https://i.stack.imgur.com/RV3g3.png)
在 PDF 查看器中支持注释,包括根据规范生成外观。
有着自己的外貌
using (PdfReader pdfReader = new PdfReader(inputPath))
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
{
Rectangle pageSize = pdfReader.GetPageSize(1);
PdfArray vertices = new PdfArray(new int[] { 100, 100, 300, 300, 100, 300, 300, 100 });
PdfAnnotation polyLine = PdfAnnotation.CreatePolygonPolyline(pdfStamper.Writer, pageSize,
"", false, vertices);
polyLine.Color = BaseColor.GREEN;
PdfAppearance appearance = PdfAppearance.CreateAppearance(pdfStamper.Writer, pageSize.Width, pageSize.Height);
appearance.SetColorStroke(BaseColor.RED);
appearance.MoveTo(100, 100);
appearance.LineTo(300, 300);
appearance.LineTo(100, 300);
appearance.LineTo(300, 100);
appearance.Stroke();
polyLine.SetAppearance(PdfName.N, appearance);
pdfStamper.AddAnnotation(polyLine, 1);
}
添加这个
![Screenshot](https://i.stack.imgur.com/iQBOL.png)
在支持注释的 PDF 查看器中,根据规范带来注释。
(我在外观中明确使用了不同的颜色,以确保 PDF 查看器显示我的外观并且不会自行创建外观。)