How to add Sticky Notes, Insert Text at cursor, .. Annotations in existing PDF file using iTextSharp with C#?

邮差的信 提交于 2019-12-02 19:24:05

问题


I want to add Annotations comment in existing PDF file using iTextSharp with C#.

Please give sample code to add Annotations in existing PDF file.

Here PS Script for my Annotation:

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

回答1:


The iText(Sharp) example TimetableAnnotations1.java / TimetableAnnotations1.cs from chapter 7 of iText in Action — 2nd Edition shows how to add annotations to existing PDFs in general.

The central code is (in the C# example):

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);

where stamper is a PdfStamper working on your PDF file; movie is a data structure the example retrieves title, text and color of the annotation from.

PdfAnnotation offers multiple other Create... methods to create other types of annotations.




回答2:


rect = GetPosition(screening);

can someone plz explain why is this is used..is there any way to find the current cursor position (top,bottom,height,width)

as with the annotation,

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();

this works fine to me..



来源:https://stackoverflow.com/questions/18636963/how-to-add-sticky-notes-insert-text-at-cursor-annotations-in-existing-pdf-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!