1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sfx2/docfile.hxx>
21 #include <svx/svdograf.hxx>
24 #include <drawdoc.hxx>
25 #include <sdpdffilter.hxx>
27 #include <vcl/graph.hxx>
28 #include <vcl/pdfread.hxx>
30 using namespace ::com::sun::star
;
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::lang
;
33 using namespace ::com::sun::star::beans
;
34 using namespace ::com::sun::star::graphic
;
35 using namespace ::com::sun::star::io
;
36 using namespace ::com::sun::star::ucb
;
37 using namespace ::sfx2
;
39 SdPdfFilter::SdPdfFilter(SfxMedium
& rMedium
, ::sd::DrawDocShell
& rDocShell
)
40 : SdFilter(rMedium
, rDocShell
)
44 SdPdfFilter::~SdPdfFilter() {}
46 bool SdPdfFilter::Import()
48 //FIXME: Replace with parsing the PDF elements to allow editing.
49 //FIXME: For now we import as images for simplicity.
51 const OUString
aFileName(
52 mrMedium
.GetURLObject().GetMainURL(INetURLObject::DecodeMechanism::NONE
));
54 // Rendering resolution.
55 const double dResolutionDPI
= 96.;
57 std::vector
<std::pair
<Graphic
, Size
>> aGraphics
;
58 if (vcl::ImportPDFUnloaded(aFileName
, aGraphics
, dResolutionDPI
) == 0)
61 // Add as many pages as we need up-front.
62 mrDocument
.CreateFirstPages();
63 for (size_t i
= 0; i
< aGraphics
.size() - 1; ++i
)
65 mrDocument
.DuplicatePage(0);
68 for (const std::pair
<Graphic
, Size
>& aPair
: aGraphics
)
70 const Graphic
& rGraphic
= aPair
.first
;
71 const Size
& aSize
= aPair
.second
;
73 const sal_Int32 nPageNumber
= rGraphic
.getPageNumber();
74 assert(nPageNumber
>= 0 && static_cast<size_t>(nPageNumber
) < aGraphics
.size());
76 // Create the page and insert the Graphic.
77 SdPage
* pPage
= mrDocument
.GetSdPage(nPageNumber
, PageKind::Standard
);
78 Size
aGrfSize(OutputDevice::LogicToLogic(aSize
, rGraphic
.GetPrefMapMode(),
79 MapMode(MapUnit::Map100thMM
)));
81 // Resize to original size based on 72 dpi to preserve page size.
82 aGrfSize
= Size(aGrfSize
.Width() * 72. / dResolutionDPI
,
83 aGrfSize
.Height() * 72. / dResolutionDPI
);
85 // Make the page size match the rendered image.
86 pPage
->SetSize(aGrfSize
);
89 SdrGrafObj
* pSdrGrafObj
= new SdrGrafObj(pPage
->getSdrModelFromSdrPage(), rGraphic
,
90 tools::Rectangle(aPos
, aGrfSize
));
91 pPage
->InsertObject(pSdrGrafObj
);
93 // we know that the initial bitmap we provided was just a placeholder,
94 // we need to swap it out, so that on the next swap in, we render the
96 // const_cast<GraphicObject&>(pSdrGrafObj->GetGraphicObject()).SwapOut();
102 bool SdPdfFilter::Export() { return false; }
104 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */