mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Separate tool for extracting pages
This commit is contained in:
140
PdfTool/pdftoolseparate.cpp
Normal file
140
PdfTool/pdftoolseparate.cpp
Normal file
@ -0,0 +1,140 @@
|
||||
// Copyright (C) 2020 Jakub Melka
|
||||
//
|
||||
// This file is part of PdfForQt.
|
||||
//
|
||||
// PdfForQt is free software: you can redistribute it and/or modify
|
||||
// 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.
|
||||
//
|
||||
// PdfForQt is distributed in the hope that it will be useful,
|
||||
// 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
|
||||
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#include "pdftoolseparate.h"
|
||||
#include "pdfdocumentbuilder.h"
|
||||
#include "pdfexception.h"
|
||||
#include "pdfoptimizer.h"
|
||||
#include "pdfdocumentwriter.h"
|
||||
|
||||
namespace pdftool
|
||||
{
|
||||
|
||||
static PDFToolSeparate s_toolSeparateApplication;
|
||||
|
||||
QString PDFToolSeparate::getStandardString(StandardString standardString) const
|
||||
{
|
||||
switch (standardString)
|
||||
{
|
||||
case Command:
|
||||
return "separate";
|
||||
|
||||
case Name:
|
||||
return PDFToolTranslationContext::tr("Extract pages");
|
||||
|
||||
case Description:
|
||||
return PDFToolTranslationContext::tr("Separate document into single page documents.");
|
||||
|
||||
default:
|
||||
Q_ASSERT(false);
|
||||
break;
|
||||
}
|
||||
|
||||
return QString();
|
||||
}
|
||||
|
||||
int PDFToolSeparate::execute(const PDFToolOptions& options)
|
||||
{
|
||||
pdf::PDFDocument document;
|
||||
QByteArray sourceData;
|
||||
if (!readDocument(options, document, &sourceData))
|
||||
{
|
||||
return ErrorDocumentReading;
|
||||
}
|
||||
|
||||
if (!document.getStorage().getSecurityHandler()->isAllowed(pdf::PDFSecurityHandler::Permission::CopyContent))
|
||||
{
|
||||
PDFConsole::writeError(PDFToolTranslationContext::tr("Document doesn't allow to copy content."), options.outputCodec);
|
||||
return ErrorPermissions;
|
||||
}
|
||||
|
||||
QString parseError;
|
||||
std::vector<pdf::PDFInteger> pageIndices = options.getPageRange(document.getCatalog()->getPageCount(), parseError, true);
|
||||
|
||||
if (!parseError.isEmpty())
|
||||
{
|
||||
PDFConsole::writeError(parseError, options.outputCodec);
|
||||
return ErrorInvalidArguments;
|
||||
}
|
||||
|
||||
if (options.separatePagePattern.isEmpty())
|
||||
{
|
||||
PDFConsole::writeError(PDFToolTranslationContext::tr("File template is empty."), options.outputCodec);
|
||||
return ErrorInvalidArguments;
|
||||
}
|
||||
|
||||
if (!options.separatePagePattern.contains("%"))
|
||||
{
|
||||
PDFConsole::writeError(PDFToolTranslationContext::tr("File template must contain character '%' for page number."), options.outputCodec);
|
||||
return ErrorInvalidArguments;
|
||||
}
|
||||
|
||||
for (pdf::PDFInteger pageIndex : pageIndices)
|
||||
{
|
||||
try
|
||||
{
|
||||
pdf::PDFDocumentBuilder documentBuilder(&document);
|
||||
documentBuilder.flattenPageTree();
|
||||
std::vector<pdf::PDFObjectReference> pageReferences = documentBuilder.getPages();
|
||||
std::vector<pdf::PDFObjectReference> singlePageRef = { pageReferences[pageIndex] };
|
||||
documentBuilder.setPages(singlePageRef);
|
||||
documentBuilder.removeOutline();
|
||||
documentBuilder.removeThreads();
|
||||
documentBuilder.removeDocumentActions();
|
||||
documentBuilder.removeStructureTree();
|
||||
|
||||
pdf::PDFDocument singlePageDocument = documentBuilder.build();
|
||||
|
||||
// Optimize document - remove unused objects and shrink object storage
|
||||
pdf::PDFOptimizer optimizer(pdf::PDFOptimizer::RemoveUnusedObjects | pdf::PDFOptimizer::ShrinkObjectStorage, nullptr);
|
||||
optimizer.setDocument(&singlePageDocument);
|
||||
optimizer.optimize();
|
||||
singlePageDocument = optimizer.takeOptimizedDocument();
|
||||
|
||||
QString fileName = options.separatePagePattern;
|
||||
fileName.replace('%', QString::number(pageIndex + 1));
|
||||
|
||||
if (QFileInfo::exists(fileName))
|
||||
{
|
||||
PDFConsole::writeError(PDFToolTranslationContext::tr("File '%1' already exists. Page %2 was not extracted.").arg(fileName).arg(pageIndex + 1), options.outputCodec);
|
||||
}
|
||||
else
|
||||
{
|
||||
pdf::PDFDocumentWriter writer(nullptr);
|
||||
pdf::PDFOperationResult result = writer.write(fileName, &singlePageDocument, false);
|
||||
if (!result)
|
||||
{
|
||||
PDFConsole::writeError(result.getErrorMessage(), options.outputCodec);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (pdf::PDFException exception)
|
||||
{
|
||||
PDFConsole::writeError(exception.getMessage(), options.outputCodec);
|
||||
}
|
||||
}
|
||||
|
||||
return ExitSuccess;
|
||||
}
|
||||
|
||||
PDFToolAbstractApplication::Options PDFToolSeparate::getOptionsFlags() const
|
||||
{
|
||||
return ConsoleFormat | OpenDocument | PageSelector | Separate;
|
||||
}
|
||||
|
||||
|
||||
} // namespace pdftool
|
Reference in New Issue
Block a user