vcl/qa/cppunit/pdfexport/data/pdf-image-annots.odg |binary
 vcl/qa/cppunit/pdfexport/pdfexport.cxx             |   31 +++++++++++++++++++++
 vcl/source/gdi/pdfwriter_impl.cxx                  |   16 +++++++++-
 3 files changed, 45 insertions(+), 2 deletions(-)

New commits:
commit 4ccafbc96b9cf30963060acd443cfc9435b7d5e8
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Wed Mar 9 13:36:27 2022 +0100
Commit:     Andras Timar <andras.ti...@collabora.com>
CommitDate: Wed Mar 9 16:58:20 2022 +0100

    PDF export of PDF images: don't preserve annotations in general
    
    Regression from 33c9bc0225a92f26770f9ef20b252af47853e7b9 (PDF export of
    PDF images: preserve hyperlinks, 2022-01-07), the problem was that we
    want to preserve hyperlinks, but annotations are added by the PDF export
    explicitly, so it isn't a good idea to "preserve" them as well.
    
    Fix the problem by going back to the old behavior, except when the
    annotation sub-type is /Link.
    
    This keeps hyperlinks working but doesn't lead to duplicated comments
    when re-exporting an image + adding comments explicitly.
    
    Conflicts:
            vcl/qa/cppunit/pdfexport/pdfexport.cxx
    
    Change-Id: I910990da59bdc1150cc346f1a5471cb6da55dd2c
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131262
    Tested-by: Aron Budea <aron.bu...@collabora.com>
    Reviewed-by: Andras Timar <andras.ti...@collabora.com>

diff --git a/vcl/qa/cppunit/pdfexport/data/pdf-image-annots.odg 
b/vcl/qa/cppunit/pdfexport/data/pdf-image-annots.odg
new file mode 100644
index 000000000000..6dee0145c536
Binary files /dev/null and b/vcl/qa/cppunit/pdfexport/data/pdf-image-annots.odg 
differ
diff --git a/vcl/qa/cppunit/pdfexport/pdfexport.cxx 
b/vcl/qa/cppunit/pdfexport/pdfexport.cxx
index ea67d5530539..fc7103fede47 100644
--- a/vcl/qa/cppunit/pdfexport/pdfexport.cxx
+++ b/vcl/qa/cppunit/pdfexport/pdfexport.cxx
@@ -149,6 +149,7 @@ public:
     void testMultiPagePDF();
     void testPdfImageRotate180();
     void testPdfImageHyperlink();
+    void testPdfImageAnnots();
 
 
     CPPUNIT_TEST_SUITE(PdfExportTest);
@@ -194,6 +195,7 @@ public:
     CPPUNIT_TEST(testMultiPagePDF);
     CPPUNIT_TEST(testPdfImageRotate180);
     CPPUNIT_TEST(testPdfImageHyperlink);
+    CPPUNIT_TEST(testPdfImageAnnots);
     CPPUNIT_TEST_SUITE_END();
 };
 
@@ -2332,6 +2334,35 @@ void PdfExportTest::testPdfImageHyperlink()
 #endif
 }
 
+void PdfExportTest::testPdfImageAnnots()
+{
+    // Given a document with a PDF image that has 2 comments (popup, text) and 
a hyperlink:
+    OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + 
"pdf-image-annots.odg";
+    mxComponent = loadFromDesktop(aURL);
+    CPPUNIT_ASSERT(mxComponent.is());
+
+    // When saving to PDF:
+    uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
+    utl::MediaDescriptor aMediaDescriptor;
+    aMediaDescriptor["FilterName"] <<= OUString("draw_pdf_Export");
+    xStorable->storeToURL(maTempFile.GetURL(), 
aMediaDescriptor.getAsConstPropertyValueList());
+
+    // Then make sure only the hyperlink is kept, since Draw itself has its 
own comments:
+    SvFileStream aFile(maTempFile.GetURL(), StreamMode::READ);
+    maMemory.WriteStream(aFile);
+    std::shared_ptr<vcl::pdf::PDFium> pPDFium = vcl::pdf::PDFiumLibrary::get();
+    std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument
+        = pPDFium->openDocument(maMemory.GetData(), maMemory.GetSize());
+    CPPUNIT_ASSERT(pPdfDocument);
+    CPPUNIT_ASSERT_EQUAL(1, pPdfDocument->getPageCount());
+    std::unique_ptr<vcl::pdf::PDFiumPage> pPdfPage = 
pPdfDocument->openPage(/*nIndex=*/0);
+    CPPUNIT_ASSERT(pPdfPage);
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: 1
+    // - Actual  : 3
+    // i.e. not only the hyperlink but also the 2 comments were exported, 
leading to duplication.
+    CPPUNIT_ASSERT_EQUAL(1, pPdfPage->getAnnotationCount());
+}
 CPPUNIT_TEST_SUITE_REGISTRATION(PdfExportTest);
 
 }
diff --git a/vcl/source/gdi/pdfwriter_impl.cxx 
b/vcl/source/gdi/pdfwriter_impl.cxx
index 896a910eae51..1d10f377c0d0 100644
--- a/vcl/source/gdi/pdfwriter_impl.cxx
+++ b/vcl/source/gdi/pdfwriter_impl.cxx
@@ -8738,7 +8738,7 @@ void 
PDFWriterImpl::writeReferenceXObject(ReferenceXObjectEmit& rEmit)
             return;
         }
 
-        // Merge page annotations (links, etc) from pPage to our page.
+        // Merge link annotations from pPage to our page.
         std::vector<filter::PDFObjectElement*> aAnnots;
         if (auto pArray = 
dynamic_cast<filter::PDFArrayElement*>(pPage->Lookup("Annots")))
         {
@@ -8756,7 +8756,19 @@ void 
PDFWriterImpl::writeReferenceXObject(ReferenceXObjectEmit& rEmit)
                     continue;
                 }
 
-                // Annotation refers to an object, remember it.
+                auto pType = 
dynamic_cast<filter::PDFNameElement*>(pObject->Lookup("Type"));
+                if (!pType || pType->GetValue() != "Annot")
+                {
+                    continue;
+                }
+
+                auto pSubtype = 
dynamic_cast<filter::PDFNameElement*>(pObject->Lookup("Subtype"));
+                if (!pSubtype || pSubtype->GetValue() != "Link")
+                {
+                    continue;
+                }
+
+                // Reference to a link annotation object, remember it.
                 aAnnots.push_back(pObject);
             }
         }

Reply via email to