//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- #include #include #include #include #include using namespace pdftron; using namespace std; using namespace SDF; using namespace PDF; //--------------------------------------------------------------------------------------- // 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. //--------------------------------------------------------------------------------------- int main(int argc, char *argv[]) { int ret = 0; PDFNet::Initialize(); PDFNet::SetResourcesPath("../../../resources"); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; try { PDFDoc doc((input_path + "newsletter.pdf").c_str()); doc.InitSecurityHandler(); int num_pages = doc.GetPageCount(); ElementWriter writer; ElementReader reader; Element element; for(int i=1; i<=num_pages; ++i) { PageIterator itr = doc.GetPageIterator(i); Page page = itr.Current(); reader.Begin(page); Page new_page = doc.PageCreate(); PageIterator next_page = itr; next_page.Next(); doc.PageInsert(next_page, new_page ); writer.Begin(new_page); while (element = reader.Next()) // Read page contents { if (element.GetType() == Element::e_text) { // Set all text to blue color. GState gs = element.GetGState(); gs.SetFillColorSpace(ColorSpace::CreateDeviceRGB()); gs.SetFillColor(ColorPt(0, 0, 1)); } else if (element.GetType() == Element::e_image) { // remove all images continue; } writer.WriteElement(element); } writer.End(); reader.End(); new_page.SetMediaBox(page.GetCropBox()); doc.PageRemove(doc.GetPageIterator(i)); } doc.Save((output_path + "newsletter_edited.pdf").c_str(), SDFDoc::e_remove_unused , 0); // doc.Save((output_path + "newsletter_edited.pdf").c_str(), Doc::e_linearized , 0); cout << "Done. Result saved in newsletter_edited.pdf..." << endl; } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } PDFNet::Terminate(); return ret; }