将粘滞便笺添加到现有PDF。丢失原始PDF内容

时间:2017-09-10 18:32:44

标签: c#-4.0 annotations itext

我现在正在尝试为现有PDF添加便利贴,但在添加便签之后,原始内容和所有页面都会丢失。我错过了什么?我知道必须有一个遍历所有页面的循环,但是当我使用单页文档时,原始文档的内容也会丢失。 这段代码部分是在其他问题中找到的代码,但我看不出我做错了什么。

private void button4_Click(object sender, EventArgs e)
{
        using (Document doc = new Document())
        {

            PdfDocument pdfDoc = new PdfDocument();
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("D:\\Test paginas 3.pdf", FileMode.OpenOrCreate));
            doc.Open();

            float height = writer.PageSize.Height;                         
            float width = writer.PageSize.Width;                                            iTextSharp.text.Rectangle rect1 = new iTextSharp.text.Rectangle(width - 120,height-120,width - 20, height - 20);
            iTextSharp.text.Rectangle rect2 = new iTextSharp.text.Rectangle(width ,height-150 ,width +150 , height-50);
            String title = "Titel van sticky note";
            String contents = "inhoud van sticky note";

            PdfAnnotation text = PdfAnnotation.CreateText(writer, rect1, title, contents, true, "Comment");
            text.Color = new BaseColor(252, 251, 0);
            text.Name = "text";

            PdfAnnotation popup = PdfAnnotation.CreatePopup(writer, rect2, null, true);

            popup.Put(PdfName.PARENT, text.IndirectReference);

            text.Put(PdfName.POPUP, popup.IndirectReference);

            writer.AddAnnotation(text);
            writer.AddAnnotation(popup);

            doc.Close();
            writer.Close();
       }

  }

0 个答案:

没有答案