PDF4QT/PdfTool/pdftoolfetchtext.cpp

132 lines
4.8 KiB
C++
Raw Normal View History

2020-10-11 18:21:20 +02:00
// Copyright (C) 2020 Jakub Melka
//
2020-12-20 19:03:58 +01:00
// This file is part of Pdf4Qt.
2020-10-11 18:21:20 +02:00
//
2020-12-20 19:03:58 +01:00
// Pdf4Qt is free software: you can redistribute it and/or modify
2020-10-11 18:21:20 +02:00
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2020-12-20 19:03:58 +01:00
// Pdf4Qt is distributed in the hope that it will be useful,
2020-10-11 18:21:20 +02:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
2020-12-20 19:03:58 +01:00
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
2020-10-11 18:21:20 +02:00
#include "pdftoolfetchtext.h"
#include "pdfdocumenttextflow.h"
namespace pdftool
{
static PDFToolFetchTextApplication s_fetchTextApplication;
QString PDFToolFetchTextApplication::getStandardString(PDFToolAbstractApplication::StandardString standardString) const
{
switch (standardString)
{
case Command:
return "fetch-text";
case Name:
return PDFToolTranslationContext::tr("Fetch text");
case Description:
2020-10-18 12:57:27 +02:00
return PDFToolTranslationContext::tr("Fetch text content from document.");
2020-10-11 18:21:20 +02:00
default:
Q_ASSERT(false);
break;
}
return QString();
}
int PDFToolFetchTextApplication::execute(const PDFToolOptions& options)
{
pdf::PDFDocument document;
QByteArray sourceData;
if (!readDocument(options, document, &sourceData))
{
return ErrorDocumentReading;
}
2020-10-18 12:57:27 +02:00
if (!document.getStorage().getSecurityHandler()->isAllowed(pdf::PDFSecurityHandler::Permission::CopyContent))
{
PDFConsole::writeError(PDFToolTranslationContext::tr("Document doesn't allow to copy content."), options.outputCodec);
return ErrorPermissions;
}
2020-10-11 18:21:20 +02:00
QString parseError;
std::vector<pdf::PDFInteger> pages = options.getPageRange(document.getCatalog()->getPageCount(), parseError, true);
if (!parseError.isEmpty())
{
PDFConsole::writeError(parseError, options.outputCodec);
return ErrorInvalidArguments;
}
pdf::PDFDocumentTextFlowFactory factory;
pdf::PDFDocumentTextFlow documentTextFlow = factory.create(&document, pages, options.textAnalysisAlgorithm);
2020-10-17 16:56:39 +02:00
PDFOutputFormatter formatter(options.outputStyle, options.outputCodec);
formatter.beginDocument("text-extraction", QString());
formatter.endl();
for (const pdf::PDFDocumentTextFlow::Item& item : documentTextFlow.getItems())
{
if (item.flags.testFlag(pdf::PDFDocumentTextFlow::StructureItemStart))
{
formatter.beginHeader("item", item.text);
}
if (!item.text.isEmpty())
{
2020-10-18 12:57:27 +02:00
bool showText = (item.flags.testFlag(pdf::PDFDocumentTextFlow::Text)) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::PageStart) && options.textShowPageNumbers) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::PageEnd) && options.textShowPageNumbers) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::StructureTitle) && options.textShowStructTitles) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::StructureLanguage) && options.textShowStructLanguage) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::StructureAlternativeDescription) && options.textShowStructAlternativeDescription) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::StructureExpandedForm) && options.textShowStructExpandedForm) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::StructureActualText) && options.textShowStructActualText) ||
(item.flags.testFlag(pdf::PDFDocumentTextFlow::StructurePhoneme) && options.textShowStructPhoneme);
if (showText)
{
formatter.writeText("text", item.text);
}
2020-10-17 16:56:39 +02:00
}
if (item.flags.testFlag(pdf::PDFDocumentTextFlow::StructureItemEnd))
{
formatter.endHeader();
}
if (item.flags.testFlag(pdf::PDFDocumentTextFlow::PageEnd))
{
formatter.endl();
}
}
formatter.endDocument();
2020-10-11 18:21:20 +02:00
for (const pdf::PDFRenderError& error : factory.getErrors())
{
PDFConsole::writeError(error.message, options.outputCodec);
}
2020-10-17 16:56:39 +02:00
PDFConsole::writeText(formatter.getString(), options.outputCodec);
2020-10-11 18:21:20 +02:00
return ExitSuccess;
}
PDFToolAbstractApplication::Options PDFToolFetchTextApplication::getOptionsFlags() const
{
2020-10-18 12:57:27 +02:00
return ConsoleFormat | OpenDocument | PageSelector | TextAnalysis | TextShow;
2020-10-11 18:21:20 +02:00
}
} // namespace pdftool