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:
@@ -55,6 +55,7 @@ SOURCES += \
|
||||
pdftoolinfopageboxes.cpp \
|
||||
pdftoolinfostructuretree.cpp \
|
||||
pdftoolrender.cpp \
|
||||
pdftoolseparate.cpp \
|
||||
pdftoolverifysignatures.cpp \
|
||||
pdftoolxml.cpp
|
||||
|
||||
@@ -83,5 +84,6 @@ HEADERS += \
|
||||
pdftoolinfopageboxes.h \
|
||||
pdftoolinfostructuretree.h \
|
||||
pdftoolrender.h \
|
||||
pdftoolseparate.h \
|
||||
pdftoolverifysignatures.h \
|
||||
pdftoolxml.h
|
||||
|
@@ -164,6 +164,11 @@ void PDFToolAbstractApplication::initializeCommandLineParser(QCommandLineParser*
|
||||
parser->addOption(QCommandLineOption("no-permissive-reading", "Do not attempt to fix damaged documents."));
|
||||
}
|
||||
|
||||
if (optionFlags.testFlag(Separate))
|
||||
{
|
||||
parser->addPositionalArgument("pattern", "Page pattern, must contain '%' character if multiple pages are selected.");
|
||||
}
|
||||
|
||||
if (optionFlags.testFlag(SignatureVerification))
|
||||
{
|
||||
parser->addOption(QCommandLineOption("ver-no-user-cert", "Disable user certificate store."));
|
||||
@@ -361,6 +366,11 @@ PDFToolOptions PDFToolAbstractApplication::getOptions(QCommandLineParser* parser
|
||||
options.permissiveReading = !parser->isSet("no-permissive-reading");
|
||||
}
|
||||
|
||||
if (optionFlags.testFlag(Separate))
|
||||
{
|
||||
options.separatePagePattern = positionalArguments.size() >= 2 ? positionalArguments[1] : QString();
|
||||
}
|
||||
|
||||
if (optionFlags.testFlag(SignatureVerification))
|
||||
{
|
||||
options.verificationUseUserCertificates = !parser->isSet("ver-no-user-cert");
|
||||
|
@@ -129,6 +129,9 @@ struct PDFToolOptions
|
||||
int renderMSAAsamples = 4;
|
||||
int renderRasterizerCount = pdf::PDFRasterizerPool::getDefaultRasterizerCount();
|
||||
|
||||
// For option 'Separate'
|
||||
QString separatePagePattern;
|
||||
|
||||
/// Returns page range. If page range is invalid, then \p errorMessage is empty.
|
||||
/// \param pageCount Page count
|
||||
/// \param[out] errorMessage Error message
|
||||
@@ -194,6 +197,7 @@ public:
|
||||
ImageExportSettingsResolution = 0x00008000, ///< Settings for resolution of exported images
|
||||
ColorManagementSystem = 0x00010000, ///< Color management system settings
|
||||
RenderFlags = 0x00020000, ///< Render flags for page image rasterizer
|
||||
Separate = 0x00040000, ///< Settings for Separate tool
|
||||
};
|
||||
Q_DECLARE_FLAGS(Options, Option)
|
||||
|
||||
|
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
|
36
PdfTool/pdftoolseparate.h
Normal file
36
PdfTool/pdftoolseparate.h
Normal file
@@ -0,0 +1,36 @@
|
||||
// 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/>.
|
||||
|
||||
#ifndef PDFTOOLSEPARATE_H
|
||||
#define PDFTOOLSEPARATE_H
|
||||
|
||||
#include "pdftoolabstractapplication.h"
|
||||
|
||||
namespace pdftool
|
||||
{
|
||||
|
||||
class PDFToolSeparate : public PDFToolAbstractApplication
|
||||
{
|
||||
public:
|
||||
virtual QString getStandardString(StandardString standardString) const override;
|
||||
virtual int execute(const PDFToolOptions& options) override;
|
||||
virtual Options getOptionsFlags() const override;
|
||||
};
|
||||
|
||||
} // namespace pdftool
|
||||
|
||||
#endif // PDFTOOLSEPARATE_H
|
Reference in New Issue
Block a user