//TODO think about storing the documents instead of the filenames in a list //Would avoid creation of the documents for each search but probably take up more memory bool MainWindow::searchPDFPostResults(const QFileInfo &fi, QString searchString) { bool nHitsExceeded = false; Poppler::Document *document = Poppler::Document::load(fi.absoluteFilePath()); if (!document || document->isLocked()) { //Oops, document empty => error message QMessageBox mbox; mbox.setText(tr("Could not read the file \n'") + fi.fileName() + tr("\ntherefore, possible matches might be missing")); mbox.exec(); delete document; return false; } //Get document title QString title = document->info("Title"); //If no title specified => use filename instead if(title == "") title = fi.fileName(); QString shortTitle = QString(title); if (shortTitle.length() > 16) { shortTitle = "..." + shortTitle.right(16); } ui->statusBar->showMessage("Durchsuche PDF: " + shortTitle); qDebug() << "Searching pdf: " << title; //Search the document for the searchString int hitCounter = 0; for (int page = 0; page < document->numPages(); page++) { //Parameters 1,2,3,4 of the search function //will contain the rectangle coordinates of where the text was found. //Since we are not interested in these coordinates, we just pass a single variable double coordinate = 0.0; if (document->page(page)->search(searchString, coordinate,coordinate,coordinate,coordinate, Poppler::Page::NextResult, Poppler::Page::CaseInsensitive)) { hitCounter++; ui->statusBar->showMessage(tr("Searching in ") + shortTitle + " - hit on page: " + QString::number(page)); //Yay, we found the search String on this page //=> Add this page to the result list addDocNPageToResultList(title, fi, page); // TODO make this limit a variable if(hitCounter >= maxHits) { QMessageBox mbox; mbox.setText(tr("Search has already reached the maximum of ") + QString::number(maxHits) + tr(" hits please choose a more specific criterion")); mbox.exec(); nHitsExceeded = true; break; } } } //Don't forget to delete the document delete document; //Return whether the max number of hits was reached return nHitsExceeded; }
void TestMetaData::checkStrings2() { Poppler::Document *doc; doc = Poppler::Document::load("../../../test/unittestcases/truetype.pdf"); QVERIFY( doc ); QFETCH( QString, key ); QFETCH( QString, value ); QCOMPARE( doc->info(key), value ); delete doc; }
int main( int argc, char **argv ) { QApplication a( argc, argv ); // QApplication required! QTime t; t.start(); QDir directory( argv[1] ); foreach ( const QString &fileName, directory.entryList() ) { if (fileName.endsWith("pdf") ) { qDebug() << "Doing" << fileName.toLatin1().data() << ":"; Poppler::Document *doc = Poppler::Document::load( directory.canonicalPath()+"/"+fileName ); if (!doc) { qWarning() << "doc not loaded"; } else if ( doc->isLocked() ) { if (! doc->unlock( "", "password" ) ) { qWarning() << "couldn't unlock document"; delete doc; } } else { doc->pdfVersion(); doc->info("Title"); doc->info("Subject"); doc->info("Author"); doc->info("Keywords"); doc->info("Creator"); doc->info("Producer"); doc->date("CreationDate").toString(); doc->date("ModDate").toString(); doc->numPages(); doc->isLinearized(); doc->isEncrypted(); doc->okToPrint(); doc->okToCopy(); doc->okToChange(); doc->okToAddNotes(); doc->pageMode(); for( int index = 0; index < doc->numPages(); ++index ) { Poppler::Page *page = doc->page( index ); QImage image = page->renderToImage(); page->pageSize(); page->orientation(); delete page; std::cout << "."; std::cout.flush(); } std::cout << std::endl; delete doc; } } } std::cout << "Elapsed time: " << (t.elapsed()/1000) << "seconds" << std::endl; }
int main( int argc, char **argv ) { QApplication a( argc, argv ); // QApplication required! if ( argc != 3) { qWarning() << "usage: test-password-qt4 owner-password filename"; exit(1); } Poppler::Document *doc = Poppler::Document::load(argv[2], argv[1]); if (!doc) { qWarning() << "doc not loaded"; exit(1); } // output some meta-data int major = 0, minor = 0; doc->getPdfVersion( &major, &minor ); qDebug() << " PDF Version: " << qPrintable(QString::fromLatin1("%1.%2").arg(major).arg(minor)); qDebug() << " Title: " << doc->info("Title"); qDebug() << " Subject: " << doc->info("Subject"); qDebug() << " Author: " << doc->info("Author"); qDebug() << " Key words: " << doc->info("Keywords"); qDebug() << " Creator: " << doc->info("Creator"); qDebug() << " Producer: " << doc->info("Producer"); qDebug() << " Date created: " << doc->date("CreationDate").toString(); qDebug() << " Date modified: " << doc->date("ModDate").toString(); qDebug() << "Number of pages: " << doc->numPages(); qDebug() << " Linearised: " << doc->isLinearized(); qDebug() << " Encrypted: " << doc->isEncrypted(); qDebug() << " OK to print: " << doc->okToPrint(); qDebug() << " OK to copy: " << doc->okToCopy(); qDebug() << " OK to change: " << doc->okToChange(); qDebug() << "OK to add notes: " << doc->okToAddNotes(); qDebug() << " Page mode: " << doc->pageMode(); QStringList fontNameList; foreach( const Poppler::FontInfo &font, doc->fonts() ) fontNameList += font.name(); qDebug() << " Fonts: " << fontNameList.join( ", " ); Poppler::Page *page = doc->page(0); qDebug() << " Page 1 size: " << page->pageSize().width()/72 << "inches x " << page->pageSize().height()/72 << "inches"; PDFDisplay test( doc ); // create picture display test.setWindowTitle("Poppler-Qt4 Test"); test.show(); // show it return a.exec(); // start event loop }
void PDFInfoDock::fillInfo() { list->clear(); Poppler::Document *doc = document->popplerDoc(); QStringList keys = doc->infoKeys(); QStringList dateKeys; dateKeys << "CreationDate"; dateKeys << "ModDate"; int i = 0; foreach (const QString &date, dateKeys) { const int id = keys.indexOf(date); if (id != -1) { list->addItem(date + ":"); list->addItem(doc->date(date).toString(Qt::SystemLocaleDate)); ++i; keys.removeAt(id); } } foreach (const QString &key, keys) { list->addItem(key + ":"); list->addItem(doc->info(key)); ++i; }
int main( int argc, char **argv ) { QApplication a( argc, argv ); // QApplication required! Q_UNUSED( argc ); Q_UNUSED( argv ); QTime t; t.start(); QDir dbDir( QStringLiteral( "./pdfdb" ) ); if ( !dbDir.exists() ) { qWarning() << "Database directory does not exist"; } QStringList excludeSubDirs; excludeSubDirs << QStringLiteral("000048") << QStringLiteral("000607"); const QStringList dirs = dbDir.entryList(QStringList() << QStringLiteral("0000*"), QDir::Dirs); foreach ( const QString &subdir, dirs ) { if ( excludeSubDirs.contains(subdir) ) { // then skip it } else { QString path = "./pdfdb/" + subdir + "/data.pdf"; std::cout <<"Doing " << path.toLatin1().data() << " :"; Poppler::Document *doc = Poppler::Document::load( path ); if (!doc) { qWarning() << "doc not loaded"; } else { int major = 0, minor = 0; doc->getPdfVersion( &major, &minor ); doc->info(QStringLiteral("Title")); doc->info(QStringLiteral("Subject")); doc->info(QStringLiteral("Author")); doc->info(QStringLiteral("Keywords")); doc->info(QStringLiteral("Creator")); doc->info(QStringLiteral("Producer")); doc->date(QStringLiteral("CreationDate")).toString(); doc->date(QStringLiteral("ModDate")).toString(); doc->numPages(); doc->isLinearized(); doc->isEncrypted(); doc->okToPrint(); doc->okToCopy(); doc->okToChange(); doc->okToAddNotes(); doc->pageMode(); for( int index = 0; index < doc->numPages(); ++index ) { Poppler::Page *page = doc->page( index ); page->renderToImage(); page->pageSize(); page->orientation(); delete page; std::cout << "."; std::cout.flush(); } std::cout << std::endl; delete doc; } } } std::cout << "Elapsed time: " << (t.elapsed()/1000) << std::endl; }
void PdfViewer::loadDocument(const QString &file, PdfView::PositionHandling keepPosition) { //QTime t = QTime::currentTime(); #ifndef QT_NO_CURSOR QApplication::setOverrideCursor(Qt::WaitCursor); #endif // QT_NO_CURSOR // TODO: close only when the new file fails to load const QString tempFileName = file; // we must copy file in a new variable because otherwise closeDocument() empties file if file == m_file (which is the case in slotReload()) closeDocument(); bool isLoaded = m_pdfView->load(tempFileName); if (!isLoaded) { QPointer<QMessageBox> msgBox = new QMessageBox(QMessageBox::Critical, tr("Open Error"), tr("Cannot open:\n") + tempFileName, QMessageBox::Ok, this); msgBox->exec(); delete msgBox; m_fileOpenRecentAction->removeFile(tempFileName); #ifndef QT_NO_CURSOR QApplication::restoreOverrideCursor(); #endif // QT_NO_CURSOR return; } Poppler::Document *doc = m_pdfView->document(); while (doc->isLocked()) { bool ok = true; QString password = QInputDialog::getText(this, tr("Document Password"), tr("Please insert the password of the document:"), QLineEdit::Password, QString(), &ok); if (!ok) { m_pdfView->close(); m_fileOpenRecentAction->removeFile(tempFileName); #ifndef QT_NO_CURSOR QApplication::restoreOverrideCursor(); #endif // QT_NO_CURSOR return; } doc->unlock(password.toLatin1(), password.toLatin1()); } m_file = QFileInfo(tempFileName).absoluteFilePath(); m_fileOpenRecentAction->addFile(m_file); // remove previous file from m_watcher and add new file if (m_watcher) { const QStringList files = m_watcher->files(); if (!files.isEmpty()) m_watcher->removePaths(files); m_watcher->addPath(m_file); } //qCritical() << t.msecsTo(QTime::currentTime()); Q_FOREACH(DocumentObserver *obs, m_observers) { if (keepPosition == PdfView::DontKeepPosition) obs->documentLoaded(); obs->pageChanged(m_currentPage, keepPosition); //qCritical() << t.msecsTo(QTime::currentTime()); } // set window title const QString docTitle = doc->info("Title"); setWindowTitle((docTitle.isEmpty() ? QFileInfo(m_file).fileName() : docTitle) + " - " + QCoreApplication::applicationName()); // enable actions m_fileSaveCopyAction->setEnabled(true); m_findAction->setEnabled(true); m_findNextAction->setEnabled(true); m_findPreviousAction->setEnabled(true); m_showPresentationAction->setEnabled(true); #ifndef QT_NO_CURSOR QApplication::restoreOverrideCursor(); #endif // QT_NO_CURSOR //qCritical() << "close and load document" << t.msecsTo(QTime::currentTime()); }