1

我基本上是在尝试 PDFNet 并调整 PDFNet 的样本之一。这是代码:

 PDFNet.Initialize();

            // Relative path to the folder containing test files.
            string input_path = "../../../../TestFiles/";
            string output_path = "../../../../TestFiles/Output/";

            PDFDoc doc = new PDFDoc(input_path + "form1.pdf");
            for (int index = 1; index <= doc.GetPageCount(); index++)
            {
                Page page = doc.GetPage(index);
                ElementBuilder eb = new ElementBuilder();       // ElementBuilder is used to build new Element objects
                eb.Reset();         // Reset GState to default
                ElementWriter writer = new ElementWriter(); // ElementWriter is used to write Elements to the page  
                writer.Begin(page);

                // Begin writing a block of text
                string data = "Page " + index;
                Element element = eb.CreateTextRun(data, Font.Create(doc, Font.StandardType1Font.e_times_roman, true), 100.0);
                element.SetTextMatrix(10, 0, 0, 10, 100, 100);
                GState gstate = element.GetGState();
                gstate.SetTextRenderMode(GState.TextRenderingMode.e_stroke_text);
                gstate.SetStrokeColorSpace(pdftron.PDF.ColorSpace.CreateDeviceRGB());
                gstate.SetStrokeColor(new pdftron.PDF.ColorPt(1, 0, 0));

                writer.WriteElement(element);
                writer.End();
                writer.Dispose();  // save changes to the current page
            }

            doc.Save(output_path + "element_builder.pdf", SDFDoc.SaveOptions.e_linearized);
            doc.Close();

但问题是,没有文本被添加到element_builder.pdf. 它只是按原样复制,看起来与form1.pdf. 有人可以帮帮我吗?

这是 PDFNet http://www.pdftron.com/pdfnet/downloads.html的链接

我正在为 .Net 4.0 使用 64 位版本

4

1 回答 1

2

您似乎缺少调用 eb.CreateTextBegin() 和 eb.CreateTextEnd()。你应该尝试这样的事情:

        PDFNet.Initialize();

        // Relative path to the folder containing test files.
        string input_path = "../../../../TestFiles/";
        string output_path = "../../../../TestFiles/Output/";

        PDFDoc doc = new PDFDoc(input_path + "form1.pdf");
        ElementWriter writer = new ElementWriter();
        ElementBuilder eb = new ElementBuilder();
        for (int index = 1; index <= doc.GetPageCount(); index++)
        {
            Page page = doc.GetPage(index); 
            writer.Begin(page);
            eb.Reset();

            // Begin writing a block of text
            string data = "Page " + index;
            Element element = eb.CreateTextBegin(Font.Create(doc, Font.StandardType1Font.e_times_roman, true), 10.0);
            writer.WriteElement(element);
            eb.CreateTextRun(data);
            element.SetTextMatrix(10, 0, 0, 10, 100, 100);
            GState gstate = element.GetGState();
            gstate.SetTextRenderMode(GState.TextRenderingMode.e_fill_text);
            gstate.SetStrokeColorSpace(pdftron.PDF.ColorSpace.CreateDeviceRGB());
            gstate.SetStrokeColor(new pdftron.PDF.ColorPt(1, 0, 0));

            writer.WriteElement(element);
            writer.WriteElement(eb.CreateTextEnd());  
            writer.End();

        }
        writer.Dispose();
        eb.Dispose();
        doc.Save(output_path + "element_builder.pdf", SDFDoc.SaveOptions.e_linearized);
        doc.Close();

顺便提一句。将文本添加到现有页面的一种可能更简单的方法可能是使用“pdftron.PDF.Stamper”,如Stamper sample中所示。

于 2012-07-31T20:28:42.830 回答