mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Save document capability
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#include "pdfparser.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QSaveFile>
|
||||
|
||||
namespace pdf
|
||||
{
|
||||
@@ -170,19 +171,45 @@ void PDFWriteObjectVisitor::visitReference(const PDFObjectReference reference)
|
||||
m_device->write("R ");
|
||||
}
|
||||
|
||||
PDFOperationResult PDFDocumentWriter::write(const QString& fileName, const PDFDocument* document)
|
||||
PDFOperationResult PDFDocumentWriter::write(const QString& fileName, const PDFDocument* document, bool safeWrite)
|
||||
{
|
||||
QFile file(fileName);
|
||||
|
||||
if (file.open(QFile::WriteOnly | QFile::Truncate))
|
||||
if (safeWrite)
|
||||
{
|
||||
PDFOperationResult result = write(&file, document);
|
||||
file.close();
|
||||
return result;
|
||||
QSaveFile file(fileName);
|
||||
file.setDirectWriteFallback(true);
|
||||
|
||||
if (file.open(QFile::WriteOnly | QFile::Truncate))
|
||||
{
|
||||
PDFOperationResult result = write(&file, document);
|
||||
if (result)
|
||||
{
|
||||
file.commit();
|
||||
}
|
||||
else
|
||||
{
|
||||
file.cancelWriting();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return tr("File '%1' can't be opened for writing. %2").arg(fileName, file.errorString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return tr("File '%1' can't be opened for writing. %2").arg(fileName, file.errorString());
|
||||
QFile file(fileName);
|
||||
|
||||
if (file.open(QFile::WriteOnly | QFile::Truncate))
|
||||
{
|
||||
PDFOperationResult result = write(&file, document);
|
||||
file.close();
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return tr("File '%1' can't be opened for writing. %2").arg(fileName, file.errorString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -40,7 +40,20 @@ public:
|
||||
|
||||
}
|
||||
|
||||
PDFOperationResult write(const QString& fileName, const PDFDocument* document);
|
||||
/// Writes document to the file. If \p safeWrite is true, then document is first
|
||||
/// written to the temporary file, and then renamed to original file name atomically,
|
||||
/// so no data can be lost on, for example, power failure. If it is not possible to
|
||||
/// create temporary file, then writing operation will attempt to write to the file
|
||||
/// directly.
|
||||
/// \param fileName File name
|
||||
/// \param document Document
|
||||
/// \param safeWrite Write document to the temporary file and then rename
|
||||
PDFOperationResult write(const QString& fileName, const PDFDocument* document, bool safeWrite);
|
||||
|
||||
/// Write document to the output device. Device must be writable (i.e. opened
|
||||
/// for writing).
|
||||
/// \param device Output device
|
||||
/// \param document Document
|
||||
PDFOperationResult write(QIODevice* device, const PDFDocument* document);
|
||||
|
||||
/// Calculates document file size, as if it is written to the disk.
|
||||
|
Reference in New Issue
Block a user