summaryrefslogtreecommitdiff
path: root/vcl/source/filter/ipdf/pdfread.cxx
blob: cc63415eba6daa287c7120d2bed4e11af3c18cc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "pdfread.hxx"

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/document/XFilter.hpp>
#include <com/sun/star/document/XImporter.hpp>
#include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/io/XStream.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>

#include <comphelper/processfactory.hxx>
#include <comphelper/propertyvalue.hxx>
#include <comphelper/scopeguard.hxx>
#include <unotools/streamwrap.hxx>
#include <vcl/wmf.hxx>

using namespace com::sun::star;

namespace
{

/// Imports a PDF stream into Draw.
uno::Reference<lang::XComponent> importIntoDraw(SvStream& rStream)
{
    // Create an empty Draw component.
    uno::Reference<frame::XDesktop2> xDesktop = css::frame::Desktop::create(comphelper::getProcessComponentContext());
    uno::Reference<frame::XComponentLoader> xComponentLoader(xDesktop, uno::UNO_QUERY);
    uno::Sequence<beans::PropertyValue> aLoadArguments =
    {
        comphelper::makePropertyValue("Hidden", true)
    };
    uno::Reference<lang::XComponent> xComponent = xComponentLoader->loadComponentFromURL("private:factory/sdraw", "_default", 0, aLoadArguments);

    // Import the PDF into it.
    uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(comphelper::getProcessServiceFactory());
    // Need to go via FilterFactory, otherwise XmlFilterAdaptor::initialize() is not called.
    uno::Reference<lang::XMultiServiceFactory> xFilterFactory(xMultiServiceFactory->createInstance("com.sun.star.document.FilterFactory"), uno::UNO_QUERY);
    uno::Reference<document::XFilter> xFilter(xFilterFactory->createInstanceWithArguments("draw_pdf_import", uno::Sequence<uno::Any>()), uno::UNO_QUERY);
    uno::Reference<document::XImporter> xImporter(xFilter, uno::UNO_QUERY);
    xImporter->setTargetDocument(xComponent);

    uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(rStream));
    uno::Sequence<beans::PropertyValue> aImportArguments =
    {
        // XmlFilterAdaptor::importImpl() mandates URL, even if it's empty.
        comphelper::makePropertyValue("URL", OUString()),
        comphelper::makePropertyValue("InputStream", xStream),
    };

    if (xFilter->filter(aImportArguments))
        return xComponent;
    else
    {
        xComponent->dispose();
        return uno::Reference<lang::XComponent>();
    }
}

}

VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Graphic& rGraphic)
{
    uno::Reference<lang::XComponent> xComponent = importIntoDraw(rStream);
    if (!xComponent.is())
        return false;
    comphelper::ScopeGuard aGuard([&xComponent]()
    {
        xComponent->dispose();
    });

    // Get the preview of the first page.
    uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier(xComponent, uno::UNO_QUERY);
    uno::Reference<drawing::XDrawPages> xDrawPages = xDrawPagesSupplier->getDrawPages();
    if (xDrawPages->getCount() <= 0)
        return false;

    uno::Reference<beans::XPropertySet> xFirstPage(xDrawPages->getByIndex(0), uno::UNO_QUERY);
    uno::Sequence<sal_Int8> aSequence;
    if (!(xFirstPage->getPropertyValue("Preview") >>= aSequence))
        return false;

    if (!aSequence.hasElements())
        return false;

    // Convert it into a GDIMetaFile.
    SvMemoryStream aPreviewStream(aSequence.getLength());
    aPreviewStream.WriteBytes(aSequence.getArray(), aSequence.getLength());
    aPreviewStream.Seek(0);
    GDIMetaFile aMtf;
    if (!ConvertWMFToGDIMetaFile(aPreviewStream, aMtf))
        return false;

    rGraphic = aMtf;

    return true;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */