From 8bce1bc1e562bdf53456d8a1a5c2d343e19ae9b6 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sat, 28 Mar 2020 14:25:32 +0100 Subject: [PATCH] Annotation settings functions --- CodeGenerator/codegenerator.h | 3 +- PdfExampleGenerator/pdfexamplesgenerator.cpp | 46 +- PdfForQtLib/sources/pdfannotation.h | 2 + PdfForQtLib/sources/pdfdocumentbuilder.cpp | 212 +++++- PdfForQtLib/sources/pdfdocumentbuilder.h | 89 ++- generated_code_definition.xml | 758 ++++++++++++++++++- 6 files changed, 1062 insertions(+), 48 deletions(-) diff --git a/CodeGenerator/codegenerator.h b/CodeGenerator/codegenerator.h index 838e056..16381f9 100644 --- a/CodeGenerator/codegenerator.h +++ b/CodeGenerator/codegenerator.h @@ -151,7 +151,8 @@ public: _TextAnnotationIcon, _LinkHighlightMode, _TextAlignment, - _AnnotationLineEnding + _AnnotationLineEnding, + _AnnotationBorderStyle }; Q_ENUM(DataType) diff --git a/PdfExampleGenerator/pdfexamplesgenerator.cpp b/PdfExampleGenerator/pdfexamplesgenerator.cpp index 9a41edc..cbb4da8 100644 --- a/PdfExampleGenerator/pdfexamplesgenerator.cpp +++ b/PdfExampleGenerator/pdfexamplesgenerator.cpp @@ -59,7 +59,11 @@ void PDFExamplesGenerator::generateAnnotationsExample() builder.createAnnotationFreeText(page3, QRectF(50, 250, 100, 50), "Title", "Subject", "Toto je horni text", Qt::AlignRight); builder.createAnnotationFreeText(page3, QRectF(250, 50, 100, 50), QRectF(300, 50, 50, 50), "Title", "Subject", "Toto je dolni text", Qt::AlignLeft, QPointF(250, 50), QPointF(300, 100), pdf::AnnotationLineEnding::OpenArrow, pdf::AnnotationLineEnding::ClosedArrow); builder.createAnnotationFreeText(page3, QRectF(250, 150, 100, 50), QRectF(50, 50, 50, 50), "Title", "Subject", "Toto je stredni text", Qt::AlignCenter, QPointF(250, 150), QPointF(300, 200), pdf::AnnotationLineEnding::OpenArrow, pdf::AnnotationLineEnding::ClosedArrow); - builder.createAnnotationFreeText(page3, QRectF(250, 250, 100, 50), QRectF(0, 50, 50, 50), "Title", "Subject", "Toto je horni text", Qt::AlignRight, QPointF(250, 250), QPointF(300, 300), pdf::AnnotationLineEnding::OpenArrow, pdf::AnnotationLineEnding::ClosedArrow); + pdf::PDFObjectReference ref = builder.createAnnotationFreeText(page3, QRectF(250, 250, 100, 50), QRectF(0, 50, 50, 50), "Title", "Subject", "Toto je horni text", Qt::AlignRight, QPointF(250, 250), QPointF(300, 300), pdf::AnnotationLineEnding::OpenArrow, pdf::AnnotationLineEnding::ClosedArrow); + builder.setAnnotationContents(ref, "UPDATED: Horni text"); + builder.setAnnotationTitle(ref, "Updated title"); + builder.setAnnotationSubject(ref, "Updated subject"); + builder.setAnnotationOpacity(ref, 0.5); { pdf::PDFObjectReference page4 = builder.appendPage(QRectF(0, 0, 400, 400)); @@ -149,6 +153,46 @@ void PDFExamplesGenerator::generateAnnotationsExample() builder.createAnnotationSquare(page5, QRectF(50, 150, 50, 50), 3.0, QColor(), Qt::red, "Title2", "Subject2", "Contents - red boundary"); builder.createAnnotationSquare(page5, QRectF(50, 250, 50, 50), 3.0, Qt::green, QColor(), "Title3", "Subject3", "Contents - green filling"); + { + pdf::PDFObjectReference page5 = builder.appendPage(QRectF(0, 0, 400, 400)); + + { + pdf::PDFObjectReference annotation = builder.createAnnotationSquare(page5, QRectF(50, 50, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary"); + builder.setAnnotationBorderStyle(annotation, pdf::PDFAnnotationBorder::Style::Solid, 2.718); + builder.setAnnotationColor(annotation, Qt::black); + } + + { + pdf::PDFObjectReference annotation = builder.createAnnotationSquare(page5, QRectF(50, 150, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary"); + builder.setAnnotationBorderStyle(annotation, pdf::PDFAnnotationBorder::Style::Underline, 2.718); + builder.setAnnotationColor(annotation, Qt::black); + } + + { + pdf::PDFObjectReference annotation = builder.createAnnotationSquare(page5, QRectF(50, 250, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary"); + builder.setAnnotationBorderStyle(annotation, pdf::PDFAnnotationBorder::Style::Inset, 2.718); + builder.setAnnotationColor(annotation, Qt::black); + } + + { + pdf::PDFObjectReference annotation = builder.createAnnotationSquare(page5, QRectF(150, 50, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary"); + builder.setAnnotationBorderStyle(annotation, pdf::PDFAnnotationBorder::Style::Beveled, 2.718); + builder.setAnnotationColor(annotation, Qt::black); + } + + { + pdf::PDFObjectReference annotation = builder.createAnnotationSquare(page5, QRectF(150, 150, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary"); + builder.setAnnotationBorderStyle(annotation, pdf::PDFAnnotationBorder::Style::Dashed, 2.718); + builder.setAnnotationColor(annotation, Qt::black); + } + + { + pdf::PDFObjectReference annotation = builder.createAnnotationSquare(page5, QRectF(150, 250, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary"); + builder.setAnnotationBorderStyle(annotation, pdf::PDFAnnotationBorder::Style::Inset, 2.718); + builder.setAnnotationColor(annotation, Qt::black); + } + } + pdf::PDFObjectReference page6 = builder.appendPage(QRectF(0, 0, 400, 400)); builder.createAnnotationCircle(page6, QRectF(50, 50, 50, 50), 3.0, Qt::green, Qt::red, "Title1", "Subject1", "Contents - green filling, red boundary"); builder.createAnnotationCircle(page6, QRectF(50, 150, 50, 50), 3.0, QColor(), Qt::red, "Title2", "Subject2", "Contents - red boundary"); diff --git a/PdfForQtLib/sources/pdfannotation.h b/PdfForQtLib/sources/pdfannotation.h index cc3dd0e..861d03a 100644 --- a/PdfForQtLib/sources/pdfannotation.h +++ b/PdfForQtLib/sources/pdfannotation.h @@ -134,6 +134,8 @@ private: std::vector m_dashPattern; }; +using AnnotationBorderStyle = PDFAnnotationBorder::Style; + /// Annotation border effect class PDFAnnotationBorderEffect { diff --git a/PdfForQtLib/sources/pdfdocumentbuilder.cpp b/PdfForQtLib/sources/pdfdocumentbuilder.cpp index 02f5327..267b156 100644 --- a/PdfForQtLib/sources/pdfdocumentbuilder.cpp +++ b/PdfForQtLib/sources/pdfdocumentbuilder.cpp @@ -64,6 +64,38 @@ void PDFObjectFactory::endDictionaryItem() std::get(dictionaryItem.object).addEntry(qMove(topItem.itemName), qMove(std::get(topItem.object))); } +PDFObjectFactory& PDFObjectFactory::operator<<(AnnotationBorderStyle style) +{ + switch (style) + { + case AnnotationBorderStyle::Solid: + *this << WrapName("S"); + break; + + case AnnotationBorderStyle::Dashed: + *this << WrapName("D"); + break; + + case AnnotationBorderStyle::Beveled: + *this << WrapName("B"); + break; + + case AnnotationBorderStyle::Inset: + *this << WrapName("I"); + break; + + case AnnotationBorderStyle::Underline: + *this << WrapName("U"); + break; + + default: + Q_ASSERT(false); + break; + } + + return *this; +} + PDFObjectFactory& PDFObjectFactory::operator<<(const QDateTime& dateTime) { addObject(PDFObject::createString(std::make_shared(PDFEncoding::converDateTimeToString(dateTime)))); @@ -1903,6 +1935,170 @@ PDFObject PDFDocumentBuilder::createTrailerDictionary(PDFObjectReference catalog } +void PDFDocumentBuilder::setAnnotationBorder(PDFObjectReference annotation, + PDFReal hRadius, + PDFReal vRadius, + PDFReal width) +{ + PDFObjectFactory objectBuilder; + + objectBuilder.beginDictionary(); + objectBuilder.beginDictionaryItem("Border"); + objectBuilder.beginArray(); + objectBuilder << hRadius; + objectBuilder << vRadius; + objectBuilder << width; + objectBuilder.endArray(); + objectBuilder.endDictionaryItem(); + objectBuilder.endDictionary(); + PDFObject annotationObject = objectBuilder.takeObject(); + mergeTo(annotation, annotationObject); +} + + +void PDFDocumentBuilder::setAnnotationBorderStyle(PDFObjectReference annotation, + AnnotationBorderStyle style, + PDFReal width) +{ + PDFObjectFactory objectBuilder; + + objectBuilder.beginDictionary(); + objectBuilder.beginDictionaryItem("BS"); + objectBuilder.beginDictionary(); + objectBuilder.beginDictionaryItem("W"); + objectBuilder << width; + objectBuilder.endDictionaryItem(); + objectBuilder.beginDictionaryItem("S"); + objectBuilder << style; + objectBuilder.endDictionaryItem(); + objectBuilder.endDictionary(); + objectBuilder.endDictionaryItem(); + objectBuilder.endDictionary(); + PDFObject annotationObject = objectBuilder.takeObject(); + mergeTo(annotation, annotationObject); +} + + +void PDFDocumentBuilder::setAnnotationColor(PDFObjectReference annotation, + QColor color) +{ + PDFObjectFactory objectBuilder; + + objectBuilder.beginDictionary(); + objectBuilder.beginDictionaryItem("C"); + objectBuilder << WrapAnnotationColor(color); + objectBuilder.endDictionaryItem(); + objectBuilder.endDictionary(); + PDFObject annotationObject = objectBuilder.takeObject(); + mergeTo(annotation, annotationObject); +} + + +void PDFDocumentBuilder::setAnnotationContents(PDFObjectReference annotation, + QString contents) +{ + PDFObjectFactory objectBuilder; + + objectBuilder.beginDictionary(); + objectBuilder.beginDictionaryItem("Contents"); + objectBuilder << contents; + objectBuilder.endDictionaryItem(); + objectBuilder.endDictionary(); + PDFObject annotationObject = objectBuilder.takeObject(); + mergeTo(annotation, annotationObject); +} + + +void PDFDocumentBuilder::setAnnotationOpacity(PDFObjectReference annotation, + PDFReal opacity) +{ + PDFObjectFactory objectBuilder; + + objectBuilder.beginDictionary(); + objectBuilder.beginDictionaryItem("CA"); + objectBuilder << opacity; + objectBuilder.endDictionaryItem(); + objectBuilder.endDictionary(); + PDFObject annotationObject = objectBuilder.takeObject(); + mergeTo(annotation, annotationObject); +} + + +void PDFDocumentBuilder::setAnnotationOpenState(PDFObjectReference annotation, + bool isOpen) +{ + PDFObjectFactory objectBuilder; + + objectBuilder.beginDictionary(); + objectBuilder.beginDictionaryItem("Open"); + objectBuilder << isOpen; + objectBuilder.endDictionaryItem(); + objectBuilder.endDictionary(); + PDFObject annotationObject = objectBuilder.takeObject(); + mergeTo(annotation, annotationObject); +} + + +void PDFDocumentBuilder::setAnnotationQuadPoints(PDFObjectReference annotation, + QPolygonF quadrilaterals) +{ + PDFObjectFactory objectBuilder; + + objectBuilder.beginDictionary(); + objectBuilder.beginDictionaryItem("QuadPoints"); + objectBuilder << quadrilaterals; + objectBuilder.endDictionaryItem(); + objectBuilder.endDictionary(); + PDFObject annotationObject = objectBuilder.takeObject(); + mergeTo(annotation, annotationObject); +} + + +void PDFDocumentBuilder::setAnnotationRichText(PDFObjectReference annotation, + QString richText) +{ + PDFObjectFactory objectBuilder; + + objectBuilder.beginDictionary(); + objectBuilder.beginDictionaryItem("RC"); + objectBuilder << richText; + objectBuilder.endDictionaryItem(); + objectBuilder.endDictionary(); + PDFObject annotationObject = objectBuilder.takeObject(); + mergeTo(annotation, annotationObject); +} + + +void PDFDocumentBuilder::setAnnotationSubject(PDFObjectReference annotation, + QString subject) +{ + PDFObjectFactory objectBuilder; + + objectBuilder.beginDictionary(); + objectBuilder.beginDictionaryItem("Subj"); + objectBuilder << subject; + objectBuilder.endDictionaryItem(); + objectBuilder.endDictionary(); + PDFObject annotationObject = objectBuilder.takeObject(); + mergeTo(annotation, annotationObject); +} + + +void PDFDocumentBuilder::setAnnotationTitle(PDFObjectReference annotation, + QString title) +{ + PDFObjectFactory objectBuilder; + + objectBuilder.beginDictionary(); + objectBuilder.beginDictionaryItem("T"); + objectBuilder << title; + objectBuilder.endDictionaryItem(); + objectBuilder.endDictionary(); + PDFObject annotationObject = objectBuilder.takeObject(); + mergeTo(annotation, annotationObject); +} + + void PDFDocumentBuilder::setDocumentAuthor(QString author) { PDFObjectFactory objectBuilder; @@ -2001,6 +2197,14 @@ void PDFDocumentBuilder::setDocumentTitle(QString title) } +void PDFDocumentBuilder::setLanguage(QLocale locale) +{ + PDFObjectFactory objectBuilder; + + setLanguage(locale.name()); +} + + void PDFDocumentBuilder::setLanguage(QString language) { PDFObjectFactory objectBuilder; @@ -2015,14 +2219,6 @@ void PDFDocumentBuilder::setLanguage(QString language) } -void PDFDocumentBuilder::setLanguage(QLocale locale) -{ - PDFObjectFactory objectBuilder; - - setLanguage(locale.name()); -} - - void PDFDocumentBuilder::updateTrailerDictionary(PDFInteger objectCount) { PDFObjectFactory objectBuilder; diff --git a/PdfForQtLib/sources/pdfdocumentbuilder.h b/PdfForQtLib/sources/pdfdocumentbuilder.h index 06bdfe4..a8af41f 100644 --- a/PdfForQtLib/sources/pdfdocumentbuilder.h +++ b/PdfForQtLib/sources/pdfdocumentbuilder.h @@ -107,6 +107,7 @@ public: PDFObjectFactory& operator<<(AnnotationLineEnding lineEnding); PDFObjectFactory& operator<<(const QPointF& point); PDFObjectFactory& operator<<(const QDateTime& dateTime); + PDFObjectFactory& operator<<(AnnotationBorderStyle style); /// Treat containers - write them as array template()))> @@ -617,6 +618,84 @@ public: PDFObject createTrailerDictionary(PDFObjectReference catalog); + /// Sets annotation border. + /// \param annotation Annotation + /// \param hRadius Horizontal corner radius + /// \param vRadius Vertical corner radius + /// \param width Line width + void setAnnotationBorder(PDFObjectReference annotation, + PDFReal hRadius, + PDFReal vRadius, + PDFReal width); + + + /// Sets annotation border style. + /// \param annotation Annotation + /// \param style Style + /// \param width Width + void setAnnotationBorderStyle(PDFObjectReference annotation, + AnnotationBorderStyle style, + PDFReal width); + + + /// Sets annotation color. + /// \param annotation Annotation + /// \param color Color + void setAnnotationColor(PDFObjectReference annotation, + QColor color); + + + /// Sets annotation contents. + /// \param annotation Annotation + /// \param contents Contents + void setAnnotationContents(PDFObjectReference annotation, + QString contents); + + + /// Sets constant opacity of annotation's graphics. + /// \param annotation Annotation + /// \param opacity Opacity (value must be in range from 0.0 to 1.0) + void setAnnotationOpacity(PDFObjectReference annotation, + PDFReal opacity); + + + /// Sets open state of the annotation. + /// \param annotation Annotation + /// \param isOpen Is annotation opened? + void setAnnotationOpenState(PDFObjectReference annotation, + bool isOpen); + + + /// Sets annotation quadrilaterals. Quadrilaterals are sequence of 4 points, where first two points are + /// on the upper side of quadrilateral, and the last two points are on the lower side of quadrilateral. + /// Quadrilaterals are represented as unclosed polygon with 4 * n vertices. + /// \param annotation Annotation + /// \param quadrilaterals Quadrilaterals + void setAnnotationQuadPoints(PDFObjectReference annotation, + QPolygonF quadrilaterals); + + + /// Sets annotation rich text contents. This function will work only on markup annotations. + /// \param annotation Annotation + /// \param richText Rich text contents + void setAnnotationRichText(PDFObjectReference annotation, + QString richText); + + + /// Sets annotation subject. + /// \param annotation Annotation + /// \param subject Subject + void setAnnotationSubject(PDFObjectReference annotation, + QString subject); + + + /// Sets annotation title. + /// \param annotation Annotation + /// \param title Title + void setAnnotationTitle(PDFObjectReference annotation, + QString title); + + /// Set document author. /// \param author Author void setDocumentAuthor(QString author); @@ -652,6 +731,11 @@ public: void setDocumentTitle(QString title); + /// Set document language. + /// \param locale Locale, from which is language determined + void setLanguage(QLocale locale); + + /// Set document language. /// \param language Document language. It should be a language identifier, as defined in ISO 639 /// and ISO 3166. For example, "en-US", where first two letter means language code (en = @@ -659,11 +743,6 @@ public: void setLanguage(QString language); - /// Set document language. - /// \param locale Locale, from which is language determined - void setLanguage(QLocale locale); - - /// This function is used to update trailer dictionary. Must be called each time the final document is /// being built. /// \param objectCount Number of objects (including empty ones) diff --git a/generated_code_definition.xml b/generated_code_definition.xml index 8e18ace..cb2e786 100644 --- a/generated_code_definition.xml +++ b/generated_code_definition.xml @@ -4486,6 +4486,698 @@ return annotationObject; This function is used to create a new trailer dictionary, when blank document is created. Do not call this function manually. _PDFObject + + + + + + + + + + annotation + _PDFObjectReference + Annotation + + + + + hRadius + _PDFReal + Horizontal corner radius + + + + + vRadius + _PDFReal + Vertical corner radius + + + + + width + _PDFReal + Line width + + + Parameters + + _void + + + + + + + + + + + + + + + + ArraySimple + hRadius;vRadius;width + + + Border + DictionaryItemComplex + + + + + Dictionary + + + + CreateObject + annotationObject + _PDFObject + + + + + + Code + + _void + mergeTo(annotation, annotationObject); + + + Annotations + setAnnotationBorder + Sets annotation border. + _void + + + + + + + + + + + annotation + _PDFObjectReference + Annotation + + + + + style + _AnnotationBorderStyle + Style + + + + + width + _PDFReal + Width + + + Parameters + + _void + + + + + + + + + + + + + + + + + + W + DictionaryItemSimple + width + + + + + S + DictionaryItemSimple + style + + + + Dictionary + + + + BS + DictionaryItemComplex + + + + + Dictionary + + + + CreateObject + annotationObject + _PDFObject + + + + + + Code + + _void + mergeTo(annotation, annotationObject); + + + Annotations + setAnnotationBorderStyle + Sets annotation border style. + _void + + + + + + + + + + + annotation + _PDFObjectReference + Annotation + + + + + color + _QColor + Color + + + Parameters + + _void + + + + + + + + + + + + C + DictionaryItemSimple + WrapAnnotationColor(color) + + + + Dictionary + + + + CreateObject + annotationObject + _PDFObject + + + + + + Code + + _void + mergeTo(annotation, annotationObject); + + + Annotations + setAnnotationColor + Sets annotation color. + _void + + + + + + + + + + + annotation + _PDFObjectReference + Annotation + + + + + contents + _QString + Contents + + + Parameters + + _void + + + + + + + + + + + + Contents + DictionaryItemSimple + contents + + + + Dictionary + + + + CreateObject + annotationObject + _PDFObject + + + + + + Code + + _void + mergeTo(annotation, annotationObject); + + + Annotations + setAnnotationContents + Sets annotation contents. + _void + + + + + + + + + + + annotation + _PDFObjectReference + Annotation + + + + + opacity + _PDFReal + Opacity (value must be in range from 0.0 to 1.0) + + + Parameters + + _void + + + + + + + + + + + + CA + DictionaryItemSimple + opacity + + + + Dictionary + + + + CreateObject + annotationObject + _PDFObject + + + + + + Code + + _void + mergeTo(annotation, annotationObject); + + + Annotations + setAnnotationOpacity + Sets constant opacity of annotation's graphics. + _void + + + + + + + + + + + annotation + _PDFObjectReference + Annotation + + + + + isOpen + _bool + Is annotation opened? + + + Parameters + + _void + + + + + + + + + + + + Open + DictionaryItemSimple + isOpen + + + + Dictionary + + + + CreateObject + annotationObject + _PDFObject + + + + + + Code + + _void + mergeTo(annotation, annotationObject); + + + Annotations + setAnnotationOpenState + Sets open state of the annotation. + _void + + + + + + + + + + + annotation + _PDFObjectReference + Annotation + + + + + quadrilaterals + _QPolygonF + Quadrilaterals + + + Parameters + + _void + + + + + + + + + + + + QuadPoints + DictionaryItemSimple + quadrilaterals + + + + Dictionary + + + + CreateObject + annotationObject + _PDFObject + + + + + + Code + + _void + mergeTo(annotation, annotationObject); + + + Annotations + setAnnotationQuadPoints + Sets annotation quadrilaterals. Quadrilaterals are sequence of 4 points, where first two points are on the upper side of quadrilateral, and the last two points are on the lower side of quadrilateral. Quadrilaterals are represented as unclosed polygon with 4 * n vertices. + _void + + + + + + + + + + + annotation + _PDFObjectReference + Annotation + + + + + richText + _QString + Rich text contents + + + Parameters + + _void + + + + + + + + + + + + RC + DictionaryItemSimple + richText + + + + Dictionary + + + + CreateObject + annotationObject + _PDFObject + + + + + + Code + + _void + mergeTo(annotation, annotationObject); + + + Annotations + setAnnotationRichText + Sets annotation rich text contents. This function will work only on markup annotations. + _void + + + + + + + + + + + annotation + _PDFObjectReference + Annotation + + + + + subject + _QString + Subject + + + Parameters + + _void + + + + + + + + + + + + Subj + DictionaryItemSimple + subject + + + + Dictionary + + + + CreateObject + annotationObject + _PDFObject + + + + + + Code + + _void + mergeTo(annotation, annotationObject); + + + Annotations + setAnnotationSubject + Sets annotation subject. + _void + + + + + + + + + + + annotation + _PDFObjectReference + Annotation + + + + + title + _QString + Title + + + Parameters + + _void + + + + + + + + + + + + T + DictionaryItemSimple + title + + + + Dictionary + + + + CreateObject + annotationObject + _PDFObject + + + + + + Code + + _void + mergeTo(annotation, annotationObject); + + + Annotations + setAnnotationTitle + Sets annotation title. + _void + @@ -4885,6 +5577,39 @@ return annotationObject; Set document title. _void + + + + + + + + + + locale + _QLocale + Locale, from which is language determined + + + Parameters + + _void + + + + + + Code + + _void + setLanguage(locale.name()); + + + Structure + setLanguage + Set document language. + _void + @@ -4942,39 +5667,6 @@ return annotationObject; Set document language. _void - - - - - - - - - - locale - _QLocale - Locale, from which is language determined - - - Parameters - - _void - - - - - - Code - - _void - setLanguage(locale.name()); - - - Structure - setLanguage - Set document language. - _void -