//
// PDFNet Copyright (c) 2001-2008 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
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
PDFNet.Initialize();
PDFNet.SetResourcesPath("../../../../../resources");
// Relative path to the folder containing test files.
string input_path = "../../../../TestFiles/";
string output_path = "../../../../TestFiles/Output/";
try
{
Console.WriteLine("-------------------------------------------------");
// Open the test file
Console.WriteLine("Opening the input file...");
PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf");
doc.InitSecurityHandler();
int num_pages = doc.GetPageCount();
ElementWriter writer = new ElementWriter();
ElementReader reader = new ElementReader();
Element element;
for (int i = 1; i <= num_pages; ++i)
{
Page page = doc.GetPage(1);
reader.Begin(page);
Page new_page = doc.PageCreate();
doc.PagePushBack(new_page);
writer.Begin(new_page);
while ((element = reader.Next()) != null) // Read page contents
{
if (element.GetType() == Element.Type.e_text)
{
// Set all text to blue color.
GState gs = element.GetGState();
gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
gs.SetFillColor(new ColorPt(0, 0, 1));
}
else if (element.GetType() == Element.Type.e_image)
{
// remove all images
continue;
}
writer.WriteElement(element);
}
writer.End();
reader.End();
new_page.SetMediaBox(page.GetCropBox());
doc.PageRemove(doc.GetPageIterator(1)); // Remove the old page
}
doc.Save(output_path + "newsletter_edited.pdf", SDFDoc.SaveOptions.e_remove_unused);
doc.Close();
Console.WriteLine("Done. Result saved in newsletter_edited.pdf...");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
PDFNet.Terminate();
}
}
}