使用C#编码的程序更新PDF的注释 [英] Updating annotations of a PDF using a program coded in C#

查看:157
本文介绍了使用C#编码的程序更新PDF的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用iTextSharp提取PDF页面中存在的注释.但是,我无法编辑这些注释.我的要求是,在多个注释中,我可以搜索特定的注释,然后编辑其内容并保存PDF.在PDF的打开处,应显示更新的版本.但是,我尝试使用itextsharp,spire和rasteredge,但没有一个能给出任何有意义的结果.

I am able to extract annotations present in a PDF page using iTextSharp. 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. On the opening of the PDF, the updated version should be displayed. However, I tried using itextsharp, spire and rasteredge but non-of them give any meaningful result.

以下是使用RasterEdge进行的功能.

Below is the function made using 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();
}

运行这些功能时,我没有收到任何错误,但是PDF的批注中也没有任何更改.如果有人可以帮忙,那就太好了.

I do not get any error when I run these functions but as well as no change in the annotations of the PDF. If anybody can help out on this, it would be great.

推荐答案

到目前为止,我仍然找不到更新或编辑实际PDF注释的方法.但是,我正在利用RasterEdge库删除现有注释,并将其替换为新内容.这可以有效地进行,因为它不需要我想象的那么多时间.那就是程序使用这种逻辑也足够快.

As of now I haven't been able to find anyway to update or edit the actual PDF Annotation. However, I am utilizing RasterEdge Library to delete the existing annotation and replace it with new content. 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);
                }
            }
        }
    }

这篇关于使用C#编码的程序更新PDF的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆