summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2020-06-19 12:59:59 +0200
committerTomaž Vajngerl <quikee@gmail.com>2020-06-21 23:09:30 +0200
commit037463d2617e5d7bbaea6b99f6b8ffd265cb38af (patch)
tree52065173a27f9bc7cba9c1c562bf6341783cc4e9
parent058f4f3c3d7204421c3e947b845a84eb8bf54675 (diff)
sd: Test PDF Annotation roundtrip
Change-Id: I2532ecb451362b3b48c8c08b79c010dcad67d5da Reviewed-on: https://gerrit.libreoffice.org/c/core/+/96760 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
-rw-r--r--sd/qa/unit/SdrPdfImportTest.cxx132
-rw-r--r--sd/qa/unit/data/PdfWithAnnotation.pdfbin0 -> 13789 bytes
2 files changed, 132 insertions, 0 deletions
diff --git a/sd/qa/unit/SdrPdfImportTest.cxx b/sd/qa/unit/SdrPdfImportTest.cxx
index 9f30ff5000be..4bbb24de4035 100644
--- a/sd/qa/unit/SdrPdfImportTest.cxx
+++ b/sd/qa/unit/SdrPdfImportTest.cxx
@@ -16,7 +16,11 @@
#include <comphelper/scopeguard.hxx>
#include <comphelper/processfactory.hxx>
+#include <comphelper/propertysequence.hxx>
+#include <unotools/tempfile.hxx>
+#include <unotools/mediadescriptor.hxx>
+#include <tools/stream.hxx>
#include <svx/svdograf.hxx>
#include <editeng/outlobj.hxx>
#include <editeng/editobj.hxx>
@@ -123,6 +127,134 @@ CPPUNIT_TEST_FIXTURE(SdrPdfImportTest, testImportSimpleText)
#endif // HAVE_FEATURE_PDFIUM
}
+CPPUNIT_TEST_FIXTURE(SdrPdfImportTest, testAnnotationsImportExport)
+{
+#if HAVE_FEATURE_PDFIUM && !defined(_WIN32)
+ // We need to enable PDFium import (and make sure to disable after the test)
+ bool bResetEnvVar = false;
+ if (getenv("LO_IMPORT_USE_PDFIUM") == nullptr)
+ {
+ bResetEnvVar = true;
+ setenv("LO_IMPORT_USE_PDFIUM", "1", false);
+ }
+ comphelper::ScopeGuard aPDFiumEnvVarGuard([&]() {
+ if (bResetEnvVar)
+ unsetenv("LO_IMPORT_USE_PDFIUM");
+ });
+
+ bool bPDFCompressorResetEnvVar = false;
+ if (getenv("VCL_DEBUG_DISABLE_PDFCOMPRESSION") == nullptr)
+ {
+ bPDFCompressorResetEnvVar = true;
+ setenv("VCL_DEBUG_DISABLE_PDFCOMPRESSION", "1", false);
+ }
+ comphelper::ScopeGuard aPDFCompressorEnvVarGuard([&]() {
+ if (bPDFCompressorResetEnvVar)
+ unsetenv("VCL_DEBUG_DISABLE_PDFCOMPRESSION");
+ });
+
+ auto pPdfiumLibrary = vcl::pdf::PDFiumLibrary::get();
+
+ mxComponent
+ = loadFromDesktop(m_directories.getURLFromSrc("sd/qa/unit/data/PdfWithAnnotation.pdf"));
+ auto pImpressDocument = dynamic_cast<SdXImpressDocument*>(mxComponent.get());
+ sd::ViewShell* pViewShell = pImpressDocument->GetDocShell()->GetViewShell();
+ CPPUNIT_ASSERT(pViewShell);
+
+ const void* pData = nullptr;
+ int nLength = 0;
+
+ {
+ // Get the first page - there should be only one.
+ SdPage* pPage = pViewShell->GetActualPage();
+ CPPUNIT_ASSERT(pPage);
+
+ // Check the number of annotations
+ CPPUNIT_ASSERT_EQUAL(size_t(1), pPage->getAnnotations().size());
+
+ // Get the first object - there should be only one.
+ SdrObject* pObject = pPage->GetObj(0);
+ CPPUNIT_ASSERT(pObject);
+
+ // Check the object is a graphic object
+ SdrGrafObj* pGraphicObject = dynamic_cast<SdrGrafObj*>(pObject);
+ CPPUNIT_ASSERT(pGraphicObject);
+
+ // Check the graphic is a vector graphic and that it is PDF
+ Graphic aGraphic = pGraphicObject->GetGraphic();
+ auto const& pVectorGraphicData = aGraphic.getVectorGraphicData();
+ CPPUNIT_ASSERT(pVectorGraphicData);
+ CPPUNIT_ASSERT_EQUAL(VectorGraphicDataType::Pdf,
+ pVectorGraphicData->getVectorGraphicDataType());
+
+ // Write the PDF
+ pData = pVectorGraphicData->getVectorGraphicDataArray().getConstArray();
+ nLength = pVectorGraphicData->getVectorGraphicDataArrayLength();
+ }
+
+ { // check graphic PDF has annotations
+
+ auto pPDFDocument = pPdfiumLibrary->openDocument(pData, nLength);
+ auto pPDFPage = pPDFDocument->openPage(0);
+
+ CPPUNIT_ASSERT_EQUAL(2, pPDFPage->getAnnotationCount());
+
+ auto pPDFAnnotation1 = pPDFPage->getAnnotation(0);
+ CPPUNIT_ASSERT_EQUAL(1, pPDFAnnotation1->getSubType()); // Text annotation
+
+ auto pPDFAnnotation2 = pPDFPage->getAnnotation(1);
+ CPPUNIT_ASSERT_EQUAL(16, pPDFAnnotation2->getSubType()); // Pop-up annotation
+ }
+
+ { // save as PDF and check annotations
+ utl::TempFile aTempFile;
+ aTempFile.EnableKillingFile();
+
+ uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
+ utl::MediaDescriptor aMediaDescriptor;
+ aMediaDescriptor["FilterName"] <<= OUString("writer_pdf_Export");
+ uno::Sequence<beans::PropertyValue> aFilterData(
+ comphelper::InitPropertySequence({ { "ExportBookmarks", uno::Any(true) } }));
+ aMediaDescriptor["FilterData"] <<= aFilterData;
+ xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
+ mxComponent->dispose();
+
+ SvFileStream aFile(aTempFile.GetURL(), StreamMode::READ);
+ SvMemoryStream aMemory;
+ aMemory.WriteStream(aFile);
+
+ // Check PDF for annotations
+ auto pPDFDocument = pPdfiumLibrary->openDocument(aMemory.GetData(), aMemory.GetSize());
+ CPPUNIT_ASSERT(pPDFDocument);
+ CPPUNIT_ASSERT_EQUAL(1, pPDFDocument->getPageCount());
+
+ auto pPDFPage = pPDFDocument->openPage(0);
+ CPPUNIT_ASSERT(pPDFPage);
+
+ // TODO: Should be 2 really
+ CPPUNIT_ASSERT_EQUAL(1, pPDFPage->getAnnotationCount());
+
+ auto pPDFAnnotation1 = pPDFPage->getAnnotation(0);
+ CPPUNIT_ASSERT_EQUAL(1, pPDFAnnotation1->getSubType()); // Text annotation
+
+ //auto pPDFAnnotation2 = pPDFPage->getAnnotation(1);
+ //CPPUNIT_ASSERT_EQUAL(16, pPDFAnnotation2->getSubType()); // Pop-up annotation
+
+ // Load document again
+ mxComponent = loadFromDesktop(aTempFile.GetURL());
+ auto pNewImpressDocument = dynamic_cast<SdXImpressDocument*>(mxComponent.get());
+ sd::ViewShell* pNewViewShell = pNewImpressDocument->GetDocShell()->GetViewShell();
+ CPPUNIT_ASSERT(pNewViewShell);
+
+ SdPage* pPage = pNewViewShell->GetActualPage();
+ CPPUNIT_ASSERT(pPage);
+
+ //CPPUNIT_ASSERT_EQUAL(false, pPage->getAnnotations().empty());
+ }
+
+#endif // HAVE_FEATURE_PDFIUM
+}
+
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sd/qa/unit/data/PdfWithAnnotation.pdf b/sd/qa/unit/data/PdfWithAnnotation.pdf
new file mode 100644
index 000000000000..09529ed17884
--- /dev/null
+++ b/sd/qa/unit/data/PdfWithAnnotation.pdf
Binary files differ