//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2012 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- #include #include #include #include using namespace std; using namespace pdftron; using namespace SDF; using namespace PDF; using namespace Filters; int main(int argc, char *argv[]) { int ret = 0; PDFNet::Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; // Sample 1 - Split a PDF document into multiple pages try { cout << "_______________________________________________" << endl; cout << "Sample 1 - Split a PDF document into multiple pages..." << endl; cout << "Opening the input pdf..." << endl; PDFDoc in_doc((input_path + "newsletter.pdf").c_str()); in_doc.InitSecurityHandler(); int page_num = in_doc.GetPageCount(); for (int i=1; i<=page_num; ++i) { PDFDoc new_doc; char fname[256]; sprintf(fname, "newsletter_split_page_%d.pdf", i); string output_file(output_path + fname); new_doc.InsertPages(0, in_doc, i, i, PDFDoc::e_none); new_doc.Save(output_file, SDFDoc::e_remove_unused, 0); cout << "Done. Result saved in " << fname << endl; } } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } // Sample 2 - Merge several PDF documents into one try { cout << "_______________________________________________" << endl; cout << "Sample 2 - Merge several PDF documents into one..." << endl; PDFDoc new_doc; new_doc.InitSecurityHandler(); int page_num = 15; for (int i=1; i<=page_num; ++i) { char fname[256]; sprintf(fname, "newsletter_split_page_%d.pdf", i); string input_file(output_path + fname); cout << "Opening " << fname << endl; PDFDoc in_doc(input_file); new_doc.InsertPages(i, in_doc, 1, in_doc.GetPageCount(), PDFDoc::e_none); } new_doc.Save(output_path + "newsletter_merge_pages.pdf", SDFDoc::e_remove_unused, 0); cout << "Done. Result saved in newsletter_merge_pages.pdf" << endl; } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } // Sample 3 - Delete every second page try { cout << "_______________________________________________" << endl; cout << "Sample 3 - Delete every second page..." << endl; cout << "Opening the input pdf..." << endl; PDFDoc in_doc((input_path + "newsletter.pdf").c_str()); in_doc.InitSecurityHandler(); int page_num = in_doc.GetPageCount(); while (page_num>=1) { PageIterator itr = in_doc.GetPageIterator(page_num); in_doc.PageRemove(itr); page_num -= 2; } in_doc.Save((output_path + "newsletter_page_remove.pdf").c_str(), 0, 0); cout << "Done. Result saved in newsletter_page_remove.pdf..." << endl; } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } // Sample 4 - Inserts a page from one document at different // locations within another document try { cout << "_______________________________________________" << endl; cout << "Sample 4 - Insert a page at different locations..." << endl; cout << "Opening the input pdf..." << endl; PDFDoc in1_doc((input_path + "newsletter.pdf").c_str()); in1_doc.InitSecurityHandler(); PDFDoc in2_doc((input_path + "fish.pdf").c_str()); in2_doc.InitSecurityHandler(); PageIterator src_page = in2_doc.GetPageIterator(); int page_num = in1_doc.GetPageCount(); for (int i=1; i copy_pages; for (PageIterator itr=in_doc.GetPageIterator(); itr.HasNext(); itr.Next()) { copy_pages.push_back(itr.Current()); } vector imported_pages = new_doc.ImportPages(copy_pages); vector::iterator i; for (i=imported_pages.begin(); i!=imported_pages.end(); ++i) { new_doc.PagePushFront(*i); // Order pages in reverse order. // Use PagePushBack() if you would like to preserve the same order. } new_doc.Save((output_path + "newsletter_import_pages.pdf").c_str(), 0, NULL); cout << "Done. Result saved in newsletter_import_pages.pdf..." << endl << endl << "Note that the output file size is less than half the size" << endl << "of the file produced using individual page copy operations" << endl << "between two documents" << endl; } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } PDFNet::Terminate(); return ret; }