filter/qa/pdf.cxx               |   44 ++++++++++++++++++++++++++++++++++++++++
 filter/source/pdf/pdfexport.cxx |   11 +++++++++-
 filter/source/pdf/pdfexport.hxx |    1 
 3 files changed, 55 insertions(+), 1 deletion(-)

New commits:
commit 21c4749d0205d1ba90494edc2527ff9d11f86f87
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Thu Nov 17 13:11:27 2022 +0100
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Thu Nov 17 14:50:07 2022 +0100

    Related: tdf#54053 PDF export: add UNO API to customize the watermark color
    
    PDF export has a watermark feature, but its color is hardcoded to light
    green, which won't fit all documents.
    
    On the other hand, the input from Heiko in
    <https://bugs.documentfoundation.org/show_bug.cgi?id=54053#c12> is to
    keep the dialog clean, so adding one more UI option is not great.
    
    Fix the problem by only adding the option at an UNO API level for now,
    this relaxes the hardcoded color without cluttering the UI.
    
    Also available on the cmdline for e.g. 0xff0000 / red color:
    
    soffice --convert-to 
pdf:writer_pdf_Export:'{"Watermark":{"type":"string","value":"draft"}, 
"WatermarkColor":{"type":"long","value":"16711680"}}' test.odt
    
    Change-Id: I38a3650df86fd34dfc09e3a5643511baa371aa83
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/142835
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins

diff --git a/filter/qa/pdf.cxx b/filter/qa/pdf.cxx
index 601f62a16bdb..8b9b0d4db788 100644
--- a/filter/qa/pdf.cxx
+++ b/filter/qa/pdf.cxx
@@ -143,6 +143,50 @@ CPPUNIT_TEST_FIXTURE(Test, testPdfDecompositionSize)
     CPPUNIT_ASSERT_DOUBLES_EQUAL(9437, aRange.getWidth(), 20.0);
 #endif
 }
+
+CPPUNIT_TEST_FIXTURE(Test, testWatermarkColor)
+{
+    // Given an empty Writer document:
+    std::shared_ptr<vcl::pdf::PDFium> pPDFium = vcl::pdf::PDFiumLibrary::get();
+    if (!pPDFium)
+        return;
+    mxComponent.set(loadFromDesktop("private:factory/swriter", 
"com.sun.star.text.TextDocument"));
+
+    // When exporting that as PDF with a red watermark:
+    uno::Reference<css::lang::XMultiServiceFactory> xFactory = 
getMultiServiceFactory();
+    uno::Reference<document::XFilter> xFilter(
+        xFactory->createInstance("com.sun.star.document.PDFFilter"), 
uno::UNO_QUERY);
+    uno::Reference<document::XExporter> xExporter(xFilter, uno::UNO_QUERY);
+    xExporter->setSourceDocument(mxComponent);
+    SvMemoryStream aStream;
+    uno::Reference<io::XOutputStream> xOutputStream(new 
utl::OStreamWrapper(aStream));
+    uno::Sequence<beans::PropertyValue> aFilterData{
+        comphelper::makePropertyValue("Watermark", OUString("X")),
+        comphelper::makePropertyValue("WatermarkColor", 
static_cast<sal_Int32>(0xff0000)),
+    };
+    uno::Sequence<beans::PropertyValue> aDescriptor{
+        comphelper::makePropertyValue("FilterName", 
OUString("writer_pdf_Export")),
+        comphelper::makePropertyValue("FilterData", aFilterData),
+        comphelper::makePropertyValue("OutputStream", xOutputStream),
+    };
+    xFilter->filter(aDescriptor);
+
+    // Then make sure that the watermark color is correct:
+    std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument
+        = pPDFium->openDocument(aStream.GetData(), aStream.GetSize(), 
OString());
+    CPPUNIT_ASSERT(pPdfDocument);
+    std::unique_ptr<vcl::pdf::PDFiumPage> pPage = pPdfDocument->openPage(0);
+    CPPUNIT_ASSERT_EQUAL(1, pPage->getObjectCount());
+    std::unique_ptr<vcl::pdf::PDFiumPageObject> pPageObject = 
pPage->getObject(0);
+    CPPUNIT_ASSERT_EQUAL(1, pPageObject->getFormObjectCount());
+    std::unique_ptr<vcl::pdf::PDFiumPageObject> pFormObject = 
pPageObject->getFormObject(0);
+    Color aFillColor = pFormObject->getFillColor();
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: rgba[ff0000ff]
+    // - Actual  : rgba[00ff00ff]
+    // i.e. the color was the (default) green, not red.
+    CPPUNIT_ASSERT_EQUAL(Color(ColorTransparency, 0xff0000), aFillColor);
+}
 }
 
 CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/filter/source/pdf/pdfexport.cxx b/filter/source/pdf/pdfexport.cxx
index b02c200e7e85..9633919da14f 100644
--- a/filter/source/pdf/pdfexport.cxx
+++ b/filter/source/pdf/pdfexport.cxx
@@ -108,6 +108,7 @@ PDFExport::PDFExport( const Reference< XComponent >& 
rxSrcDoc,
     mbRemoveTransparencies      ( false ),
 
     mbIsRedactMode              ( false ),
+    maWatermarkColor            ( COL_LIGHTGREEN ),
 
     mbHideViewerToolbar         ( false ),
     mbHideViewerMenubar         ( false ),
@@ -559,6 +560,14 @@ bool PDFExport::Export( const OUString& rFile, const 
Sequence< PropertyValue >&
                     rProp.Value >>= mbAddStream;
                 else if ( rProp.Name == "Watermark" )
                     rProp.Value >>= msWatermark;
+                else if ( rProp.Name == "WatermarkColor" )
+                {
+                    sal_Int32 nColor{};
+                    if (rProp.Value >>= nColor)
+                    {
+                        maWatermarkColor = Color(ColorTransparency, nColor);
+                    }
+                }
                 else if ( rProp.Name == "TiledWatermark" )
                     rProp.Value >>= msTiledWatermark;
                 // now all the security related properties...
@@ -1192,7 +1201,7 @@ void PDFExport::ImplWriteWatermark( vcl::PDFWriter& 
rWriter, const Size& rPageSi
     rWriter.Push();
     rWriter.SetMapMode( MapMode( MapUnit::MapPoint ) );
     rWriter.SetFont( aFont );
-    rWriter.SetTextColor( COL_LIGHTGREEN );
+    rWriter.SetTextColor(maWatermarkColor);
     Point aTextPoint;
     tools::Rectangle aTextRect;
     if( rPageSize.Width() > rPageSize.Height() )
diff --git a/filter/source/pdf/pdfexport.hxx b/filter/source/pdf/pdfexport.hxx
index 12d43ada945e..50d3b9dbcb72 100644
--- a/filter/source/pdf/pdfexport.hxx
+++ b/filter/source/pdf/pdfexport.hxx
@@ -73,6 +73,7 @@ private:
     bool                mbIsRedactMode;
 
     OUString            msWatermark;
+    Color               maWatermarkColor;
     OUString            msTiledWatermark;
 
     // these variable are here only to have a location in filter/pdf to set 
the default

Reply via email to