namespace
{ class EnvVarGuard
{ public:
EnvVarGuard(constchar* var, constchar* val)
{ if (getenv(var) == nullptr)
{
sVar = var;
SetEnv(sVar, val);
}
}
~EnvVarGuard()
{ if (sVar)
SetEnv(sVar, nullptr);
}
private: staticvoid SetEnv(constchar* var, constchar* val)
{ #ifdef _WIN32 if (!val)
val = ""; // remove
_putenv_s(var, val); #else if (val)
setenv(var, val, false); else
unsetenv(var); #endif
}
constchar* sVar = nullptr;
};
}
class SdrPdfImportTest : public UnoApiTest
{ public:
SdrPdfImportTest()
: UnoApiTest(u"/sd/qa/unit/data/"_ustr)
{
}
};
// Load the PDF in Draw, which will load the PDF as an Graphic, then // mark the graphic object and trigger "break" function. This should // convert the PDF content into objects/shapes.
CPPUNIT_TEST_FIXTURE(SdrPdfImportTest, testImportSimpleText)
{ auto pPdfium = vcl::pdf::PDFiumLibrary::get(); if (!pPdfium)
{ return;
}
// We need to enable PDFium import (and make sure to disable after the test)
EnvVarGuard UsePDFiumGuard("LO_IMPORT_USE_PDFIUM", "1");
loadFromFile(u"SimplePDF.pdf"); auto pImpressDocument = dynamic_cast<SdXImpressDocument*>(mxComponent.get());
sd::ViewShell* pViewShell = pImpressDocument->GetDocShell()->GetViewShell();
CPPUNIT_ASSERT(pViewShell);
// Get the first page - there should be only one.
SdPage* pPage = pViewShell->GetActualPage();
CPPUNIT_ASSERT(pPage);
// Check there is one object on the page only
CPPUNIT_ASSERT_EQUAL(size_t(1), pPage->GetObjCount());
// 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(); autoconst& pVectorGraphicData = aGraphic.getVectorGraphicData();
CPPUNIT_ASSERT(pVectorGraphicData);
CPPUNIT_ASSERT_EQUAL(VectorGraphicDataType::Pdf, pVectorGraphicData->getType());
// Mark the object
SdrView* pView = pViewShell->GetView();
pView->MarkObj(pObject, pView->GetSdrPageView());
// Execute the break operation - to turn the PDF into shapes/objects
pViewShell->GetDrawView()->DoImportMarkedMtf();
// Check there is one object on the page only
CPPUNIT_ASSERT_EQUAL(size_t(1), pPage->GetObjCount());
// Get the object
SdrObject* pImportedObject = pPage->GetObj(0);
CPPUNIT_ASSERT(pImportedObject);
// Check the object position
CPPUNIT_ASSERT_EQUAL(tools::Rectangle(Point(2011, 2102), Size(2106 + 1, 298 + 1)),
pImportedObject->GetLogicRect());
// Object should be a text object containing one paragraph with // content "This is PDF!"
auto pPdfiumLibrary = vcl::pdf::PDFiumLibrary::get();
loadFromFile(u"PdfWithAnnotation.pdf"); auto pImpressDocument = dynamic_cast<SdXImpressDocument*>(mxComponent.get());
sd::ViewShell* pViewShell = pImpressDocument->GetDocShell()->GetViewShell();
CPPUNIT_ASSERT(pViewShell);
BinaryDataContainer aContainer;
{ // 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(); autoconst& pVectorGraphicData = aGraphic.getVectorGraphicData();
CPPUNIT_ASSERT(pVectorGraphicData);
CPPUNIT_ASSERT_EQUAL(VectorGraphicDataType::Pdf, pVectorGraphicData->getType());
// Write the PDF
aContainer = pVectorGraphicData->getBinaryDataContainer();
}
auto pPDFAnnotation1 = pPDFPage->getAnnotation(0);
CPPUNIT_ASSERT_EQUAL(vcl::pdf::PDFAnnotationSubType::Text,
pPDFAnnotation1->getSubType()); // Text annotation
auto pPDFAnnotation2 = pPDFPage->getAnnotation(1);
CPPUNIT_ASSERT_EQUAL(vcl::pdf::PDFAnnotationSubType::Popup,
pPDFAnnotation2->getSubType()); // Pop-up annotation
}
{ // save as PDF and check annotations
uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
utl::MediaDescriptor aMediaDescriptor;
aMediaDescriptor[u"FilterName"_ustr] <<= u"writer_pdf_Export"_ustr;
uno::Sequence<beans::PropertyValue> aFilterData(
comphelper::InitPropertySequence({ { "ExportBookmarks", uno::Any(true) } }));
aMediaDescriptor[u"FilterData"_ustr] <<= aFilterData;
xStorable->storeToURL(maTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
// Check PDF for annotations auto pPDFDocument = parsePDFExport();
CPPUNIT_ASSERT(pPDFDocument);
CPPUNIT_ASSERT_EQUAL(1, pPDFDocument->getPageCount());
auto pPDFPage = pPDFDocument->openPage(0);
CPPUNIT_ASSERT(pPDFPage);
auto pPDFAnnotation1 = pPDFPage->getAnnotation(0);
CPPUNIT_ASSERT_EQUAL(vcl::pdf::PDFAnnotationSubType::Text,
pPDFAnnotation1->getSubType()); // Text annotation
auto pPDFAnnotation2 = pPDFPage->getAnnotation(1);
CPPUNIT_ASSERT_EQUAL(vcl::pdf::PDFAnnotationSubType::Popup,
pPDFAnnotation2->getSubType()); // Pop-up annotation
// Load document again
loadFromURL(maTempFile.GetURL()); auto pNewImpressDocument = dynamic_cast<SdXImpressDocument*>(mxComponent.get());
sd::ViewShell* pNewViewShell = pNewImpressDocument->GetDocShell()->GetViewShell();
CPPUNIT_ASSERT(pNewViewShell);
// We expect only 1 annotation in the document because the PDF // annotations are dependent on each-other: // parent annotation "Text" and the child annotation "Pop-up"
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.