简体   繁体   English

使用C#编码的程序更新PDF的注释

[英]Updating annotations of a PDF using a program coded in C#

I am able to extract annotations present in a PDF page using iTextSharp. 我可以使用iTextSharp提取PDF页面中存在的注释。 However, I am not able to edit these annotations. 但是,我无法编辑这些注释。 My requirement is that among multiple annotations I can search a specific annotation and then edit its content and save the PDF. 我的要求是,在多个注释中,我可以搜索特定的注释,然后编辑其内容并保存PDF。 On the opening of the PDF, the updated version should be displayed. 在打开PDF时,应显示更新的版本。 However, I tried using itextsharp , spire and rasteredge but non-of them give any meaningful result. 但是,我尝试使用itextsharp ,spire和itextsharp ,但是它们都不给任何有意义的结果。

Below is the function made using RasterEdge. 以下是使用RasterEdge进行的功能。

static void RasterEdit(string PDF)
{
    PDFDocument doc = new PDFDocument(PDF);
    PDFPage page = (PDFPage)doc.GetPage(0);
    List<IPDFAnnot> annots = PDFAnnotHandler.GetAllAnnotations(doc);
    PDFAnnotHandler.DeleteAnnotation(doc, annots);
    doc.Save(PDF);
    int i = 0;
    Console.Write(" Location where you want to edit tag? ");
    string location = Console.ReadLine();
    location = location.ToUpper();

    Console.Write("New Employee Name: ");
    string emp = Console.ReadLine();
    foreach (IPDFAnnot annot in annots)
    {
        i++;
        if (annot is PDFAnnotStickyNote)
        {
            string []txt;
            txt=annot.Content.Split(':');

            /*

             */
            if (txt[0]==location)
            {
                annot.Content = location + ":" + emp; 
            }
        }
       // Console.WriteLine(i+" "+annot.Content);
    }
    PDFAnnotHandler.AddAnnotation(PDF, annots);
}

Spire: 尖塔:

static void spireEditTag(string PDF)
{

    //Initialize an instance of PdfDocument class and load the file 
    Spire.Pdf.PdfDocument pdf = new Spire.Pdf.PdfDocument();
    pdf.LoadFromFile(PDF);

    string newfile = System.IO.Path.GetDirectoryName(PDF) + @"\NewPDF.pdf";
    //pdf.SaveToFile(newfile);
    //System.Diagnostics.Process.Start(newfile);

    //PdfPageBase page = pdf.Pages[0];

    //Get the first annotation in the first page 

    Spire.Pdf.Annotations.PdfAnnotation annotation = pdf.Pages[0].AnnotationsWidget[0];

    Console.Write("New Employee: ");
    string emp = Console.ReadLine();
    //Add the modified text and set the formatting 
    annotation.Text = emp;
    //annotation.Color = new PdfRGBColor(Color.Red);
    //annotation.Border = new PdfAnnotationBorder(1f);

    pdf.Pages[0].AnnotationsWidget[0].Text = emp;

    //pdfDocumentViewer1.SaveToFile(fileName);

    //Save the file and view 
    pdf.SaveToFile(PDF,FileFormat.PDF);

    pdf.Close();
    //System.Diagnostics.Process.Start(PDF);

} 

iTextSharp: iTextSharp:

static void ReadTagPDF(string PDF)
{
    PdfReader reader = new PdfReader(PDF);
    for(int i=1; i<=reader.NumberofPages; i++)
    {
        PdfArray array = reader.GetPageN(i).GetAsArray(PdfName.ANNOTS);
        if(array==null) continue;
        for( int j=0; j<array.Size; j++)
        {
            PdfDictionary annot = array.GetAsDict(j);
            PdfString text = annot.GetAsString(PdfName.CONTENTS);

            StringBuilder sb = new StringBuilder();

            if(Convert.ToString(text).Trim()=="1F-MTR-7")
            {
                annot.Put(PdfName.CONTENTS, new PdfString("Test");
                reader.AddPdfObject(annot);
            }
        }
    }
      reader.Close();
}

I do not get any error when I run these functions but as well as no change in the annotations of the PDF. 运行这些功能时没有出现任何错误,但是PDF的批注中也没有任何更改。 If anybody can help out on this, it would be great. 如果有人可以对此提供帮助,那就太好了。

As of now I haven't been able to find anyway to update or edit the actual PDF Annotation. 到目前为止,我仍然找不到更新或编辑实际PDF注释的方法。 However, I am utilizing RasterEdge Library to delete the existing annotation and replace it with new content. 但是,我正在利用RasterEdge库删除现有注释,并将其替换为新内容。 This works efficiently as it's not taking as much time as I thought. 这有效,因为它不需要我想象的那么多时间。 That is the program is quick enough with this logic too. 这就是程序使用此逻辑也足够快。

 using RasterEdge.XDoc.PDF;
 static void EditTag(string PDF) 
    {       

        PDFDocument doc = new PDFDocument(PDF);
        List<IPDFAnnot> annots = PDFAnnotHandler.GetAllAnnotations(doc);
        foreach (IPDFAnnot annot in annots)
        {
            if (annot is PDFAnnotTextBox)
            {
                PDFAnnotTextBox obj = (PDFAnnotTextBox)annot;
                if (obj.Content.Trim() == "Content to be searched and replaced")
                {
                    //  get the 1st page
                    PDFPage page = (PDFPage)doc.GetPage(0);

                    //  create the annotation
                    PDFAnnotTextBox test = new PDFAnnotTextBox();

                    //Copy Properties of Original Annotation
                    test.Boundary = obj.Boundary;
                    test.LineColor = obj.LineColor;
                    test.LineStyle = obj.LineStyle;
                    test.LineWidth = obj.LineWidth;
                    test.Opacity = obj.Opacity;
                    test.TextColor = obj.TextColor;
                    test.TextFont = obj.TextFont;
                    test.FillColor = Color.Yellow;
                    test.TextFont = new System.Drawing.Font(test.TextFont.FontFamily, 6);

                    //Changing the Required Subject and Content of Annotation
                    test.Content = obj.Content + "appending content";

                    Console.WriteLine("Deleting Existing Annotation.");
                    PDFAnnotHandler.DeleteAnnotation(doc, obj);
                    Console.WriteLine("Deleted.");
                    Console.WriteLine("Adding new Annotation.");
                    PDFAnnotHandler.AddAnnotation(page, test);
                    Console.WriteLine("New Annotation added.");

                    //  save the PDF
                    doc.Save(PDF);
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM