//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- #include #include #include #include //----------------------------------------------------------------------------------- // This sample illustrates one approach to PDF image extraction // using PDFNet. // // Note: Besides direct image export, you can also convert PDF images // to GDI+ Bitmap, or extract uncompressed/compressed image data directly // using element.GetImageData() (e.g. as illustrated in ElementReaderAdv // sample project). //----------------------------------------------------------------------------------- #include using namespace std; using namespace pdftron; using namespace Common; using namespace SDF; using namespace PDF; // Relative paths to folders containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; int image_counter = 0; void ImageExtract(ElementReader& reader) { Element element; while ((element = reader.Next()) != NULL) { switch (element.GetType()) { case Element::e_image: case Element::e_inline_image: { cout << "\n--> Image: " << ++image_counter; cout << "\n Width: " << element.GetImageWidth(); cout << "\n Height: " << element.GetImageHeight(); cout << "\n BPC: " << element.GetBitsPerComponent(); Common::Matrix2D ctm = element.GetCTM(); double x2=1, y2=1; ctm.Mult(x2, y2); cout << "\n Coords: x1=" << ctm.m_h << ", y1=" << ctm.m_v << ", x2=" << x2 << ", y2=" << y2; if (element.GetType() == Element::e_image) { Image image(element.GetXObject()); char fname[256]; sprintf(fname, "image_extract1_%d", image_counter); string path(output_path + fname); image.Export(path.c_str()); //string path(output_path + fname + ".tif"); //image.ExportAsTiff(path.c_str()); //string path(output_path + fname + ".png"); //image.ExportAsPng(path.c_str()); } } break; case Element::e_form: // Process form XObjects reader.FormBegin(); ImageExtract(reader); reader.End(); break; } } } int main(int argc, char *argv[]) { int ret = 0; // Initialize PDFNet PDFNet::Initialize(); PDFNet::SetResourcesPath("../../../resources"); // Example 1: // Extract images by traversing the display list for // every page. With this approach it is possible to obtain // image positioning information and DPI. try { PDFDoc doc((input_path + "newsletter.pdf").c_str()); doc.InitSecurityHandler(); ElementReader reader; // Read every page for (PageIterator itr=doc.GetPageIterator(); itr.HasNext(); itr.Next()) { reader.Begin(itr.Current()); ImageExtract(reader); reader.End(); } cout << "Done..." << endl; } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } cout << "----------------------------------------------------------------" << endl; // Example 2: // Extract images by scanning the low-level document. try { PDFDoc doc((input_path + "newsletter.pdf").c_str()); doc.InitSecurityHandler(); image_counter = 0; SDFDoc& cos_doc=doc.GetSDFDoc(); int num_objs = cos_doc.XRefSize(); for(int i=1; i