diff --git a/Pdf4QtDocPageOrganizer/assembleoutputsettingsdialog.ui b/Pdf4QtDocPageOrganizer/assembleoutputsettingsdialog.ui index 32da2b7..37fca4a 100644 --- a/Pdf4QtDocPageOrganizer/assembleoutputsettingsdialog.ui +++ b/Pdf4QtDocPageOrganizer/assembleoutputsettingsdialog.ui @@ -7,7 +7,7 @@ 0 0 614 - 213 + 250 diff --git a/Pdf4QtDocPageOrganizer/mainwindow.cpp b/Pdf4QtDocPageOrganizer/mainwindow.cpp index a4bda46..bf59f83 100644 --- a/Pdf4QtDocPageOrganizer/mainwindow.cpp +++ b/Pdf4QtDocPageOrganizer/mainwindow.cpp @@ -23,6 +23,7 @@ #include "pdfwidgetutils.h" #include "pdfdocumentreader.h" +#include "pdfdocumentwriter.h" #include #include @@ -551,6 +552,27 @@ void MainWindow::performOperation(Operation operation) QString fileNameTemplate = dialog.getFileName(); const bool isOverwriteEnabled = dialog.isOverwriteFiles(); + if (!directory.endsWith('/')) + { + directory += "/"; + } + + auto replaceInString = [](QString& templateString, QChar character, int number) + { + int index = templateString.indexOf(character, 0, Qt::CaseSensitive); + if (index != -1) + { + int lastIndex = templateString.lastIndexOf(character, -1, Qt::CaseSensitive); + int count = lastIndex - index + 1; + + QString textNumber = QString::number(number); + textNumber = textNumber.rightJustified(count, '0', false); + + templateString.remove(index, count); + templateString.insert(index, textNumber); + } + }; + for (const std::vector& assembledPages : assembledDocuments) { pdf::PDFOperationResult currentResult = manipulator.assemble(assembledPages); @@ -562,14 +584,19 @@ void MainWindow::performOperation(Operation operation) pdf::PDFDocumentManipulator::AssembledPage samplePage = assembledPages.front(); sourceDocumentIndex = samplePage.documentIndex == -1 ? documentCount + samplePage.imageIndex : samplePage.documentIndex; - sourcePageIndex = qMax(samplePage.pageIndex + 1, 1); + sourcePageIndex = qMax(int(samplePage.pageIndex + 1), 1); QString fileName = fileNameTemplate; + replaceInString(fileName, '#', assembledDocumentIndex); + replaceInString(fileName, '@', sourcePageIndex); + replaceInString(fileName, '%', sourceDocumentIndex); + if (!fileName.endsWith(".pdf")) { fileName += ".pdf"; } + fileName.prepend(directory); assembledDocumentStorage.emplace_back(std::make_pair(std::move(fileName), manipulator.takeAssembledDocument())); ++assembledDocumentIndex; @@ -578,10 +605,31 @@ void MainWindow::performOperation(Operation operation) if (!result) { QMessageBox::critical(this, tr("Error"), result.getErrorMessage()); - break; + return; } + // Now, try to save files + for (const auto& assembledDocumentItem : assembledDocumentStorage) + { + QString filename = assembledDocumentItem.first; + const pdf::PDFDocument* document = &assembledDocumentItem.second; + const bool isDocumentFileAlreadyExisting = QFile::exists(filename); + if (!isOverwriteEnabled && isDocumentFileAlreadyExisting) + { + QMessageBox::critical(this, tr("Error"), tr("Document with given filename already exists.")); + return; + } + + pdf::PDFDocumentWriter writer(nullptr); + pdf::PDFOperationResult result = writer.write(filename, document, isDocumentFileAlreadyExisting); + + if (!result) + { + QMessageBox::critical(this, tr("Error"), result.getErrorMessage()); + return; + } + } } break; diff --git a/Pdf4QtDocPageOrganizer/mainwindow.ui b/Pdf4QtDocPageOrganizer/mainwindow.ui index 3edb567..0964820 100644 --- a/Pdf4QtDocPageOrganizer/mainwindow.ui +++ b/Pdf4QtDocPageOrganizer/mainwindow.ui @@ -11,7 +11,7 @@ - MainWindow + Workspace diff --git a/Pdf4QtDocPageOrganizer/pageitemmodel.cpp b/Pdf4QtDocPageOrganizer/pageitemmodel.cpp index 6e2d2fe..3f04131 100644 --- a/Pdf4QtDocPageOrganizer/pageitemmodel.cpp +++ b/Pdf4QtDocPageOrganizer/pageitemmodel.cpp @@ -877,7 +877,7 @@ std::vector> PageItemMod } assembledPage.pageRotation = pdf::getPageRotationCombined(originalPageRotation, item.pageAdditionalRotation); - assembledPage.pageSize = pdf::PDFPage::getRotatedSize(item.rotatedPageDimensionsMM, pdf::getPageRotationInversed(item.pageAdditionalRotation)); + assembledPage.pageSize = item.rotatedPageDimensionsMM; return assembledPage; }; diff --git a/Pdf4QtDocPageOrganizer/pageitemmodel.h b/Pdf4QtDocPageOrganizer/pageitemmodel.h index 8b3db55..65ae06a 100644 --- a/Pdf4QtDocPageOrganizer/pageitemmodel.h +++ b/Pdf4QtDocPageOrganizer/pageitemmodel.h @@ -48,8 +48,8 @@ struct PageGroupItem int documentIndex = -1; pdf::PDFInteger pageIndex = -1; pdf::PDFInteger imageIndex = -1; - QSizeF rotatedPageDimensionsMM; - pdf::PageRotation pageAdditionalRotation = pdf::PageRotation::None; + QSizeF rotatedPageDimensionsMM; ///< Rotated page dimensions, but without additional rotation + pdf::PageRotation pageAdditionalRotation = pdf::PageRotation::None; ///< Additional rotation applied to the page PageType pageType = PT_DocumentPage; }; diff --git a/Pdf4QtLib/sources/pdfdocumentwriter.cpp b/Pdf4QtLib/sources/pdfdocumentwriter.cpp index 956d645..0b61376 100644 --- a/Pdf4QtLib/sources/pdfdocumentwriter.cpp +++ b/Pdf4QtLib/sources/pdfdocumentwriter.cpp @@ -173,6 +173,14 @@ void PDFWriteObjectVisitor::visitReference(const PDFObjectReference reference) PDFOperationResult PDFDocumentWriter::write(const QString& fileName, const PDFDocument* document, bool safeWrite) { + Q_ASSERT(document); + + const PDFObjectStorage& storage = document->getStorage(); + if (!storage.getSecurityHandler()->isEncryptionAllowed()) + { + return tr("Writing of encrypted documents is not supported."); + } + if (safeWrite) { QSaveFile file(fileName); @@ -204,6 +212,13 @@ PDFOperationResult PDFDocumentWriter::write(const QString& fileName, const PDFDo { PDFOperationResult result = write(&file, document); file.close(); + + if (!result) + { + // If some error occured, then remove invalid file + file.remove(); + } + return result; } else