1

我正在尝试在现有文档 PDF 中插入 3D 注释。在“PdfAppearance ap..”行中出现错误 - “使用 PdfStamper.getUnderContent() 或 PdfStamper.getOverContent()”。如何更改代码以使其工作?

private void button4_Click(object sender, EventArgs e)
        {
            Document document = new Document();
            PdfReader reader = new PdfReader("model.PDF");
            PdfStamper stamper = new PdfStamper(reader, /* new FileOutputStream("HelloWorldStamped.pdf"));*/
            new FileStream("PartCreated1.pdf", FileMode.Create));
            PdfContentByte newPage;
            int total = reader.NumberOfPages;
            for (int i = 1; i <= total; i++)
            {
                newPage = stamper.GetOverContent(i);
                if (i == 1)
                {
                    PdfWriter writer = newPage.PdfWriter;
                    Stream sd = File.Open(RESOURCE, FileMode.Open);
                    iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(100, 100, 500, 800);
                    PdfStream stream3d = new PdfStream(sd, writer);
                    stream3d.Put(PdfName.TYPE, new PdfName("3D"));
                    stream3d.Put(PdfName.SUBTYPE, new PdfName("U3D"));
                    stream3d.FlateCompress();
                    PdfIndirectObject streamObject = writer.AddToBody(stream3d);
                    stream3d.WriteLength();
                    PdfDictionary dict3D = new PdfDictionary();
                    dict3D.Put(new PdfName(PDF_NAME_XN), new PdfString("DefaultView"));
                    dict3D.Put(new PdfName(PDF_NAME_IN), new PdfString("Unnamed"));
                    dict3D.Put(new PdfName(PDF_NAME_MS), new PdfName(PDF_NAME_U3D));
                    dict3D.Put(new PdfName(PDF_NAME_U3DPATH), new PdfString("DefaultView"));
                    PdfIndirectObject dictObject = writer.AddToBody(dict3D);
                    PdfAnnotation annot = new PdfAnnotation(writer, rect);
                    annot.Put(PdfName.CONTENTS, new PdfString("3D Model"));
                    annot.Put(PdfName.SUBTYPE, new PdfName("3D"));
                    annot.Put(PdfName.TYPE, PdfName.ANNOT);
                    annot.Put(new PdfName("3DD"), streamObject.IndirectReference);
                    annot.Put(new PdfName("3DV"), dictObject.IndirectReference);
                    PdfAppearance ap = stamper.GetOverContent(i).PdfWriter.DirectContent.CreateAppearance(rect.Width, rect.Height);//in this line I got an error "Use PdfStamper.getUnderContent() or PdfStamper.getOverContent()"
                    annot.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap);
                    annot.SetPage();
                    writer.AddAnnotation(annot);
                    sd.Dispose();
                    sd.Close();

                }
            }
            stamper.Close();
        }
4

1 回答 1

1

我自己得到了它:

PdfAppearance ap = newPage.CreateAppearance(rect.Width, rect.Height);
annot.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap);
annot.SetPage();
stamper.AddAnnotation(annot, i);
sd.Dispose();
sd.Close();
于 2012-05-23T04:53:10.247 回答