// 0 <= pageNo <numPage CImage* getPage(int w, int h, int pageNo) { if(!cacheValid(w,h,pageNo)) { // poppler uses 1 indexing for pageNo. double DPI_W=calcDPI_width(w, pageNo); double DPI_H=calcDPI_height(h, pageNo); double DPI=MIN(DPI_W, DPI_H); delete _textCache[pageNo]; _textCache[pageNo]=new intmatrixn(); _textCacheState=pageNo; //_pdfDoc->getPageRotate(pageNo+1) _pdfDoc->displayPage(_outputDev, pageNo+1, DPI, DPI, 0, gFalse, gTrue, gFalse); #ifdef USE_NULLOUTPUTDEV _nullOutputDev->setInfo(_pdfDoc->getPageRotate(pageNo+1), _pdfDoc->getCatalog()->getPage(pageNo+1)->getCropBox()->x1, _pdfDoc->getCatalog()->getPage(pageNo+1)->getCropBox()->y1, _pdfDoc->getPageCropWidth(pageNo+1), _pdfDoc->getPageCropHeight(pageNo+1), _outputDev->getBitmap()->getWidth(), _outputDev->getBitmap()->getHeight()); _pdfDoc->displayPage(_nullOutputDev, pageNo+1, DPI, DPI, 0, gFalse, gTrue, gFalse); #endif _textCacheState=-1; /*//_pdfDoc->displayPageSlice(_outputDev, pageNo+1, DPI, DPI, 0, gFalse, gTrue, gFalse, 20,20, 400,400); if(zoom2<zoom1) { if(h==_outputDev->getBitmap()->getHeight()) bOkay=true; else { vDPI=72.0*h/_pdfDoc->getPageCropHeight(pageNo+1); } } else { if(w==_outputDev->getBitmap()->getWidth()) bOkay=true; else { double tt=72*w/_pdfDoc->getPageCropHeight(pageNo+1); } } } while(!bOkay);*/ SplashBitmap *temp=_outputDev->takeBitmap(); CImage* ptr=new CImage(); ptr->SetData(temp->getWidth(), temp->getHeight(), temp->getDataPtr(), temp->getRowSize()); delete _bmpCache[pageNo]; _bmpCache[pageNo]=ptr; delete temp; } return _bmpCache[pageNo]; }
gboolean import_pdf(const gchar *filename, DiagramData *dia, DiaContext *ctx, void* user_data) { PDFDoc *doc; GooString *fileName = new GooString(filename); // no passwords yet GooString *ownerPW = NULL; GooString *userPW = NULL; gboolean ret = FALSE; // without this we will get strange crashes (at least with /O2 build) globalParams = new GlobalParams(); doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW); if (!doc->isOk()) { dia_context_add_message (ctx, _("PDF document not OK.\n%s"), dia_context_get_filename (ctx)); } else { DiaOutputDev *diaOut = new DiaOutputDev(dia, doc->getNumPages()); for (int pg = 1; pg <= doc->getNumPages(); ++pg) { Page *page = doc->getPage (pg); if (!page || !page->isOk()) continue; doc->displayPage(diaOut, pg, 72.0, 72.0, /* DPI, scaling elsewhere */ 0, /* rotate */ gTrue, /* useMediaBox */ gTrue, /* Crop */ gFalse /* printing */ ); } delete diaOut; ret = TRUE; } delete doc; delete globalParams; delete fileName; return ret; }
bool PdfPlug::convert(QString fn) { bool firstPg = true; int currentLayer = m_Doc->activeLayer(); int baseLayer = m_Doc->activeLayer(); importedColors.clear(); if(progressDialog) { progressDialog->setOverallProgress(2); progressDialog->setLabel("GI", tr("Generating Items")); qApp->processEvents(); } QFile f(fn); oldDocItemCount = m_Doc->Items->count(); if (progressDialog) { progressDialog->setBusyIndicator("GI"); qApp->processEvents(); } globalParams = new GlobalParams(); if (globalParams) { GooString *fname = new GooString(QFile::encodeName(fn).data()); globalParams->setErrQuiet(gTrue); GBool hasOcg = gFalse; QList<OptionalContentGroup*> ocgGroups; // globalParams->setPrintCommands(gTrue); PDFDoc *pdfDoc = new PDFDoc(fname, 0, 0, 0); if (pdfDoc) { if (pdfDoc->isOk()) { double hDPI = 72.0; double vDPI = 72.0; int firstPage = 1; int lastPage = pdfDoc->getNumPages(); SlaOutputDev *dev = new SlaOutputDev(m_Doc, &Elements, &importedColors, importerFlags); if (dev->isOk()) { OCGs* ocg = pdfDoc->getOptContentConfig(); if (ocg) { hasOcg = ocg->hasOCGs(); if (hasOcg) { QStringList ocgNames; Array *order = ocg->getOrderArray(); if (order) { for (int i = 0; i < order->getLength (); ++i) { Object orderItem; order->get(i, &orderItem); if (orderItem.isDict()) { Object ref; order->getNF(i, &ref); if (ref.isRef()) { OptionalContentGroup *oc = ocg->findOcgByRef(ref.getRef()); QString ocgName = UnicodeParsedString(oc->getName()); if (!ocgNames.contains(ocgName)) { ocgGroups.prepend(oc); ocgNames.append(ocgName); } } ref.free(); } else { GooList *ocgs; int i; ocgs = ocg->getOCGs (); for (i = 0; i < ocgs->getLength (); ++i) { OptionalContentGroup *oc = (OptionalContentGroup *)ocgs->get(i); QString ocgName = UnicodeParsedString(oc->getName()); if (!ocgNames.contains(ocgName)) { ocgGroups.prepend(oc); ocgNames.append(ocgName); } } } } } else { GooList *ocgs; int i; ocgs = ocg->getOCGs (); for (i = 0; i < ocgs->getLength (); ++i) { OptionalContentGroup *oc = (OptionalContentGroup *)ocgs->get(i); QString ocgName = UnicodeParsedString(oc->getName()); if (!ocgNames.contains(ocgName)) { ocgGroups.prepend(oc); ocgNames.append(ocgName); } } } } } GBool useMediaBox = gTrue; GBool crop = gFalse; GBool printing = gFalse; dev->startDoc(pdfDoc, pdfDoc->getXRef(), pdfDoc->getCatalog()); int rotate = pdfDoc->getPageRotate(firstPage); if (importerFlags & LoadSavePlugin::lfCreateDoc) { // POPPLER_VERSION appeared in 0.19.0 first #ifdef POPPLER_VERSION if (hasOcg) { QString actL = m_Doc->activeLayerName(); for (int a = 0; a < ocgGroups.count(); a++) { OptionalContentGroup *oc = ocgGroups[a]; if (actL != UnicodeParsedString(oc->getName())) currentLayer = m_Doc->addLayer(UnicodeParsedString(oc->getName()), false); else currentLayer = m_Doc->layerIDFromName(UnicodeParsedString(oc->getName())); // POPPLER_VERSION appeared in 0.19.0 first #ifdef POPPLER_VERSION if ((oc->getViewState() == OptionalContentGroup::ocUsageOn) || (oc->getViewState() == OptionalContentGroup::ocUsageUnset)) m_Doc->setLayerVisible(currentLayer, true); else m_Doc->setLayerVisible(currentLayer, false); if ((oc->getPrintState() == OptionalContentGroup::ocUsageOn) || (oc->getPrintState() == OptionalContentGroup::ocUsageUnset)) m_Doc->setLayerPrintable(currentLayer, true); else m_Doc->setLayerPrintable(currentLayer, false); #else if (oc->getState() == OptionalContentGroup::On) { m_Doc->setLayerVisible(currentLayer, true); m_Doc->setLayerPrintable(currentLayer, true); } else { m_Doc->setLayerVisible(currentLayer, false); m_Doc->setLayerPrintable(currentLayer, false); } #endif oc->setState(OptionalContentGroup::Off); } dev->layersSetByOCG = true; } #endif Object info; pdfDoc->getDocInfo(&info); if (info.isDict()) { Object obj; GooString *s1; Dict *infoDict = info.getDict(); if (infoDict->lookup((char*)"Title", &obj )->isString()) { s1 = obj.getString(); m_Doc->documentInfo().setTitle(UnicodeParsedString(obj.getString())); obj.free(); } if (infoDict->lookup((char*)"Author", &obj )->isString()) { s1 = obj.getString(); m_Doc->documentInfo().setAuthor(UnicodeParsedString(obj.getString())); obj.free(); } if (infoDict->lookup((char*)"Subject", &obj )->isString()) { s1 = obj.getString(); m_Doc->documentInfo().setSubject(UnicodeParsedString(obj.getString())); obj.free(); } if (infoDict->lookup((char*)"Keywords", &obj )->isString()) { s1 = obj.getString(); m_Doc->documentInfo().setKeywords(UnicodeParsedString(obj.getString())); obj.free(); } } info.free(); for (int pp = 0; pp < lastPage; pp++) { m_Doc->setActiveLayer(baseLayer); if (firstPg) firstPg = false; else m_Doc->addPage(pp); m_Doc->currentPage()->setInitialHeight(pdfDoc->getPageMediaHeight(pp + 1)); m_Doc->currentPage()->setInitialWidth(pdfDoc->getPageMediaWidth(pp + 1)); m_Doc->currentPage()->setHeight(pdfDoc->getPageMediaHeight(pp + 1)); m_Doc->currentPage()->setWidth(pdfDoc->getPageMediaWidth(pp + 1)); m_Doc->currentPage()->MPageNam = CommonStrings::trMasterPageNormal; m_Doc->currentPage()->m_pageSize = "Custom"; m_Doc->setPageSize("Custom"); m_Doc->reformPages(true); if (hasOcg) { for (int a = 0; a < ocgGroups.count(); a++) { OptionalContentGroup *oc = ocgGroups[a]; // m_Doc->setActiveLayer(UnicodeParsedString(oc->getName())); // currentLayer = m_Doc->activeLayer(); oc->setState(OptionalContentGroup::On); // pdfDoc->displayPage(dev, pp + 1, hDPI, vDPI, rotate, useMediaBox, crop, printing); // oc->setState(OptionalContentGroup::Off); } pdfDoc->displayPage(dev, pp + 1, hDPI, vDPI, rotate, useMediaBox, crop, printing); } else pdfDoc->displayPage(dev, pp + 1, hDPI, vDPI, rotate, useMediaBox, crop, printing); } } else { if (hasOcg) { for (int a = 0; a < ocgGroups.count(); a++) { ocgGroups[a]->setState(OptionalContentGroup::On); } } pdfDoc->displayPage(dev, firstPage, hDPI, vDPI, rotate, useMediaBox, crop, printing); } } delete dev; } } delete pdfDoc; } delete globalParams; globalParams = 0; // qDebug() << "converting finished"; // qDebug() << "Imported" << Elements.count() << "Elements"; if (Elements.count() == 0) { if (importedColors.count() != 0) { for (int cd = 0; cd < importedColors.count(); cd++) { m_Doc->PageColors.remove(importedColors[cd]); } } } if (progressDialog) progressDialog->close(); return true; }
int main(int argc, char *argv[]) { PDFDoc *doc; GString *fileName; GString *ownerPW, *userPW; SplashColor paperColor; SplashOutputDev *splashOut; GBool ok; int exitCode; int pg; exitCode = 99; // parse args ok = parseArgs(argDesc, &argc, argv); if (!ok || argc != 2 || printVersion || printHelp) { fprintf(stderr, "pdf2jpeg version %s\n", xpdfVersion); fprintf(stderr, "%s\n", xpdfCopyright); if (!printVersion) { printUsage("pdf2jpeg", "<PDF-file> -o <jpegfile>", argDesc); } goto err0; } fileName = new GString(argv[1]); // read config file globalParams = new GlobalParams(cfgFileName); globalParams->setupBaseFonts(NULL); // open PDF file if (ownerPassword[0]) { ownerPW = new GString(ownerPassword); } else { ownerPW = NULL; } if (userPassword[0]) { userPW = new GString(userPassword); } else { userPW = NULL; } doc = new PDFDoc(fileName, ownerPW, userPW); if (userPW) { delete userPW; } if (ownerPW) { delete ownerPW; } if (!doc->isOk()) { exitCode = 1; goto err1; } paperColor[0] = paperColor[1] = paperColor[2] = 0xff; splashOut = new SplashOutputDev(splashModeRGB8, 1, gFalse, paperColor); splashOut->startDoc(doc->getXRef()); if(page>=1 && page<=doc->getNumPages()) { double r = resolution; if(width) { int old_width = doc->getPageCropWidth(page); r = 72.0*width/old_width; } doc->displayPage(splashOut, page, r, r, 0, gFalse, gTrue, gFalse); SplashBitmap*bitmap = splashOut->getBitmap(); if(bitmap) { Guchar*rgb = bitmap->getDataPtr(); int width = bitmap->getWidth(); int height = bitmap->getHeight(); jpeg_save(rgb, width, height, quality, output); } } delete splashOut; exitCode = 0; // clean up err1: delete doc; delete globalParams; err0: // check for memory leaks Object::memCheck(stderr); gMemReport(stderr); return exitCode; }
int extract_images_from_pdf(char* filename, char* target, char* owner_password, char* user_password, char* range, char* format, int jpg_quality, GBool dump_jpg, GBool tiff_jpg) { if (user_cancelled) return gpret_user_cancelled; // load config xpdf_rc xrc; // open file xpdf_doc xdoc(filename, owner_password, user_password); PDFDoc* doc = xdoc.get_doc(); if (!doc->isOk()) return doc->getErrorCode() == errEncrypted ? gpret_pdf_encrypted : gpret_cant_open_pdf; // check for copy permission // if (!doc->okToCopy()) // return gpret_dont_allow_copy; // get page range page_range range_list(range); if (*range == '\0') { range_list.add_item(range_item(1, doc->getNumPages())); } if (user_cancelled) return gpret_user_cancelled; // write image files fi_loader fi; int progress = 0; image_extractor img_out(target, dump_jpg, format, jpg_quality, tiff_jpg); for (int i = 0; i < range_list.item_count(); i++) { range_item& item = range_list.get_item(i); for (int pg = item.first; pg <= min(item.last, doc->getNumPages()); pg++) { if (user_cancelled) return gpret_user_cancelled; doc->displayPage(&img_out, pg, 72, 72, 0, gFalse, gTrue, gFalse); printf("progress: %d\n", ++progress * 100 / range_list.page_count()); } } printf("image count: %d\n", img_out.get_image_number()); return gpret_success; }