1 | package pdfimport.pdfbox;
|
---|
2 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
3 |
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.geom.Rectangle2D;
|
---|
6 | import java.io.File;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import org.apache.pdfbox.pdfviewer.PageDrawer;
|
---|
10 | import org.apache.pdfbox.pdmodel.PDDocument;
|
---|
11 | import org.apache.pdfbox.pdmodel.PDPage;
|
---|
12 | import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
---|
13 | import org.apache.pdfbox.util.PDFStreamEngine;
|
---|
14 |
|
---|
15 | import pdfimport.PathOptimizer;
|
---|
16 |
|
---|
17 | public class PdfBoxParser extends PDFStreamEngine{
|
---|
18 | private final PathOptimizer target;
|
---|
19 |
|
---|
20 | public PdfBoxParser(PathOptimizer target){
|
---|
21 | this.target = target;
|
---|
22 | }
|
---|
23 |
|
---|
24 | @SuppressWarnings("unchecked")
|
---|
25 | public void parse(File file) throws Exception
|
---|
26 | {
|
---|
27 | PDDocument document = PDDocument.load( file);
|
---|
28 |
|
---|
29 | if( document.isEncrypted() ){
|
---|
30 | throw new Exception(tr("Encrypted documents not supported."));
|
---|
31 | }
|
---|
32 |
|
---|
33 | List allPages = document.getDocumentCatalog().getAllPages();
|
---|
34 |
|
---|
35 | if (allPages.size() != 1) {
|
---|
36 | throw new Exception(tr("The PDF file must have exactly one page."));
|
---|
37 | }
|
---|
38 |
|
---|
39 | PDPage page = (PDPage)allPages.get(0);
|
---|
40 | PDRectangle pageSize = page.findMediaBox();
|
---|
41 | Dimension pageDimension = pageSize.createDimension();
|
---|
42 | int rotation = page.getRotation();
|
---|
43 |
|
---|
44 | GraphicsProcessor p = new GraphicsProcessor(target, rotation, pageDimension.getHeight());
|
---|
45 | PageDrawer drawer = new PageDrawer();
|
---|
46 | drawer.drawPage(p, page, pageDimension);
|
---|
47 | this.target.bounds = new Rectangle2D.Double(pageSize.getLowerLeftX(), pageSize.getLowerLeftY(), pageSize.getWidth(), pageSize.getHeight());
|
---|
48 | }
|
---|
49 | }
|
---|