include/vcl/pdfwriter.hxx        |    2 -
 sw/qa/core/text/text.cxx         |   49 +++++++++++++++++++++++++++++----------
 sw/qa/inc/swmodeltestbase.hxx    |    2 +
 sw/qa/unit/swmodeltestbase.cxx   |    8 ++++++
 sw/source/core/text/itrform2.cxx |   11 ++++++++
 5 files changed, 59 insertions(+), 13 deletions(-)

New commits:
commit 6b2433f3b66c1f118dabcd40924122deb2d4f204
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Wed Sep 14 08:35:28 2022 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Wed Sep 14 10:08:04 2022 +0200

    sw content controls, dropdown: add PDF export
    
    Map this to vcl::PDFWriter::ListBoxWidget, which uses the /FT/Ch widget,
    i.e. a (dropdown) choice widget.
    
    Also extract a SwModelTestBase::StoreToTempFile() from the various test
    cases that export to a tempfile just by specifying the filter name.
    
    Change-Id: If9ad52ae2553d7199e28188cfdf455353e202ccd
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/139904
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins

diff --git a/include/vcl/pdfwriter.hxx b/include/vcl/pdfwriter.hxx
index 8860675de688..34da05a642da 100644
--- a/include/vcl/pdfwriter.hxx
+++ b/include/vcl/pdfwriter.hxx
@@ -408,7 +408,7 @@ public:
         }
     };
 
-    struct ListBoxWidget final : public AnyWidget
+    struct VCL_DLLPUBLIC ListBoxWidget final : public AnyWidget
     {
         bool                            DropDown;
         bool                            MultiSelect;
diff --git a/sw/qa/core/text/text.cxx b/sw/qa/core/text/text.cxx
index e5d530af2531..26623e2d3f01 100644
--- a/sw/qa/core/text/text.cxx
+++ b/sw/qa/core/text/text.cxx
@@ -116,10 +116,7 @@ CPPUNIT_TEST_FIXTURE(SwCoreTextTest, 
testBibliographyUrlPdfExport)
     xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false);
 
     // When exporting to PDF:
-    uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
-    utl::MediaDescriptor aMediaDescriptor;
-    aMediaDescriptor["FilterName"] <<= OUString("writer_pdf_Export");
-    xStorable->storeToURL(maTempFile.GetURL(), 
aMediaDescriptor.getAsConstPropertyValueList());
+    StoreToTempFile("writer_pdf_Export");
 
     // Then make sure the field links the source.
     SvFileStream aFile(maTempFile.GetURL(), StreamMode::READ);
@@ -632,10 +629,7 @@ CPPUNIT_TEST_FIXTURE(SwCoreTextTest, testContentControlPDF)
     pWrtShell->InsertContentControl(SwContentControlType::RICH_TEXT);
 
     // When exporting to PDF:
-    uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
-    utl::MediaDescriptor aMediaDescriptor;
-    aMediaDescriptor["FilterName"] <<= OUString("writer_pdf_Export");
-    xStorable->storeToURL(maTempFile.GetURL(), 
aMediaDescriptor.getAsConstPropertyValueList());
+    StoreToTempFile("writer_pdf_Export");
 
     // Then make sure that a fillable form widget is emitted:
     SvFileStream aFile(maTempFile.GetURL(), StreamMode::READ);
@@ -664,10 +658,7 @@ CPPUNIT_TEST_FIXTURE(SwCoreTextTest, 
testCheckboxContentControlPDF)
     pWrtShell->InsertContentControl(SwContentControlType::CHECKBOX);
 
     // When exporting to PDF:
-    uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
-    utl::MediaDescriptor aMediaDescriptor;
-    aMediaDescriptor["FilterName"] <<= OUString("writer_pdf_Export");
-    xStorable->storeToURL(maTempFile.GetURL(), 
aMediaDescriptor.getAsConstPropertyValueList());
+    StoreToTempFile("writer_pdf_Export");
 
     // Then make sure that a checkbox form widget is emitted:
     SvFileStream aFile(maTempFile.GetURL(), StreamMode::READ);
@@ -693,6 +684,40 @@ CPPUNIT_TEST_FIXTURE(SwCoreTextTest, 
testCheckboxContentControlPDF)
                          pAnnotation->getFormFieldType(pPdfDocument.get()));
 }
 
+CPPUNIT_TEST_FIXTURE(SwCoreTextTest, testDropdownContentControlPDF)
+{
+    // Given a file with a dropdown content control:
+    SwDoc* pDoc = createSwDoc();
+    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
+    pWrtShell->InsertContentControl(SwContentControlType::DROP_DOWN_LIST);
+
+    // When exporting to PDF:
+    StoreToTempFile("writer_pdf_Export");
+
+    // Then make sure that a dropdown form widget is emitted:
+    SvFileStream aFile(maTempFile.GetURL(), StreamMode::READ);
+    SvMemoryStream aMemory;
+    aMemory.WriteStream(aFile);
+    std::shared_ptr<vcl::pdf::PDFium> pPDFium = vcl::pdf::PDFiumLibrary::get();
+    if (!pPDFium)
+    {
+        return;
+    }
+    std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument
+        = pPDFium->openDocument(aMemory.GetData(), aMemory.GetSize(), 
OString());
+    std::unique_ptr<vcl::pdf::PDFiumPage> pPage = pPdfDocument->openPage(0);
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: 1
+    // - Actual  : 0
+    // i.e. the dropdown content control was just exported as normal text.
+    CPPUNIT_ASSERT_EQUAL(1, pPage->getAnnotationCount());
+    std::unique_ptr<vcl::pdf::PDFiumAnnotation> pAnnotation = 
pPage->getAnnotation(0);
+    CPPUNIT_ASSERT_EQUAL(vcl::pdf::PDFAnnotationSubType::Widget, 
pAnnotation->getSubType());
+    // Also check the form widget type (our dropdown is called combo in PDF 
terms):
+    CPPUNIT_ASSERT_EQUAL(vcl::pdf::PDFFormFieldType::ComboBox,
+                         pAnnotation->getFormFieldType(pPdfDocument.get()));
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/qa/inc/swmodeltestbase.hxx b/sw/qa/inc/swmodeltestbase.hxx
index c102e75422de..d43094bde2b5 100644
--- a/sw/qa/inc/swmodeltestbase.hxx
+++ b/sw/qa/inc/swmodeltestbase.hxx
@@ -395,6 +395,8 @@ protected:
      * Gets SwDoc from loaded component
      */
     SwDoc* getSwDoc();
+
+    void StoreToTempFile(const OUString& rFilterName);
 };
 
 /**
diff --git a/sw/qa/unit/swmodeltestbase.cxx b/sw/qa/unit/swmodeltestbase.cxx
index 374e18b6f799..7d9e8de3c3df 100644
--- a/sw/qa/unit/swmodeltestbase.cxx
+++ b/sw/qa/unit/swmodeltestbase.cxx
@@ -758,4 +758,12 @@ SwXTextDocument& SwModelTestBase::getSwXTextDocument()
 
 SwDoc* SwModelTestBase::getSwDoc() { return 
getSwXTextDocument().GetDocShell()->GetDoc(); }
 
+void SwModelTestBase::StoreToTempFile(const OUString& rFilterName)
+{
+    uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
+    utl::MediaDescriptor aMediaDescriptor;
+    aMediaDescriptor["FilterName"] <<= rFilterName;
+    xStorable->storeToURL(maTempFile.GetURL(), 
aMediaDescriptor.getAsConstPropertyValueList());
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/text/itrform2.cxx b/sw/source/core/text/itrform2.cxx
index 322ad91b380a..c42e4ee5bfac 100644
--- a/sw/source/core/text/itrform2.cxx
+++ b/sw/source/core/text/itrform2.cxx
@@ -947,6 +947,17 @@ bool SwContentControlPortion::DescribePDFControl(const 
SwTextPaintInfo& rInf) co
             pCheckBoxWidget->OffValue = pContentControl->GetUncheckedState();
             break;
         }
+        case SwContentControlType::DROP_DOWN_LIST:
+        {
+            pDescriptor = std::make_unique<vcl::PDFWriter::ListBoxWidget>();
+            auto pListWidget = 
static_cast<vcl::PDFWriter::ListBoxWidget*>(pDescriptor.get());
+            pListWidget->DropDown = true;
+            for (const auto& rItem : pContentControl->GetListItems())
+            {
+                pListWidget->Entries.push_back(rItem.m_aDisplayText);
+            }
+            break;
+        }
         default:
             break;
     }

Reply via email to