如何添加粘滞便笺,在光标处插入文本,..使用带有C#的iTextSharp在现有PDF文件中添加注释?

时间:2013-09-05 12:55:53

标签: c# itextsharp

我想使用带有C#的iTextSharp在现有PDF文件中添加注释注释。

请提供示例代码以在现有PDF文件中添加注释。

这里是我的注释的PS脚本:

[/Contents (My Text contents) /Rect [100 600 150 550] /SrcPg 1 /Title (My Title text) /Color [0 0 1] /Subtype /Caret /ANN pdfmark

2 个答案:

答案 0 :(得分:4)

TimetableAnnotations1.java第7章中的iText(夏普)示例TimetableAnnotations1.cs / iText in Action — 2nd Edition显示了如何向现有PDF添加注释。

中心代码是(在C#示例中):

rect = GetPosition(screening);
annotation = PdfAnnotation.CreateText(
    stamper.Writer, rect, movie.MovieTitle,
    string.Format(INFO, movie.Year, movie.Duration),
    false, "Help"
);
annotation.Color = WebColors.GetRGBColor(
    "#" + movie.entry.category.color
);
stamper.AddAnnotation(annotation, page);

其中stamperPdfStamper处理您的PDF文件; movie是一个数据结构,该示例从。

中检索注释的标题,文本和颜色

PdfAnnotation提供了多种其他Create...方法来创建其他类型的注释。

答案 1 :(得分:0)

rect = GetPosition(screening);

有人可以解释为什么这是用的..有没有办法找到当前光标位置(顶部,底部,高度,宽度)

与注释一样,

Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@"C:\Users\Asus\Desktop\Test.pdf", FileMode.OpenOrCreate));
        doc.AddDocListener(writer);
        doc.Open();

        doc.Add(new Annotation("annotation", "The text displayed in the sticky note", 100f, 500f, 200f, 600f));
        doc.Close();

这对我来说很好..