// // Copyright (c) 2001-2012 by PDFTron Systems Inc. All Rights Reserved. // using System; using pdftron; using pdftron.Common; using pdftron.Filters; using pdftron.SDF; using pdftron.PDF; namespace ElementEditTestCS { /// /// The sample code shows how to edit the page display list and how to modify graphics state /// attributes on existing Elements. In particular the sample program strips all images from /// the page and changes text color to blue. /// class ElementEditTest { // Helper function static void ProcessElements(ElementReader reader, ElementWriter writer) { Element element; while ((element = reader.Next()) != null) // Read page contents { switch (element.GetType()) { case Element.Type.e_path: // Process path data... break; case Element.Type.e_image: case Element.Type.e_inline_image: // remove all images by skipping them continue; case Element.Type.e_text: // Process text strings... { // Set all text to blue color. GState gs = element.GetGState(); gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB()); gs.SetFillColor(new ColorPt(0, 0, 1)); break; } case Element.Type.e_form: // Recursively process form XObjects { reader.FormBegin(); ProcessElements(reader, writer); reader.End(); break; } } writer.WriteElement(element); } } /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../../../TestFiles/"; string output_path = "../../../../TestFiles/Output/"; string input_filename = "newsletter.pdf"; string output_filename = "newsletter_edited.pdf"; try { Console.WriteLine("-------------------------------------------------"); // Open the test file Console.WriteLine("Opening the input file..."); PDFDoc doc = new PDFDoc(input_path + input_filename); doc.InitSecurityHandler(); int num_pages = doc.GetPageCount(); ElementWriter writer = new ElementWriter(); ElementReader reader = new ElementReader(); for (int i = 1; i <= num_pages; ++i) { Page page = doc.GetPage(i); reader.Begin(page); writer.Begin(page, ElementWriter.WriteMode.e_replacement, false); ProcessElements(reader, writer); writer.End(); reader.End(); } // Calling Dispose() on ElementReader/Writer/Builder can result in increased performance and lower memory consumption. writer.Dispose(); reader.Dispose(); doc.Save(output_path + output_filename, SDFDoc.SaveOptions.e_remove_unused); doc.Close(); Console.WriteLine("Done. Result saved in {0}...", output_filename); } catch (PDFNetException e) { Console.WriteLine(e.Message); } PDFNet.Terminate(); } } }