mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-01-28 16:20:17 +01:00
Create a new blank document
This commit is contained in:
parent
1af6cf0c31
commit
fcaa288c5b
@ -251,7 +251,7 @@ void CodeGenerator::generateCode(QString headerName, QString sourceName) const
|
||||
QFile headerFile(headerName);
|
||||
if (headerFile.exists())
|
||||
{
|
||||
if (headerFile.open(QFile::ReadOnly))
|
||||
if (headerFile.open(QFile::ReadOnly | QFile::Text))
|
||||
{
|
||||
QString utfCode = QString::fromUtf8(headerFile.readAll());
|
||||
headerFile.close();
|
||||
@ -273,7 +273,7 @@ void CodeGenerator::generateCode(QString headerName, QString sourceName) const
|
||||
QFile sourceFile(sourceName);
|
||||
if (sourceFile.exists())
|
||||
{
|
||||
if (sourceFile.open(QFile::ReadOnly))
|
||||
if (sourceFile.open(QFile::ReadOnly | QFile::Text))
|
||||
{
|
||||
QString utfCode = QString::fromUtf8(sourceFile.readAll());
|
||||
sourceFile.close();
|
||||
|
37
PdfExampleGenerator/PdfExampleGenerator.pro
Normal file
37
PdfExampleGenerator/PdfExampleGenerator.pro
Normal file
@ -0,0 +1,37 @@
|
||||
QT += gui
|
||||
|
||||
CONFIG += c++11 console
|
||||
CONFIG -= app_bundle
|
||||
|
||||
# The following define makes your compiler emit warnings if you use
|
||||
# any Qt feature that has been marked deprecated (the exact warnings
|
||||
# depend on your compiler). Please consult the documentation of the
|
||||
# deprecated API in order to know how to port your code away from it.
|
||||
DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
# You can also make your code fail to compile if it uses deprecated APIs.
|
||||
# In order to do so, uncomment the following line.
|
||||
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
QMAKE_CXXFLAGS += /std:c++latest /utf-8
|
||||
|
||||
INCLUDEPATH += $$PWD/../PDFForQtLib/Sources
|
||||
|
||||
DESTDIR = $$OUT_PWD/..
|
||||
|
||||
LIBS += -L$$OUT_PWD/..
|
||||
|
||||
LIBS += -lPDFForQtLib
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
pdfexamplesgenerator.cpp
|
||||
|
||||
# Default rules for deployment.
|
||||
qnx: target.path = /tmp/$${TARGET}/bin
|
||||
else: unix:!android: target.path = /opt/$${TARGET}/bin
|
||||
!isEmpty(target.path): INSTALLS += target
|
||||
|
||||
HEADERS += \
|
||||
pdfexamplesgenerator.h
|
26
PdfExampleGenerator/main.cpp
Normal file
26
PdfExampleGenerator/main.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
// 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 <QCoreApplication>
|
||||
|
||||
#include "pdfexamplesgenerator.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
PDFExamplesGenerator::generateAnnotationsExample();
|
||||
}
|
33
PdfExampleGenerator/pdfexamplesgenerator.cpp
Normal file
33
PdfExampleGenerator/pdfexamplesgenerator.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
// 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 "pdfexamplesgenerator.h"
|
||||
|
||||
#include "pdfdocumentbuilder.h"
|
||||
#include "pdfdocumentwriter.h"
|
||||
|
||||
void PDFExamplesGenerator::generateAnnotationsExample()
|
||||
{
|
||||
pdf::PDFDocumentBuilder builder;
|
||||
|
||||
builder.appendPage(QRectF(0, 0, 400, 400));
|
||||
|
||||
// Write result to a file
|
||||
pdf::PDFDocument document = builder.build();
|
||||
pdf::PDFDocumentWriter writer(nullptr);
|
||||
writer.write("Ex_Annotations.pdf", &document);
|
||||
}
|
29
PdfExampleGenerator/pdfexamplesgenerator.h
Normal file
29
PdfExampleGenerator/pdfexamplesgenerator.h
Normal file
@ -0,0 +1,29 @@
|
||||
// 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 PDFEXAMPLESGENERATOR_H
|
||||
#define PDFEXAMPLESGENERATOR_H
|
||||
|
||||
class PDFExamplesGenerator
|
||||
{
|
||||
public:
|
||||
explicit PDFExamplesGenerator() = delete;
|
||||
|
||||
static void generateAnnotationsExample();
|
||||
};
|
||||
|
||||
#endif // PDFEXAMPLESGENERATOR_H
|
@ -20,6 +20,7 @@ TEMPLATE = subdirs
|
||||
SUBDIRS += \
|
||||
CodeGenerator \
|
||||
JBIG2_Viewer \
|
||||
PdfExampleGenerator \
|
||||
PdfForQtLib \
|
||||
UnitTests \
|
||||
PdfForQtViewer
|
||||
@ -27,3 +28,4 @@ SUBDIRS += \
|
||||
UnitTests.depends = PdfForQtLib
|
||||
PdfForQtViewer.depends = PdfForQtLib
|
||||
JBIG2_Viewer.depends = PdfForQtLib
|
||||
PDFExampleGenerator.depends = PdfForQtLib
|
||||
|
@ -43,6 +43,7 @@ SOURCES += \
|
||||
sources/pdfcms.cpp \
|
||||
sources/pdfcompiler.cpp \
|
||||
sources/pdfdocumentbuilder.cpp \
|
||||
sources/pdfdocumentwriter.cpp \
|
||||
sources/pdfexecutionpolicy.cpp \
|
||||
sources/pdffile.cpp \
|
||||
sources/pdfitemmodels.cpp \
|
||||
@ -90,6 +91,7 @@ HEADERS += \
|
||||
sources/pdfcompiler.h \
|
||||
sources/pdfdocumentbuilder.h \
|
||||
sources/pdfdocumentdrawinterface.h \
|
||||
sources/pdfdocumentwriter.h \
|
||||
sources/pdfexecutionpolicy.h \
|
||||
sources/pdffile.h \
|
||||
sources/pdfitemmodels.h \
|
||||
|
@ -78,6 +78,9 @@ public:
|
||||
/// Returns security handler associated with these objects
|
||||
const PDFSecurityHandler* getSecurityHandler() const { return m_securityHandler.data(); }
|
||||
|
||||
/// Sets security handler associated with these objects
|
||||
void setSecurityHandler(PDFSecurityHandlerPointer handler) { m_securityHandler = qMove(handler); }
|
||||
|
||||
/// Adds a new object to the object list. This function
|
||||
/// is not thread safe, do not call it from multiple threads.
|
||||
/// \param object Object to be added
|
||||
|
@ -64,6 +64,13 @@ void PDFObjectFactory::endDictionaryItem()
|
||||
std::get<PDFDictionary>(dictionaryItem.object).addEntry(qMove(topItem.itemName), qMove(std::get<PDFObject>(topItem.object)));
|
||||
}
|
||||
|
||||
PDFObjectFactory& PDFObjectFactory::operator<<(WrapEmptyArray)
|
||||
{
|
||||
beginArray();
|
||||
endArray();
|
||||
return *this;
|
||||
}
|
||||
|
||||
PDFObjectFactory& PDFObjectFactory::operator<<(QString textString)
|
||||
{
|
||||
if (!PDFEncoding::canConvertToEncoding(textString, PDFEncoding::Encoding::PDFDoc))
|
||||
@ -234,11 +241,16 @@ void PDFDocumentBuilder::reset()
|
||||
|
||||
void PDFDocumentBuilder::createDocument()
|
||||
{
|
||||
if (!m_storage.getObjects().empty())
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
addObject(PDFObject::createNull());
|
||||
PDFObjectReference catalog = createCatalog();
|
||||
PDFObject trailerDictionary = createTrailerDictionary(catalog);
|
||||
m_storage.updateTrailerDictionary(trailerDictionary);
|
||||
m_storage.setSecurityHandler(PDFSecurityHandlerPointer(new PDFNoneSecurityHandler()));
|
||||
}
|
||||
|
||||
PDFDocument PDFDocumentBuilder::build()
|
||||
@ -257,6 +269,11 @@ void PDFDocumentBuilder::mergeTo(PDFObjectReference reference, PDFObject object)
|
||||
m_storage.setObject(reference, PDFObjectManipulator::merge(m_storage.getObject(reference), qMove(object), PDFObjectManipulator::RemoveNullObjects));
|
||||
}
|
||||
|
||||
void PDFDocumentBuilder::appendTo(PDFObjectReference reference, PDFObject object)
|
||||
{
|
||||
m_storage.setObject(reference, PDFObjectManipulator::merge(m_storage.getObject(reference), qMove(object), PDFObjectManipulator::ConcatenateArrays));
|
||||
}
|
||||
|
||||
QRectF PDFDocumentBuilder::getPopupWindowRect(const QRectF& rectangle) const
|
||||
{
|
||||
return rectangle.translated(rectangle.width() * 1.25, 0);
|
||||
@ -267,6 +284,37 @@ QString PDFDocumentBuilder::getProducerString() const
|
||||
return PDF_LIBRARY_NAME;
|
||||
}
|
||||
|
||||
PDFObjectReference PDFDocumentBuilder::getPageTreeRoot() const
|
||||
{
|
||||
if (const PDFDictionary* trailerDictionary = getDictionaryFromObject(m_storage.getTrailerDictionary()))
|
||||
{
|
||||
if (const PDFDictionary* catalogDictionary = getDictionaryFromObject(trailerDictionary->get("Root")))
|
||||
{
|
||||
PDFObject pagesRoot = catalogDictionary->get("Pages");
|
||||
if (pagesRoot.isReference())
|
||||
{
|
||||
return pagesRoot.getReference();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return PDFObjectReference();
|
||||
}
|
||||
|
||||
PDFInteger PDFDocumentBuilder::getPageTreeRootChildCount() const
|
||||
{
|
||||
if (const PDFDictionary* pageTreeRootDictionary = getDictionaryFromObject(getObjectByReference(getPageTreeRoot())))
|
||||
{
|
||||
PDFObject childCountObject = getObject(pageTreeRootDictionary->get("Count"));
|
||||
if (childCountObject.isInt())
|
||||
{
|
||||
return childCountObject.getInteger();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* START GENERATED CODE */
|
||||
|
||||
PDFObjectReference PDFDocumentBuilder::createAnnotationSquare(PDFObjectReference page,
|
||||
@ -369,6 +417,9 @@ PDFObjectReference PDFDocumentBuilder::createCatalog()
|
||||
objectBuilder.beginDictionaryItem("Type");
|
||||
objectBuilder << WrapName("Catalog");
|
||||
objectBuilder.endDictionaryItem();
|
||||
objectBuilder.beginDictionaryItem("Pages");
|
||||
objectBuilder << createCatalogPageTreeRoot();
|
||||
objectBuilder.endDictionaryItem();
|
||||
objectBuilder.endDictionary();
|
||||
PDFObjectReference catalogReference = addObject(objectBuilder.takeObject());
|
||||
return catalogReference;
|
||||
@ -429,6 +480,58 @@ void PDFDocumentBuilder::updateTrailerDictionary(PDFInteger objectCount)
|
||||
}
|
||||
|
||||
|
||||
PDFObjectReference PDFDocumentBuilder::appendPage(QRectF mediaBox)
|
||||
{
|
||||
PDFObjectFactory objectBuilder;
|
||||
|
||||
objectBuilder.beginDictionary();
|
||||
objectBuilder.beginDictionaryItem("Type");
|
||||
objectBuilder << WrapName("Page");
|
||||
objectBuilder.endDictionaryItem();
|
||||
objectBuilder.beginDictionaryItem("Parent");
|
||||
objectBuilder << getPageTreeRoot();
|
||||
objectBuilder.endDictionaryItem();
|
||||
objectBuilder.beginDictionary();
|
||||
objectBuilder.endDictionary();
|
||||
objectBuilder.beginDictionaryItem("MediaBox");
|
||||
objectBuilder << mediaBox;
|
||||
objectBuilder.endDictionaryItem();
|
||||
objectBuilder.endDictionary();
|
||||
PDFObjectReference pageReference = addObject(objectBuilder.takeObject());
|
||||
objectBuilder.beginDictionary();
|
||||
objectBuilder.beginDictionaryItem("Kids");
|
||||
objectBuilder << std::initializer_list<PDFObjectReference>{ pageReference };
|
||||
objectBuilder.endDictionaryItem();
|
||||
objectBuilder.beginDictionaryItem("Count");
|
||||
objectBuilder << getPageTreeRootChildCount() + 1;
|
||||
objectBuilder.endDictionaryItem();
|
||||
objectBuilder.endDictionary();
|
||||
PDFObject updatedTreeRoot = objectBuilder.takeObject();
|
||||
appendTo(getPageTreeRoot(), updatedTreeRoot);
|
||||
return pageReference;
|
||||
}
|
||||
|
||||
|
||||
PDFObjectReference PDFDocumentBuilder::createCatalogPageTreeRoot()
|
||||
{
|
||||
PDFObjectFactory objectBuilder;
|
||||
|
||||
objectBuilder.beginDictionary();
|
||||
objectBuilder.beginDictionaryItem("Type");
|
||||
objectBuilder << WrapName("Pages");
|
||||
objectBuilder.endDictionaryItem();
|
||||
objectBuilder.beginDictionaryItem("Kids");
|
||||
objectBuilder << WrapEmptyArray();
|
||||
objectBuilder.endDictionaryItem();
|
||||
objectBuilder.beginDictionaryItem("Count");
|
||||
objectBuilder << 0;
|
||||
objectBuilder.endDictionaryItem();
|
||||
objectBuilder.endDictionary();
|
||||
PDFObjectReference pageTreeRoot = addObject(objectBuilder.takeObject());
|
||||
return pageTreeRoot;
|
||||
}
|
||||
|
||||
|
||||
/* END GENERATED CODE */
|
||||
|
||||
} // namespace pdf
|
||||
|
@ -47,6 +47,7 @@ struct WrapAnnotationColor
|
||||
};
|
||||
|
||||
struct WrapCurrentDateTime { };
|
||||
struct WrapEmptyArray { };
|
||||
|
||||
/// Factory for creating various PDF objects, such as simple objects,
|
||||
/// dictionaries, arrays etc.
|
||||
@ -75,6 +76,7 @@ public:
|
||||
PDFObjectFactory& operator<<(WrapCurrentDateTime);
|
||||
PDFObjectFactory& operator<<(WrapAnnotationColor color);
|
||||
PDFObjectFactory& operator<<(QString textString);
|
||||
PDFObjectFactory& operator<<(WrapEmptyArray);
|
||||
|
||||
/// Treat containers - write them as array
|
||||
template<typename Container, typename ValueType = decltype(*std::begin(std::declval<Container>()))>
|
||||
@ -141,7 +143,7 @@ private:
|
||||
std::vector<Item> m_items;
|
||||
};
|
||||
|
||||
class PDFDocumentBuilder
|
||||
class PDFFORQTLIBSHARED_EXPORT PDFDocumentBuilder
|
||||
{
|
||||
public:
|
||||
/// Creates a new blank document (with no pages)
|
||||
@ -158,8 +160,24 @@ public:
|
||||
/// is edited at call of this function, then it is lost.
|
||||
void createDocument();
|
||||
|
||||
/// Builds a new document. This function can throw exceptions,
|
||||
/// if document being built was invalid.
|
||||
PDFDocument build();
|
||||
|
||||
/// If object is reference, the dereference attempt is performed
|
||||
/// and object is returned. If it is not a reference, then self
|
||||
/// is returned. If dereference attempt fails, then null object
|
||||
/// is returned (no exception is thrown).
|
||||
const PDFObject& getObject(const PDFObject& object) const;
|
||||
|
||||
/// Returns dictionary from an object. If object is not a dictionary,
|
||||
/// then nullptr is returned (no exception is thrown).
|
||||
const PDFDictionary* getDictionaryFromObject(const PDFObject& object) const;
|
||||
|
||||
/// Returns object by reference. If dereference attempt fails, then null object
|
||||
/// is returned (no exception is thrown).
|
||||
const PDFObject& getObjectByReference(PDFObjectReference reference) const;
|
||||
|
||||
/* START GENERATED CODE */
|
||||
|
||||
/// Square annotation displays rectangle (or square). When opened, they display pop-up window
|
||||
@ -216,18 +234,67 @@ public:
|
||||
void updateTrailerDictionary(PDFInteger objectCount);
|
||||
|
||||
|
||||
/// Appends a new page after last page.
|
||||
/// \param mediaBox Media box of the page (size of paper)
|
||||
PDFObjectReference appendPage(QRectF mediaBox);
|
||||
|
||||
|
||||
/// Creates page tree root for the catalog. This function is only called when new document is being
|
||||
/// created. Do not call this function manually.
|
||||
PDFObjectReference createCatalogPageTreeRoot();
|
||||
|
||||
|
||||
/* END GENERATED CODE */
|
||||
|
||||
private:
|
||||
PDFObjectReference addObject(PDFObject object);
|
||||
void mergeTo(PDFObjectReference reference, PDFObject object);
|
||||
void appendTo(PDFObjectReference reference, PDFObject object);
|
||||
QRectF getPopupWindowRect(const QRectF& rectangle) const;
|
||||
QString getProducerString() const;
|
||||
PDFObjectReference getPageTreeRoot() const;
|
||||
PDFInteger getPageTreeRootChildCount() const;
|
||||
|
||||
PDFObjectStorage m_storage;
|
||||
PDFVersion m_version;
|
||||
};
|
||||
|
||||
// Implementation
|
||||
|
||||
inline
|
||||
const PDFObject& PDFDocumentBuilder::getObject(const PDFObject& object) const
|
||||
{
|
||||
if (object.isReference())
|
||||
{
|
||||
// Try to dereference the object
|
||||
return m_storage.getObject(object.getReference());
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
inline
|
||||
const PDFDictionary* PDFDocumentBuilder::getDictionaryFromObject(const PDFObject& object) const
|
||||
{
|
||||
const PDFObject& dereferencedObject = getObject(object);
|
||||
if (dereferencedObject.isDictionary())
|
||||
{
|
||||
return dereferencedObject.getDictionary();
|
||||
}
|
||||
else if (dereferencedObject.isStream())
|
||||
{
|
||||
return dereferencedObject.getStream()->getDictionary();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
inline
|
||||
const PDFObject& PDFDocumentBuilder::getObjectByReference(PDFObjectReference reference) const
|
||||
{
|
||||
return m_storage.getObject(reference);
|
||||
}
|
||||
|
||||
} // namespace pdf
|
||||
|
||||
#endif // PDFDOCUMENTBUILDER_H
|
||||
|
299
PdfForQtLib/sources/pdfdocumentwriter.cpp
Normal file
299
PdfForQtLib/sources/pdfdocumentwriter.cpp
Normal file
@ -0,0 +1,299 @@
|
||||
// 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 "pdfdocumentwriter.h"
|
||||
#include "pdfconstants.h"
|
||||
#include "pdfvisitor.h"
|
||||
#include "pdfparser.h"
|
||||
|
||||
#include <QFile>
|
||||
|
||||
namespace pdf
|
||||
{
|
||||
|
||||
class PDFWriteObjectVisitor : public PDFAbstractVisitor
|
||||
{
|
||||
public:
|
||||
explicit PDFWriteObjectVisitor(QIODevice* device) :
|
||||
m_device(device)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
virtual void visitNull() override;
|
||||
virtual void visitBool(bool value) override;
|
||||
virtual void visitInt(PDFInteger value) override;
|
||||
virtual void visitReal(PDFReal value) override;
|
||||
virtual void visitString(const PDFString* string) override;
|
||||
virtual void visitName(const PDFString* name) override;
|
||||
virtual void visitArray(const PDFArray* array) override;
|
||||
virtual void visitDictionary(const PDFDictionary* dictionary) override;
|
||||
virtual void visitStream(const PDFStream* stream) override;
|
||||
virtual void visitReference(const PDFObjectReference reference) override;
|
||||
|
||||
PDFObject getDecryptedObject();
|
||||
|
||||
private:
|
||||
void writeName(const QByteArray& string);
|
||||
|
||||
QIODevice* m_device;
|
||||
};
|
||||
|
||||
void PDFWriteObjectVisitor::visitNull()
|
||||
{
|
||||
m_device->write("null ");
|
||||
}
|
||||
|
||||
void PDFWriteObjectVisitor::visitBool(bool value)
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
m_device->write("true ");
|
||||
}
|
||||
else
|
||||
{
|
||||
m_device->write("false ");
|
||||
}
|
||||
}
|
||||
|
||||
void PDFWriteObjectVisitor::visitInt(PDFInteger value)
|
||||
{
|
||||
m_device->write(QString::number(value).toLatin1());
|
||||
m_device->write(" ");
|
||||
}
|
||||
|
||||
void PDFWriteObjectVisitor::visitReal(PDFReal value)
|
||||
{
|
||||
// Jakub Melka: we use 5 digits, because they are specified
|
||||
// in PDF 1.7 specification, appendix C, Table C.1, where it is defined,
|
||||
// that number of significant digits of precision is 5.
|
||||
m_device->write(QString::number(value, 'f', 5).toLatin1());
|
||||
m_device->write(" ");
|
||||
}
|
||||
|
||||
void PDFWriteObjectVisitor::visitString(const PDFString* string)
|
||||
{
|
||||
const QByteArray& data = string->getString();
|
||||
if (data.indexOf('(') != -1 ||
|
||||
data.indexOf(')') != -1 ||
|
||||
data.indexOf('\\') != -1)
|
||||
{
|
||||
m_device->write("<");
|
||||
m_device->write(data.toHex());
|
||||
m_device->write(">");
|
||||
}
|
||||
else
|
||||
{
|
||||
m_device->write("(");
|
||||
m_device->write(data);
|
||||
m_device->write(")");
|
||||
}
|
||||
|
||||
m_device->write(" ");
|
||||
}
|
||||
|
||||
void PDFWriteObjectVisitor::writeName(const QByteArray& string)
|
||||
{
|
||||
m_device->write("/");
|
||||
|
||||
for (const char character : string)
|
||||
{
|
||||
if (PDFLexicalAnalyzer::isRegular(character))
|
||||
{
|
||||
m_device->write(&character, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_device->write("#");
|
||||
m_device->write(QByteArray(&character, 1).toHex());
|
||||
}
|
||||
}
|
||||
|
||||
m_device->write(" ");
|
||||
}
|
||||
|
||||
void PDFWriteObjectVisitor::visitName(const PDFString* name)
|
||||
{
|
||||
writeName(name->getString());
|
||||
}
|
||||
|
||||
void PDFWriteObjectVisitor::visitArray(const PDFArray* array)
|
||||
{
|
||||
m_device->write("[ ");
|
||||
acceptArray(array);
|
||||
m_device->write("] ");
|
||||
}
|
||||
|
||||
void PDFWriteObjectVisitor::visitDictionary(const PDFDictionary* dictionary)
|
||||
{
|
||||
m_device->write("<< ");
|
||||
|
||||
for (size_t i = 0, count = dictionary->getCount(); i < count; ++i)
|
||||
{
|
||||
writeName(dictionary->getKey(i));
|
||||
dictionary->getValue(i).accept(this);
|
||||
}
|
||||
|
||||
m_device->write(">> ");
|
||||
}
|
||||
|
||||
void PDFWriteObjectVisitor::visitStream(const PDFStream* stream)
|
||||
{
|
||||
visitDictionary(stream->getDictionary());
|
||||
|
||||
m_device->write("stream");
|
||||
m_device->write("\x0D\x0A");
|
||||
m_device->write(*stream->getContent());
|
||||
m_device->write("\x0D\x0A");
|
||||
m_device->write("endstream");
|
||||
}
|
||||
|
||||
void PDFWriteObjectVisitor::visitReference(const PDFObjectReference reference)
|
||||
{
|
||||
visitInt(reference.objectNumber);
|
||||
visitInt(reference.generation);
|
||||
m_device->write("R ");
|
||||
}
|
||||
|
||||
PDFOperationResult PDFDocumentWriter::write(const QString& fileName, const PDFDocument* document)
|
||||
{
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
PDFOperationResult PDFDocumentWriter::write(QIODevice* device, const PDFDocument* document)
|
||||
{
|
||||
if (!device->isWritable())
|
||||
{
|
||||
return tr("Device is not writable.");
|
||||
}
|
||||
|
||||
const PDFObjectStorage& storage = document->getStorage();
|
||||
const PDFObjectStorage::PDFObjects& objects = storage.getObjects();
|
||||
const size_t objectCount = objects.size();
|
||||
if (storage.getSecurityHandler()->getMode() != EncryptionMode::None)
|
||||
{
|
||||
return tr("Writing of encrypted documents is not supported.");
|
||||
}
|
||||
|
||||
// Write header
|
||||
PDFVersion version = document->getInfo()->version;
|
||||
device->write(QString("%PDF-%1.%2").arg(version.major).arg(version.minor).toLatin1());
|
||||
writeCRLF(device);
|
||||
device->write("% PDF producer: ");
|
||||
device->write(PDF_LIBRARY_NAME);
|
||||
writeCRLF(device);
|
||||
writeCRLF(device);
|
||||
writeCRLF(device);
|
||||
|
||||
// Write objects
|
||||
std::vector<PDFInteger> offsets(objectCount, -1);
|
||||
for (size_t i = 0; i < objectCount; ++i)
|
||||
{
|
||||
const PDFObjectStorage::Entry& entry = objects[i];
|
||||
if (entry.object.isNull())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Jakub Melka: we must mark actual position of object
|
||||
offsets[i] = device->pos();
|
||||
|
||||
PDFWriteObjectVisitor visitor(device);
|
||||
writeObjectHeader(device, PDFObjectReference(i, entry.generation));
|
||||
entry.object.accept(&visitor);
|
||||
writeObjectFooter(device);
|
||||
}
|
||||
|
||||
// Write cross-reference table
|
||||
PDFInteger xrefOffset = device->pos();
|
||||
device->write("xref");
|
||||
writeCRLF(device);
|
||||
device->write(QString("0 %1").arg(objectCount).toLatin1());
|
||||
writeCRLF(device);
|
||||
|
||||
for (size_t i = 0; i < objectCount; ++i)
|
||||
{
|
||||
const PDFObjectStorage::Entry& entry = objects[i];
|
||||
PDFInteger generation = entry.generation;
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
generation = 65535;
|
||||
}
|
||||
|
||||
PDFInteger offset = offsets[i];
|
||||
if (offset == -1)
|
||||
{
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
QString offsetString = QString::number(offset).rightJustified(10, QChar('0'), true);
|
||||
QString generationString = QString::number(generation).rightJustified(5, QChar('0'), true);
|
||||
|
||||
device->write(offsetString.toLatin1());
|
||||
device->write(" ");
|
||||
device->write(generationString.toLatin1());
|
||||
device->write(" ");
|
||||
device->write(entry.object.isNull() ? "f" : "n");
|
||||
writeCRLF(device);
|
||||
}
|
||||
device->write("trailer");
|
||||
writeCRLF(device);
|
||||
PDFWriteObjectVisitor trailerVisitor(device);
|
||||
storage.getTrailerDictionary().accept(&trailerVisitor);
|
||||
writeCRLF(device);
|
||||
device->write("startxref");
|
||||
writeCRLF(device);
|
||||
device->write(QString::number(xrefOffset).toLatin1());
|
||||
writeCRLF(device);
|
||||
|
||||
// Write footer
|
||||
device->write("%%EOF");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void PDFDocumentWriter::writeCRLF(QIODevice* device)
|
||||
{
|
||||
device->write("\x0D\x0A");
|
||||
}
|
||||
|
||||
void PDFDocumentWriter::writeObjectHeader(QIODevice* device, PDFObjectReference reference)
|
||||
{
|
||||
QString objectHeader = QString("%1 %2 obj").arg(QString::number(reference.objectNumber)).arg(QString::number(reference.generation));
|
||||
device->write(objectHeader.toLatin1());
|
||||
writeCRLF(device);
|
||||
}
|
||||
|
||||
void PDFDocumentWriter::writeObjectFooter(QIODevice* device)
|
||||
{
|
||||
device->write("endobj");
|
||||
writeCRLF(device);
|
||||
}
|
||||
|
||||
} // namespace pdf
|
57
PdfForQtLib/sources/pdfdocumentwriter.h
Normal file
57
PdfForQtLib/sources/pdfdocumentwriter.h
Normal file
@ -0,0 +1,57 @@
|
||||
// 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 PDFDOCUMENTWRITER_H
|
||||
#define PDFDOCUMENTWRITER_H
|
||||
|
||||
#include "pdfdocument.h"
|
||||
#include "pdfprogress.h"
|
||||
#include "pdfutils.h"
|
||||
|
||||
#include <QIODevice>
|
||||
|
||||
namespace pdf
|
||||
{
|
||||
|
||||
/// Class used for writing PDF documents to the desired target device (or file,
|
||||
/// buffer, etc.). If writing is not successful, then error message is returned.
|
||||
class PDFFORQTLIBSHARED_EXPORT PDFDocumentWriter
|
||||
{
|
||||
Q_DECLARE_TR_FUNCTIONS(pdf::PDFDocumentWriter)
|
||||
|
||||
public:
|
||||
explicit inline PDFDocumentWriter(PDFProgress* progress) :
|
||||
m_progress(progress)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
PDFOperationResult write(const QString& fileName, const PDFDocument* document);
|
||||
PDFOperationResult write(QIODevice* device, const PDFDocument* document);
|
||||
|
||||
private:
|
||||
void writeCRLF(QIODevice* device);
|
||||
void writeObjectHeader(QIODevice* device, PDFObjectReference reference);
|
||||
void writeObjectFooter(QIODevice* device);
|
||||
|
||||
/// Progress indicator
|
||||
PDFProgress* m_progress;
|
||||
};
|
||||
|
||||
} // namespace pdf
|
||||
|
||||
#endif // PDFDOCUMENTWRITER_H
|
@ -15,7 +15,6 @@
|
||||
// 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 PDFUTILS_H
|
||||
#define PDFUTILS_H
|
||||
|
||||
@ -497,6 +496,33 @@ static inline bool isFuzzyComparedPointsSame(const QPointF& p1, const QPointF& p
|
||||
return squaredDistance < squaredTolerance;
|
||||
}
|
||||
|
||||
/// Storage for result of some operation. Stores, if operation was successful, or not and
|
||||
/// also error message, why operation has failed. Can be converted explicitly to bool.
|
||||
class PDFOperationResult
|
||||
{
|
||||
public:
|
||||
inline PDFOperationResult(bool success) :
|
||||
m_success(success)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
inline PDFOperationResult(QString message) :
|
||||
m_success(false),
|
||||
m_errorMessage(qMove(message))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
explicit operator bool() const { return m_success; }
|
||||
|
||||
const QString& getErrorMessage() const { return m_errorMessage; }
|
||||
|
||||
private:
|
||||
bool m_success;
|
||||
QString m_errorMessage;
|
||||
};
|
||||
|
||||
} // namespace pdf
|
||||
|
||||
#endif // PDFUTILS_H
|
||||
|
@ -340,6 +340,13 @@ return annotationObject;</property>
|
||||
<property name="objectType">DictionaryItemSimple</property>
|
||||
<property name="value">WrapName("Catalog")</property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="dictionaryItemName">Pages</property>
|
||||
<property name="objectType">DictionaryItemSimple</property>
|
||||
<property name="value">createCatalogPageTreeRoot()</property>
|
||||
</QObject>
|
||||
</property>
|
||||
<property name="dictionaryItemName"></property>
|
||||
<property name="objectType">Dictionary</property>
|
||||
@ -553,5 +560,170 @@ return annotationObject;</property>
|
||||
<property name="functionDescription">This function is used to update trailer dictionary. Must be called each time the final document is being built.</property>
|
||||
<property name="returnType">_void</property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedFunction">
|
||||
<property name="objectName"></property>
|
||||
<property name="items">
|
||||
<QObject class="codegen::GeneratedAction">
|
||||
<property name="objectName"></property>
|
||||
<property name="items">
|
||||
<QObject class="codegen::GeneratedParameter">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="parameterName">mediaBox</property>
|
||||
<property name="parameterType">_QRectF</property>
|
||||
<property name="parameterDescription">Media box of the page (size of paper)</property>
|
||||
</QObject>
|
||||
</property>
|
||||
<property name="actionType">Parameters</property>
|
||||
<property name="variableName"></property>
|
||||
<property name="variableType">_void</property>
|
||||
<property name="code"></property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedAction">
|
||||
<property name="objectName"></property>
|
||||
<property name="items">
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items">
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="dictionaryItemName">Type</property>
|
||||
<property name="objectType">DictionaryItemSimple</property>
|
||||
<property name="value">WrapName("Page")</property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="dictionaryItemName">Parent</property>
|
||||
<property name="objectType">DictionaryItemSimple</property>
|
||||
<property name="value">getPageTreeRoot()</property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="dictionaryItemName"></property>
|
||||
<property name="objectType">Dictionary</property>
|
||||
<property name="value"></property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="dictionaryItemName">MediaBox</property>
|
||||
<property name="objectType">DictionaryItemSimple</property>
|
||||
<property name="value">mediaBox</property>
|
||||
</QObject>
|
||||
</property>
|
||||
<property name="dictionaryItemName"></property>
|
||||
<property name="objectType">Dictionary</property>
|
||||
<property name="value"></property>
|
||||
</QObject>
|
||||
</property>
|
||||
<property name="actionType">CreateObject</property>
|
||||
<property name="variableName">pageReference</property>
|
||||
<property name="variableType">_PDFObjectReference</property>
|
||||
<property name="code"></property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedAction">
|
||||
<property name="objectName"></property>
|
||||
<property name="items">
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items">
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="dictionaryItemName">Kids</property>
|
||||
<property name="objectType">DictionaryItemSimple</property>
|
||||
<property name="value">std::initializer_list<PDFObjectReference>{ pageReference }</property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="dictionaryItemName">Count</property>
|
||||
<property name="objectType">DictionaryItemSimple</property>
|
||||
<property name="value">getPageTreeRootChildCount() + 1</property>
|
||||
</QObject>
|
||||
</property>
|
||||
<property name="dictionaryItemName"></property>
|
||||
<property name="objectType">Dictionary</property>
|
||||
<property name="value"></property>
|
||||
</QObject>
|
||||
</property>
|
||||
<property name="actionType">CreateObject</property>
|
||||
<property name="variableName">updatedTreeRoot</property>
|
||||
<property name="variableType">_PDFObject</property>
|
||||
<property name="code"></property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedAction">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="actionType">Code</property>
|
||||
<property name="variableName"></property>
|
||||
<property name="variableType">_void</property>
|
||||
<property name="code">appendTo(getPageTreeRoot(), updatedTreeRoot);
|
||||
return pageReference;</property>
|
||||
</QObject>
|
||||
</property>
|
||||
<property name="functionType">Structure</property>
|
||||
<property name="functionName">appendPage</property>
|
||||
<property name="functionDescription">Appends a new page after last page.</property>
|
||||
<property name="returnType">_PDFObjectReference</property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedFunction">
|
||||
<property name="objectName"></property>
|
||||
<property name="items">
|
||||
<QObject class="codegen::GeneratedAction">
|
||||
<property name="objectName"></property>
|
||||
<property name="items">
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items">
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="dictionaryItemName">Type</property>
|
||||
<property name="objectType">DictionaryItemSimple</property>
|
||||
<property name="value">WrapName("Pages")</property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="dictionaryItemName">Kids</property>
|
||||
<property name="objectType">DictionaryItemSimple</property>
|
||||
<property name="value">WrapEmptyArray()</property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedPDFObject">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="dictionaryItemName">Count</property>
|
||||
<property name="objectType">DictionaryItemSimple</property>
|
||||
<property name="value">0</property>
|
||||
</QObject>
|
||||
</property>
|
||||
<property name="dictionaryItemName"></property>
|
||||
<property name="objectType">Dictionary</property>
|
||||
<property name="value"></property>
|
||||
</QObject>
|
||||
</property>
|
||||
<property name="actionType">CreateObject</property>
|
||||
<property name="variableName">pageTreeRoot</property>
|
||||
<property name="variableType">_PDFObjectReference</property>
|
||||
<property name="code"></property>
|
||||
</QObject>
|
||||
<QObject class="codegen::GeneratedAction">
|
||||
<property name="objectName"></property>
|
||||
<property name="items"/>
|
||||
<property name="actionType">Code</property>
|
||||
<property name="variableName"></property>
|
||||
<property name="variableType">_void</property>
|
||||
<property name="code">return pageTreeRoot;</property>
|
||||
</QObject>
|
||||
</property>
|
||||
<property name="functionType">Structure</property>
|
||||
<property name="functionName">createCatalogPageTreeRoot</property>
|
||||
<property name="functionDescription">Creates page tree root for the catalog. This function is only called when new document is being created. Do not call this function manually.</property>
|
||||
<property name="returnType">_PDFObjectReference</property>
|
||||
</QObject>
|
||||
</property>
|
||||
</root>
|
||||
|
Loading…
x
Reference in New Issue
Block a user