Revision 9772
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.api/src/main/resources/META-INF/services/org.gvsig.tools.library.Library | ||
---|---|---|
1 |
org.gvsig.pdf.lib.api.PDFLibrary |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.api/src/main/java/org/gvsig/pdf/lib/api/PDFLocator.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.pdf.lib.api; |
|
25 |
|
|
26 |
import org.gvsig.tools.locator.AbstractLocator; |
|
27 |
import org.gvsig.tools.locator.Locator; |
|
28 |
import org.gvsig.tools.locator.LocatorException; |
|
29 |
|
|
30 |
@SuppressWarnings("rawtypes") |
|
31 |
public class PDFLocator extends AbstractLocator { |
|
32 |
|
|
33 |
private static final String LOCATOR_NAME = "PDFLocator"; |
|
34 |
|
|
35 |
public static final String PDF_MANAGER_NAME = |
|
36 |
"PDFManager"; |
|
37 |
|
|
38 |
private static final String PDF_MANAGER_DESCRIPTION = |
|
39 |
"PDF Manager of gvSIG"; |
|
40 |
|
|
41 |
private static final PDFLocator instance = new PDFLocator(); |
|
42 |
|
|
43 |
private PDFLocator() { |
|
44 |
|
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* Return the singleton instance. |
|
49 |
* |
|
50 |
* @return the singleton instance |
|
51 |
*/ |
|
52 |
public static PDFLocator getInstance() { |
|
53 |
return instance; |
|
54 |
} |
|
55 |
|
|
56 |
@Override |
|
57 |
public String getLocatorName() { |
|
58 |
return LOCATOR_NAME; |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* Return a reference to PDFManager. |
|
63 |
* |
|
64 |
* @return a reference to PDFManager |
|
65 |
* @throws LocatorException |
|
66 |
* if there is no access to the class or the class cannot be |
|
67 |
* instantiated |
|
68 |
* @see Locator#get(String) |
|
69 |
*/ |
|
70 |
public static PDFManager getPDFManager() |
|
71 |
throws LocatorException { |
|
72 |
return (PDFManager) getInstance().get(PDF_MANAGER_NAME); |
|
73 |
} |
|
74 |
|
|
75 |
/** |
|
76 |
* Registers the Class implementing the PDFManager interface. |
|
77 |
* |
|
78 |
* @param clazz |
|
79 |
* implementing the PDFManager interface |
|
80 |
*/ |
|
81 |
// public static void registerPDFManager(Class clazz) { |
|
82 |
// getInstance().register(PDF_MANAGER_NAME, |
|
83 |
// PDF_MANAGER_DESCRIPTION, clazz); |
|
84 |
// } |
|
85 |
|
|
86 |
public static void registerDefaultPDFManager(Class clazz) { |
|
87 |
getInstance().registerDefault(PDF_MANAGER_NAME, |
|
88 |
PDF_MANAGER_DESCRIPTION, clazz); |
|
89 |
} |
|
90 |
|
|
91 |
|
|
92 |
} |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.api/src/main/java/org/gvsig/pdf/lib/api/PDFLibrary.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.pdf.lib.api; |
|
25 |
|
|
26 |
import org.gvsig.fmap.dal.DALLibrary; |
|
27 |
import org.gvsig.tools.ToolsLibrary; |
|
28 |
import org.gvsig.tools.library.AbstractLibrary; |
|
29 |
import org.gvsig.tools.library.LibraryException; |
|
30 |
|
|
31 |
/** |
|
32 |
* |
|
33 |
* @author jjdelcerro |
|
34 |
*/ |
|
35 |
public class PDFLibrary extends AbstractLibrary { |
|
36 |
|
|
37 |
@Override |
|
38 |
public void doRegistration() { |
|
39 |
super.doRegistration(); |
|
40 |
registerAsAPI(PDFLibrary.class); |
|
41 |
this.require(ToolsLibrary.class); |
|
42 |
this.require(DALLibrary.class); |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
protected void doInitialize() throws LibraryException { |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
protected void doPostInitialize() throws LibraryException { |
|
51 |
} |
|
52 |
|
|
53 |
} |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.api/src/main/java/org/gvsig/pdf/lib/api/PDFManager.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.pdf.lib.api; |
|
25 |
|
|
26 |
import java.io.File; |
|
27 |
import java.io.OutputStream; |
|
28 |
import java.util.List; |
|
29 |
import org.gvsig.tools.task.SimpleTaskStatus; |
|
30 |
|
|
31 |
/** |
|
32 |
* |
|
33 |
* @author jjdelcerro |
|
34 |
*/ |
|
35 |
public interface PDFManager { |
|
36 |
|
|
37 |
public PDFDocument createPDFDocument(byte[] document); |
|
38 |
public PDFDocument createPDFDocument(String document); |
|
39 |
public PDFDocument createPDFDocument(File document); |
|
40 |
public PDFDocument createPDFDocument(); |
|
41 |
public PDFWriter createPDFWriter(); |
|
42 |
public void merge(List<PDFDocument> inputs, |
|
43 |
OutputStream output, |
|
44 |
long maxMemoryToUse, |
|
45 |
String title, |
|
46 |
String creator, |
|
47 |
String subject |
|
48 |
); |
|
49 |
|
|
50 |
public void merge(List inputPdfs, File outputFile, String title, String creator, String subject, SimpleTaskStatus status); |
|
51 |
|
|
52 |
public void merge(List inputPdfs, OutputStream outputPdf, String title, String creator, String subject, SimpleTaskStatus status); |
|
53 |
|
|
54 |
} |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.api/src/main/java/org/gvsig/pdf/lib/api/PDFWriter.java | ||
---|---|---|
1 |
package org.gvsig.pdf.lib.api; |
|
2 |
|
|
3 |
import java.awt.Graphics2D; |
|
4 |
|
|
5 |
/** |
|
6 |
* |
|
7 |
* @author osc |
|
8 |
*/ |
|
9 |
public interface PDFWriter { |
|
10 |
|
|
11 |
public static final int PAGE_LETTER = 1; |
|
12 |
public static final int PAGE_LEGAL = 2; |
|
13 |
public static final int PAGE_A4 = 4; |
|
14 |
public static final int PAGE_A5 = 5; |
|
15 |
|
|
16 |
public void open(int pageSize); |
|
17 |
|
|
18 |
public void close(); |
|
19 |
|
|
20 |
public float getWidth(); |
|
21 |
|
|
22 |
public float getHeight(); |
|
23 |
|
|
24 |
public Graphics2D getGraphics2D(); |
|
25 |
|
|
26 |
public byte[] toByteArray(); |
|
27 |
|
|
28 |
public PDFDocument toDocument(); |
|
29 |
} |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.api/src/main/java/org/gvsig/pdf/lib/api/PDFDocument.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.pdf.lib.api; |
|
7 |
|
|
8 |
import java.io.IOException; |
|
9 |
import java.io.InputStream; |
|
10 |
import org.gvsig.tools.dispose.Disposable; |
|
11 |
import org.gvsig.tools.swing.api.SimpleImage; |
|
12 |
|
|
13 |
/** |
|
14 |
* |
|
15 |
* @author osc |
|
16 |
*/ |
|
17 |
public interface PDFDocument extends Disposable { |
|
18 |
public InputStream getInputStream() throws IOException ; |
|
19 |
|
|
20 |
public void setSource(Object source); |
|
21 |
|
|
22 |
public byte[] toByteArray(); |
|
23 |
|
|
24 |
public String toHexString(); |
|
25 |
|
|
26 |
public int getNumberOfPages(); |
|
27 |
|
|
28 |
public SimpleImage toImage(); |
|
29 |
|
|
30 |
public SimpleImage toImage(int page); |
|
31 |
|
|
32 |
public SimpleImage toImage(int page, int dpis); |
|
33 |
} |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.api/pom.xml | ||
---|---|---|
1 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
2 |
<modelVersion>4.0.0</modelVersion> |
|
3 |
<parent> |
|
4 |
<groupId>org.gvsig</groupId> |
|
5 |
<artifactId>org.gvsig.pdf.lib</artifactId> |
|
6 |
<version>1.0.156</version> |
|
7 |
</parent> |
|
8 |
<artifactId>org.gvsig.pdf.lib.api</artifactId> |
|
9 |
<name>${project.artifactId}</name> |
|
10 |
<dependencies> |
|
11 |
<dependency> |
|
12 |
<groupId>org.gvsig</groupId> |
|
13 |
<artifactId>org.gvsig.tools.lib</artifactId> |
|
14 |
<scope>compile</scope> |
|
15 |
</dependency> |
|
16 |
<dependency> |
|
17 |
<groupId>org.gvsig</groupId> |
|
18 |
<artifactId>org.gvsig.tools.swing.api</artifactId> |
|
19 |
<scope>compile</scope> |
|
20 |
</dependency> |
|
21 |
<dependency> |
|
22 |
<groupId>org.gvsig</groupId> |
|
23 |
<artifactId>org.gvsig.fmap.dal.api</artifactId> |
|
24 |
<scope>compile</scope> |
|
25 |
</dependency> |
|
26 |
</dependencies> |
|
27 |
|
|
28 |
</project> |
|
0 | 29 |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/pom.xml | ||
---|---|---|
1 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
2 |
<modelVersion>4.0.0</modelVersion> |
|
3 |
<parent> |
|
4 |
<groupId>org.gvsig</groupId> |
|
5 |
<artifactId>org.gvsig.pdf</artifactId> |
|
6 |
<version>1.0.156</version> |
|
7 |
</parent> |
|
8 |
<artifactId>org.gvsig.pdf.lib</artifactId> |
|
9 |
<packaging>pom</packaging> |
|
10 |
<name>${project.artifactId}</name> |
|
11 |
<modules> |
|
12 |
<module>org.gvsig.pdf.lib.api</module> |
|
13 |
<module>org.gvsig.pdf.lib.impl</module> |
|
14 |
</modules> |
|
15 |
</project> |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.impl/src/test/java/org/gvsig/pdf/lib/impl/TestPDFToImage.java | ||
---|---|---|
1 |
package org.gvsig.pdf.lib.impl; |
|
2 |
/* |
|
3 |
* To change this license header, choose License Headers in Project Properties. |
|
4 |
* To change this template file, choose Tools | Templates |
|
5 |
* and open the template in the editor. |
|
6 |
*/ |
|
7 |
import java.io.InputStream; |
|
8 |
import org.gvsig.pdf.lib.api.PDFDocument; |
|
9 |
import org.gvsig.pdf.lib.api.PDFLocator; |
|
10 |
import org.gvsig.pdf.lib.api.PDFManager; |
|
11 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
|
12 |
|
|
13 |
/** |
|
14 |
* |
|
15 |
* @author fdiaz |
|
16 |
*/ |
|
17 |
public class TestPDFToImage extends AbstractLibraryAutoInitTestCase { |
|
18 |
|
|
19 |
@Override |
|
20 |
protected void doSetUp() throws Exception { |
|
21 |
} |
|
22 |
|
|
23 |
public void test1() { |
|
24 |
InputStream in = this.getClass().getResourceAsStream("21-262.pdf"); |
|
25 |
PDFManager manager = PDFLocator.getPDFManager(); |
|
26 |
PDFDocument pdfDoc = manager.createPDFDocument(); |
|
27 |
pdfDoc.setSource(in); |
|
28 |
assertEquals(1, pdfDoc.getNumberOfPages()); |
|
29 |
assertNotNull(pdfDoc.toImage(0)); |
|
30 |
} |
|
31 |
|
|
32 |
} |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.impl/src/main/java/org/gvsig/pdf/lib/impl/DefaultPDFWriter.java | ||
---|---|---|
1 |
package org.gvsig.pdf.lib.impl; |
|
2 |
|
|
3 |
import de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D; |
|
4 |
import java.awt.Graphics2D; |
|
5 |
import java.io.IOException; |
|
6 |
import org.apache.commons.io.output.ByteArrayOutputStream; |
|
7 |
import org.apache.pdfbox.pdmodel.PDDocument; |
|
8 |
import org.apache.pdfbox.pdmodel.PDPage; |
|
9 |
import org.apache.pdfbox.pdmodel.common.PDRectangle; |
|
10 |
import org.gvsig.pdf.lib.api.PDFDocument; |
|
11 |
import org.gvsig.pdf.lib.api.PDFWriter; |
|
12 |
|
|
13 |
/** |
|
14 |
* |
|
15 |
* @author gvSIG Team |
|
16 |
*/ |
|
17 |
|
|
18 |
@SuppressWarnings("UseSpecificCatch") |
|
19 |
public class DefaultPDFWriter implements PDFWriter { |
|
20 |
|
|
21 |
private PDRectangle pageSize; |
|
22 |
private PdfBoxGraphics2D graphics; |
|
23 |
private PDDocument document; |
|
24 |
private byte[] data; |
|
25 |
|
|
26 |
public DefaultPDFWriter() { |
|
27 |
|
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
public void open(int pageSize) { |
|
32 |
switch (pageSize) { |
|
33 |
case PDFWriter.PAGE_A4: |
|
34 |
this.pageSize = PDRectangle.A4; |
|
35 |
break; |
|
36 |
case PDFWriter.PAGE_A5: |
|
37 |
this.pageSize = PDRectangle.A5; |
|
38 |
break; |
|
39 |
case PDFWriter.PAGE_LEGAL: |
|
40 |
this.pageSize = PDRectangle.LEGAL; |
|
41 |
break; |
|
42 |
case PDFWriter.PAGE_LETTER: |
|
43 |
this.pageSize = PDRectangle.LETTER; |
|
44 |
break; |
|
45 |
default: |
|
46 |
throw new IllegalArgumentException("Invalid value of page format"); |
|
47 |
} |
|
48 |
this.close(); |
|
49 |
this.data = null; |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
public void close() { |
|
54 |
if( this.graphics!=null ) { |
|
55 |
this.graphics.dispose(); |
|
56 |
this.graphics = null; |
|
57 |
} |
|
58 |
if( this.document!=null ) { |
|
59 |
try { |
|
60 |
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
61 |
this.document.save(os); |
|
62 |
this.data = os.toByteArray(); |
|
63 |
} catch (IOException ex) { |
|
64 |
this.data = null; |
|
65 |
} |
|
66 |
this.document = null; |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
@Override |
|
71 |
public Graphics2D getGraphics2D() { |
|
72 |
try { |
|
73 |
if (this.graphics != null) { |
|
74 |
return this.graphics; |
|
75 |
} |
|
76 |
|
|
77 |
if (this.document == null) { |
|
78 |
this.document = new PDDocument(); |
|
79 |
PDPage page = new PDPage(this.pageSize); |
|
80 |
this.document.addPage(page); |
|
81 |
} |
|
82 |
this.graphics = new PdfBoxGraphics2D( |
|
83 |
this.document, |
|
84 |
this.pageSize |
|
85 |
); |
|
86 |
return this.graphics; |
|
87 |
} catch (Exception ex) { |
|
88 |
throw new RuntimeException("Can't create graphics.", ex); |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
@Override |
|
93 |
public float getWidth() { |
|
94 |
return this.pageSize.getWidth(); |
|
95 |
} |
|
96 |
|
|
97 |
@Override |
|
98 |
public float getHeight() { |
|
99 |
return this.pageSize.getHeight(); |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public byte[] toByteArray() { |
|
104 |
if( this.document!=null ) { |
|
105 |
this.close(); |
|
106 |
} |
|
107 |
return this.data; |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public PDFDocument toDocument() { |
|
112 |
if( this.document!=null ) { |
|
113 |
this.close(); |
|
114 |
} |
|
115 |
PDFDocument theDocument = new DefaultPDFDocument(); |
|
116 |
theDocument.setSource(this.data); |
|
117 |
return theDocument; |
|
118 |
} |
|
119 |
|
|
120 |
} |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.impl/src/main/java/org/gvsig/pdf/lib/impl/PdfAsImageDataProfile.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.pdf.lib.impl; |
|
7 |
|
|
8 |
import java.io.File; |
|
9 |
import java.net.URL; |
|
10 |
import org.apache.commons.lang3.StringUtils; |
|
11 |
import org.apache.tika.Tika; |
|
12 |
import org.gvsig.fmap.dal.feature.AbstractDataProfile; |
|
13 |
import org.gvsig.pdf.lib.api.PDFDocument; |
|
14 |
import org.gvsig.pdf.lib.api.PDFLocator; |
|
15 |
import org.gvsig.tools.dispose.DisposeUtils; |
|
16 |
import org.gvsig.tools.dynobject.Tags; |
|
17 |
import org.gvsig.tools.swing.api.SimpleImage; |
|
18 |
import org.gvsig.tools.util.URLUtils; |
|
19 |
|
|
20 |
/** |
|
21 |
* |
|
22 |
* @author fdiaz |
|
23 |
*/ |
|
24 |
class PdfAsImageDataProfile extends AbstractDataProfile { |
|
25 |
|
|
26 |
/*friend*/ PdfAsImageDataProfile() { |
|
27 |
super("Image", SimpleImage.class); |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
protected Object doCreateData(Object data, Tags tags) { |
|
32 |
PDFDocument doc = null; |
|
33 |
try { |
|
34 |
Object file = data; |
|
35 |
if (file instanceof URL) { |
|
36 |
file = URLUtils.toFile((URL) file); |
|
37 |
} |
|
38 |
if (file instanceof File) { |
|
39 |
File fileImage = (File) file; |
|
40 |
Tika tika = new Tika(); |
|
41 |
String mimeType = tika.detect(fileImage); |
|
42 |
if (StringUtils.equalsIgnoreCase(mimeType, "application/pdf")) { |
|
43 |
doc = PDFLocator.getPDFManager().createPDFDocument(); |
|
44 |
doc.setSource(fileImage); |
|
45 |
SimpleImage img = null; |
|
46 |
for (int i = 0; i < doc.getNumberOfPages(); i++) { |
|
47 |
img = doc.toImage(i); |
|
48 |
break; //Falta soporte para Multipage images, solo cogemos la primera. |
|
49 |
} |
|
50 |
return img; |
|
51 |
} |
|
52 |
} |
|
53 |
} catch (Exception ex) { |
|
54 |
LOGGER.debug("Can't get pdf", ex); |
|
55 |
} finally { |
|
56 |
DisposeUtils.disposeQuietly(doc); |
|
57 |
} |
|
58 |
return null; |
|
59 |
} |
|
60 |
|
|
61 |
} |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.impl/src/main/java/org/gvsig/pdf/lib/impl/DefaultPDFDocument.java | ||
---|---|---|
1 |
package org.gvsig.pdf.lib.impl; |
|
2 |
|
|
3 |
import com.openhtmltopdf.extend.FSSupplier; |
|
4 |
import com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder.FontStyle; |
|
5 |
import com.openhtmltopdf.outputdevice.helper.ExternalResourceControlPriority; |
|
6 |
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder; |
|
7 |
import java.awt.image.BufferedImage; |
|
8 |
import java.io.ByteArrayInputStream; |
|
9 |
import java.io.ByteArrayOutputStream; |
|
10 |
import java.io.File; |
|
11 |
import java.io.FileInputStream; |
|
12 |
import java.io.FileNotFoundException; |
|
13 |
import java.io.IOException; |
|
14 |
import java.io.InputStream; |
|
15 |
import java.net.URL; |
|
16 |
import org.gvsig.pdf.lib.api.PDFDocument; |
|
17 |
import org.slf4j.LoggerFactory; |
|
18 |
import org.apache.commons.codec.binary.Hex; |
|
19 |
import org.apache.commons.codec.DecoderException; |
|
20 |
import org.apache.commons.codec.binary.Base64; |
|
21 |
import org.apache.commons.io.IOUtils; |
|
22 |
import org.apache.commons.lang3.StringUtils; |
|
23 |
import org.apache.pdfbox.pdmodel.PDDocument; |
|
24 |
import org.apache.pdfbox.rendering.ImageType; |
|
25 |
import org.apache.pdfbox.rendering.PDFRenderer; |
|
26 |
import org.gvsig.tools.swing.api.SimpleImage; |
|
27 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
|
28 |
import org.jsoup.Jsoup; |
|
29 |
import org.jsoup.helper.W3CDom; |
|
30 |
import org.jsoup.nodes.Document; |
|
31 |
|
|
32 |
/** |
|
33 |
* |
|
34 |
* @author gvSIG Team |
|
35 |
*/ |
|
36 |
@SuppressWarnings("UseSpecificCatch") |
|
37 |
public class DefaultPDFDocument implements PDFDocument { |
|
38 |
|
|
39 |
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DefaultPDFDocument.class); |
|
40 |
private Object source; |
|
41 |
private int nummberOfPages; |
|
42 |
private PDDocument document; |
|
43 |
|
|
44 |
public DefaultPDFDocument() { |
|
45 |
this.nummberOfPages = -1; |
|
46 |
this.document = null; |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
public void setSource(Object source) { |
|
51 |
this.source = source; |
|
52 |
this.nummberOfPages = -1; |
|
53 |
this.document = null; |
|
54 |
} |
|
55 |
|
|
56 |
@Override |
|
57 |
public InputStream getInputStream() throws IOException { |
|
58 |
InputStream is = null; |
|
59 |
if (source == null) { |
|
60 |
return null; |
|
61 |
} else if (source instanceof InputStream) { |
|
62 |
return (InputStream) source; |
|
63 |
} else if (source instanceof File) { |
|
64 |
File fileSource = (File) source; |
|
65 |
try { |
|
66 |
is = new FileInputStream(fileSource); |
|
67 |
} catch (IOException ex) { |
|
68 |
LOGGER.warn("Not been able to convert file to input stream", ex); |
|
69 |
throw new IOException(ex); |
|
70 |
} |
|
71 |
} else if (source instanceof URL) { |
|
72 |
URL urlSource = (URL) source; |
|
73 |
try { |
|
74 |
is = urlSource.openStream(); |
|
75 |
} catch (IOException ex) { |
|
76 |
LOGGER.warn("Not been able to open URL stream", ex); |
|
77 |
throw new IOException(ex); |
|
78 |
} |
|
79 |
} else if (source instanceof String) { |
|
80 |
String strSource = (String) source; |
|
81 |
if( StringUtils.startsWithIgnoreCase(strSource, "<html>") ) { |
|
82 |
byte[] pdfbytes = html2pdf(strSource); |
|
83 |
if( pdfbytes==null ) { |
|
84 |
throw new IOException(); |
|
85 |
} |
|
86 |
is = new ByteArrayInputStream(pdfbytes); |
|
87 |
} else { |
|
88 |
File f = new File(strSource); |
|
89 |
if (f.exists()) { |
|
90 |
try { |
|
91 |
is = new FileInputStream(f); |
|
92 |
} catch (FileNotFoundException ex) { |
|
93 |
LOGGER.warn("Not been able to load existing file", ex); |
|
94 |
throw new IOException(ex); |
|
95 |
} |
|
96 |
} else { |
|
97 |
try { |
|
98 |
URL url = new URL(strSource); |
|
99 |
is = url.openStream(); |
|
100 |
is.available(); // Force exception if is null |
|
101 |
} catch (Exception ex) { |
|
102 |
try { |
|
103 |
byte[] data = Hex.decodeHex(strSource.toCharArray()); |
|
104 |
is = new ByteArrayInputStream(data); |
|
105 |
} catch (DecoderException ex2) { |
|
106 |
try { |
|
107 |
byte[] data = Base64.decodeBase64(strSource); |
|
108 |
is = new ByteArrayInputStream(data); |
|
109 |
} catch (Exception ex3) { |
|
110 |
throw new IOException(ex3); |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
} |
|
115 |
} |
|
116 |
} else if (source instanceof byte[]) { |
|
117 |
byte[] byteSource = (byte[]) source; |
|
118 |
is = new ByteArrayInputStream(byteSource); |
|
119 |
} |
|
120 |
|
|
121 |
return is; |
|
122 |
} |
|
123 |
|
|
124 |
@Override |
|
125 |
public byte[] toByteArray() { |
|
126 |
if (this.source != null) { |
|
127 |
try { |
|
128 |
InputStream is = this.getInputStream(); |
|
129 |
byte[] bytes = new byte[is.available()]; |
|
130 |
is.read(bytes); |
|
131 |
return bytes; |
|
132 |
} catch (IOException ex) { |
|
133 |
LOGGER.warn("Not been able to convert pdf to byte array", ex); |
|
134 |
} |
|
135 |
} |
|
136 |
return null; |
|
137 |
|
|
138 |
} |
|
139 |
|
|
140 |
private String bytearray_hex(byte[] data) { |
|
141 |
StringBuilder builder = new StringBuilder(); |
|
142 |
for (byte abyte : data) { |
|
143 |
int v = abyte & 0xff; |
|
144 |
builder.append(String.format("%02x", v)); |
|
145 |
} |
|
146 |
return builder.toString(); |
|
147 |
} |
|
148 |
|
|
149 |
@Override |
|
150 |
public String toHexString() { |
|
151 |
byte[] data = this.toByteArray(); |
|
152 |
return bytearray_hex(data); |
|
153 |
} |
|
154 |
|
|
155 |
@Override |
|
156 |
public int getNumberOfPages() { |
|
157 |
if( this.source == null ) { |
|
158 |
return -1; |
|
159 |
} |
|
160 |
if( this.nummberOfPages>=0 ) { |
|
161 |
return this.nummberOfPages; |
|
162 |
} |
|
163 |
try { |
|
164 |
InputStream is = this.getInputStream(); |
|
165 |
this.document = PDDocument.load(is); |
|
166 |
this.nummberOfPages = document.getNumberOfPages(); |
|
167 |
return this.nummberOfPages; |
|
168 |
} catch (IOException ex) { |
|
169 |
return -1; |
|
170 |
} |
|
171 |
} |
|
172 |
|
|
173 |
@Override |
|
174 |
public SimpleImage toImage() { |
|
175 |
return this.toImage(0, 300); |
|
176 |
} |
|
177 |
|
|
178 |
@Override |
|
179 |
public SimpleImage toImage(int page) { |
|
180 |
return this.toImage(page, 300); |
|
181 |
} |
|
182 |
|
|
183 |
@Override |
|
184 |
public SimpleImage toImage(int page, int dpis) { |
|
185 |
if( this.source == null ) { |
|
186 |
return null; |
|
187 |
} |
|
188 |
if( page<0 ) { |
|
189 |
return null; |
|
190 |
} |
|
191 |
try { |
|
192 |
if (this.document == null) { |
|
193 |
InputStream is = this.getInputStream(); |
|
194 |
this.document = PDDocument.load(is); |
|
195 |
this.nummberOfPages = document.getNumberOfPages(); |
|
196 |
} |
|
197 |
if( page>=this.nummberOfPages ) { |
|
198 |
return null; |
|
199 |
} |
|
200 |
PDFRenderer pdfRenderer = new PDFRenderer(document); |
|
201 |
BufferedImage img = pdfRenderer.renderImageWithDPI(page, dpis, ImageType.RGB); |
|
202 |
return ToolsSwingLocator.getToolsSwingManager().createSimpleImage(img); |
|
203 |
} catch (Throwable ex) { |
|
204 |
return null; |
|
205 |
} |
|
206 |
} |
|
207 |
|
|
208 |
@Override |
|
209 |
public void dispose() { |
|
210 |
IOUtils.closeQuietly(document); |
|
211 |
this.source = null; |
|
212 |
this.nummberOfPages = -1; |
|
213 |
this.document = null; |
|
214 |
} |
|
215 |
|
|
216 |
private byte[] html2pdf(String html) { |
|
217 |
// |
|
218 |
// https://github.com/danfickle/openhtmltopdf/issues/661 header/footer |
|
219 |
// |
|
220 |
ByteArrayOutputStream pdfstream = null; |
|
221 |
try { |
|
222 |
if( StringUtils.contains(html, "<br>") ) { |
|
223 |
html = StringUtils.replace(html, "<br>", "<br/>"); |
|
224 |
} |
|
225 |
if( StringUtils.contains(html, " ") ) { |
|
226 |
html = StringUtils.replace(html, " ", " "); |
|
227 |
} |
|
228 |
pdfstream = new ByteArrayOutputStream(); |
|
229 |
|
|
230 |
// |
|
231 |
// https://stackoverflow.com/questions/60524835/html-to-pdf-with-german-alphabet |
|
232 |
// Habria que ver esto ya que falla con otros casos como con los acentros ocute. |
|
233 |
// |
|
234 |
// Parece ser que con algo como lo siguiente se podria evitar. |
|
235 |
// |
|
236 |
//Document jsoupDoc = Jsoup.parse(html); // org.jsoup.nodes.Document |
|
237 |
//W3CDom w3cDom = new W3CDom(); // org.jsoup.helper.W3CDom |
|
238 |
//org.w3c.dom.Document w3cDoc = w3cDom.fromJsoup(jsoupDoc); |
|
239 |
// |
|
240 |
//PdfRendererBuilder builder = new PdfRendererBuilder(); |
|
241 |
//builder.withW3cDocument(w3cDoc, "file://localhost/"); |
|
242 |
|
|
243 |
Document jsoupDoc = Jsoup.parse(html); // org.jsoup.nodes.Document |
|
244 |
W3CDom w3cDom = new W3CDom(); // org.jsoup.helper.W3CDom |
|
245 |
org.w3c.dom.Document w3cDoc = w3cDom.fromJsoup(jsoupDoc); |
|
246 |
|
|
247 |
PdfRendererBuilder builder = new PdfRendererBuilder(); |
|
248 |
builder.useFastMode(); |
|
249 |
builder.toStream(pdfstream); |
|
250 |
// builder.withHtmlContent(html,null); |
|
251 |
builder.withW3cDocument(w3cDoc, "file://localhost/"); |
|
252 |
|
|
253 |
Class cls = this.getClass(); |
|
254 |
builder.useFont(() -> cls.getResourceAsStream("dejavu/DejaVuSans.ttf"), "Sans", 400, FontStyle.NORMAL, true); |
|
255 |
builder.useFont(() -> cls.getResourceAsStream("dejavu/DejaVuSans-Bold.ttf"), "Sans", 700, FontStyle.NORMAL, true); |
|
256 |
builder.useFont(() -> cls.getResourceAsStream("dejavu/DejaVuSans-Oblique.ttf"), "Sans", 400, FontStyle.OBLIQUE, true); |
|
257 |
builder.useFont(() -> cls.getResourceAsStream("dejavu/DejaVuSans-BoldOblique.ttf"), "Sans", 700, FontStyle.OBLIQUE, true); |
|
258 |
|
|
259 |
builder.useFont(() -> cls.getResourceAsStream("dejavu/DejaVuSerif.ttf"), "Serif", 400, FontStyle.NORMAL, true); |
|
260 |
builder.useFont(() -> cls.getResourceAsStream("dejavu/DejaVuSerif-Bold.ttf"), "Serif", 700, FontStyle.NORMAL, true); |
|
261 |
builder.useFont(() -> cls.getResourceAsStream("dejavu/DejaVuSerif-Italic.ttf"), "Serif", 400, FontStyle.ITALIC, true); |
|
262 |
builder.useFont(() -> cls.getResourceAsStream("dejavu/DejaVuSerif-BoldItalic.ttf"), "Serif", 700, FontStyle.ITALIC, true); |
|
263 |
|
|
264 |
builder.useFont(() -> cls.getResourceAsStream("dejavu/DejaVuMono.ttf"), "Mono", 400, FontStyle.NORMAL, true); |
|
265 |
builder.useFont(() -> cls.getResourceAsStream("dejavu/DejaVuMono-Bold.ttf"), "Mono", 700, FontStyle.NORMAL, true); |
|
266 |
builder.useFont(() -> cls.getResourceAsStream("dejavu/DejaVuMono-Oblique.ttf"), "Mono", 400, FontStyle.OBLIQUE, true); |
|
267 |
builder.useFont(() -> cls.getResourceAsStream("dejavu/DejaVuMono-BoldOblique.ttf"), "Mono", 700, FontStyle.OBLIQUE, true); |
|
268 |
|
|
269 |
builder.run(); |
|
270 |
return pdfstream.toByteArray(); |
|
271 |
} catch(Throwable t) { |
|
272 |
LOGGER.warn("Can't convert HTML to PDF.",t); |
|
273 |
return null; |
|
274 |
} finally { |
|
275 |
IOUtils.closeQuietly(pdfstream); |
|
276 |
} |
|
277 |
} |
|
278 |
} |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.impl/src/main/java/org/gvsig/pdf/lib/impl/PDFImplLibrary.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.pdf.lib.impl; |
|
25 |
|
|
26 |
import org.gvsig.fmap.dal.DALLocator; |
|
27 |
import org.gvsig.fmap.dal.DataManager; |
|
28 |
import org.gvsig.fmap.dal.feature.DataProfile; |
|
29 |
import org.gvsig.pdf.lib.api.PDFLibrary; |
|
30 |
import org.gvsig.pdf.lib.api.PDFLocator; |
|
31 |
import org.gvsig.tools.ToolsLibrary; |
|
32 |
import org.gvsig.tools.library.AbstractLibrary; |
|
33 |
import org.gvsig.tools.library.LibraryException; |
|
34 |
import org.gvsig.tools.swing.api.ToolsSwingLibrary; |
|
35 |
|
|
36 |
public class PDFImplLibrary extends AbstractLibrary { |
|
37 |
|
|
38 |
@Override |
|
39 |
public void doRegistration() { |
|
40 |
super.doRegistration(); |
|
41 |
registerAsImplementationOf(PDFLibrary.class); |
|
42 |
this.require(ToolsLibrary.class); |
|
43 |
this.require(ToolsSwingLibrary.class); |
|
44 |
// this.require(DALLibrary.class); |
|
45 |
// this.require(GeometryLibrary.class); |
|
46 |
} |
|
47 |
|
|
48 |
@Override |
|
49 |
protected void doInitialize() throws LibraryException { |
|
50 |
PDFLocator.registerDefaultPDFManager(DefaultPDFManager.class); |
|
51 |
} |
|
52 |
|
|
53 |
@Override |
|
54 |
protected void doPostInitialize() throws LibraryException { |
|
55 |
DataManager dataManager = DALLocator.getDataManager(); |
|
56 |
dataManager.registerDataProfile(new PDFProfile()); |
|
57 |
dataManager.registerDataProfile(new PdfAsImageDataProfile()); |
|
58 |
} |
|
59 |
|
|
60 |
} |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.impl/src/main/java/org/gvsig/pdf/lib/impl/DefaultPDFManager.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.pdf.lib.impl; |
|
25 |
|
|
26 |
import java.io.BufferedOutputStream; |
|
27 |
import java.io.ByteArrayOutputStream; |
|
28 |
import java.io.File; |
|
29 |
import java.io.FileOutputStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InputStream; |
|
32 |
import java.io.OutputStream; |
|
33 |
import java.util.Calendar; |
|
34 |
import java.util.List; |
|
35 |
import org.apache.commons.io.IOUtils; |
|
36 |
import org.apache.commons.io.output.CloseShieldOutputStream; |
|
37 |
import org.apache.pdfbox.cos.COSStream; |
|
38 |
import org.apache.pdfbox.io.MemoryUsageSetting; |
|
39 |
import org.apache.pdfbox.multipdf.PDFMergerUtility; |
|
40 |
import org.apache.pdfbox.pdmodel.PDDocumentInformation; |
|
41 |
import org.apache.pdfbox.pdmodel.common.PDMetadata; |
|
42 |
import org.apache.xmpbox.XMPMetadata; |
|
43 |
import org.apache.xmpbox.schema.DublinCoreSchema; |
|
44 |
import org.apache.xmpbox.schema.PDFAIdentificationSchema; |
|
45 |
import org.apache.xmpbox.schema.XMPBasicSchema; |
|
46 |
import org.apache.xmpbox.xml.XmpSerializer; |
|
47 |
import org.gvsig.pdf.lib.api.PDFDocument; |
|
48 |
import org.gvsig.pdf.lib.api.PDFManager; |
|
49 |
import org.gvsig.pdf.lib.api.PDFWriter; |
|
50 |
import org.gvsig.tools.ToolsLocator; |
|
51 |
import org.gvsig.tools.folders.FoldersManager; |
|
52 |
import org.gvsig.tools.task.SimpleTaskStatus; |
|
53 |
|
|
54 |
public class DefaultPDFManager implements PDFManager { |
|
55 |
|
|
56 |
@Override |
|
57 |
public PDFDocument createPDFDocument(byte[] document) { |
|
58 |
DefaultPDFDocument doc = new DefaultPDFDocument(); |
|
59 |
doc.setSource(document); |
|
60 |
return doc; |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public PDFDocument createPDFDocument(String document) { |
|
65 |
DefaultPDFDocument doc = new DefaultPDFDocument(); |
|
66 |
doc.setSource(document); |
|
67 |
return doc; |
|
68 |
} |
|
69 |
|
|
70 |
@Override |
|
71 |
public PDFDocument createPDFDocument(File document) { |
|
72 |
DefaultPDFDocument doc = new DefaultPDFDocument(); |
|
73 |
doc.setSource(document); |
|
74 |
return doc; |
|
75 |
} |
|
76 |
|
|
77 |
@Override |
|
78 |
public PDFDocument createPDFDocument() { |
|
79 |
DefaultPDFDocument doc = new DefaultPDFDocument(); |
|
80 |
return doc; |
|
81 |
} |
|
82 |
|
|
83 |
@Override |
|
84 |
public PDFWriter createPDFWriter() { |
|
85 |
return new DefaultPDFWriter(); |
|
86 |
} |
|
87 |
|
|
88 |
@Override |
|
89 |
public void merge(List<PDFDocument> inputs, OutputStream output, long maxMemoryToUse, String title, String creator, String subject) { |
|
90 |
try { |
|
91 |
PDFMergerUtility pdfMerger = new PDFMergerUtility(); |
|
92 |
pdfMerger.setDestinationStream(output); |
|
93 |
|
|
94 |
PDDocumentInformation documentInformation = new PDDocumentInformation(); |
|
95 |
documentInformation.setTitle(title); |
|
96 |
documentInformation.setCreator(creator); |
|
97 |
documentInformation.setSubject(subject); |
|
98 |
|
|
99 |
for (PDFDocument input : inputs) { |
|
100 |
pdfMerger.addSource(input.getInputStream()); |
|
101 |
} |
|
102 |
FoldersManager folderManager = ToolsLocator.getFoldersManager(); |
|
103 |
pdfMerger.mergeDocuments( |
|
104 |
MemoryUsageSetting.setupMixed(maxMemoryToUse) |
|
105 |
.setTempDir(folderManager.getTemporaryFolder()) |
|
106 |
); |
|
107 |
} catch (IOException ex) { |
|
108 |
throw new RuntimeException("Can't merge pdf documents", ex); |
|
109 |
} |
|
110 |
} |
|
111 |
|
|
112 |
@Override |
|
113 |
public void merge(List inputPdfs, File outputFile, String title, String creator, String subject, SimpleTaskStatus status) { |
|
114 |
FileOutputStream os = null; |
|
115 |
try { |
|
116 |
os = new FileOutputStream(outputFile); |
|
117 |
merge(inputPdfs, os, title, creator, subject, status); |
|
118 |
} catch (Exception ex) { |
|
119 |
throw new RuntimeException("Can't merge pdf's", ex); |
|
120 |
} finally { |
|
121 |
IOUtils.closeQuietly(os); |
|
122 |
} |
|
123 |
|
|
124 |
} |
|
125 |
|
|
126 |
@Override |
|
127 |
public void merge(List inputPdfs, OutputStream outputPdf, String title, String creator, String subject, SimpleTaskStatus status) { |
|
128 |
COSStream cosStream = null; |
|
129 |
OutputStream bos = null; |
|
130 |
try { |
|
131 |
status.push(); |
|
132 |
status.setRangeOfValues(1, inputPdfs.size()); |
|
133 |
PDFMergerUtility pdfMerger = new PDFMergerUtility(); |
|
134 |
cosStream = new COSStream(); |
|
135 |
if(outputPdf instanceof BufferedOutputStream){ |
|
136 |
bos = CloseShieldOutputStream.wrap(outputPdf); |
|
137 |
} else { |
|
138 |
bos = new BufferedOutputStream(CloseShieldOutputStream.wrap(outputPdf)); |
|
139 |
} |
|
140 |
pdfMerger.setDestinationStream(bos); |
|
141 |
PDDocumentInformation pdfDocumentInfo = createPDFDocumentInfo(title, creator, subject); |
|
142 |
PDMetadata xmpMetadata = createXMPMetadata(cosStream, title, creator, subject); |
|
143 |
pdfMerger.setDestinationDocumentInformation(pdfDocumentInfo); |
|
144 |
pdfMerger.setDestinationMetadata(xmpMetadata); |
|
145 |
for (Object pdf : inputPdfs) { |
|
146 |
if(pdf instanceof File){ |
|
147 |
pdfMerger.addSource((File)pdf); |
|
148 |
} else if(pdf instanceof PDFDocument){ |
|
149 |
pdfMerger.addSource(((PDFDocument)pdf).getInputStream()); |
|
150 |
} else if(pdf instanceof InputStream){ |
|
151 |
pdfMerger.addSource((InputStream)pdf); |
|
152 |
} |
|
153 |
status.incrementCurrentValue(); |
|
154 |
} |
|
155 |
pdfMerger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly()); |
|
156 |
} catch (Exception ex) { |
|
157 |
throw new RuntimeException("Can't merge pdf's", ex); |
|
158 |
} finally { |
|
159 |
IOUtils.closeQuietly(bos); |
|
160 |
IOUtils.closeQuietly(cosStream); |
|
161 |
status.pop(); |
|
162 |
} |
|
163 |
|
|
164 |
} |
|
165 |
|
|
166 |
private PDDocumentInformation createPDFDocumentInfo(String title, String creator, String subject) |
|
167 |
{ |
|
168 |
// LOG.info("Setting document info (title, author, subject) for merged PDF"); |
|
169 |
PDDocumentInformation documentInformation = new PDDocumentInformation(); |
|
170 |
documentInformation.setTitle(title); |
|
171 |
documentInformation.setCreator(creator); |
|
172 |
documentInformation.setSubject(subject); |
|
173 |
return documentInformation; |
|
174 |
} |
|
175 |
|
|
176 |
private PDMetadata createXMPMetadata(COSStream cosStream, String title, String creator, String subject) |
|
177 |
throws Exception |
|
178 |
{ |
|
179 |
// LOG.info("Setting XMP metadata (title, author, subject) for merged PDF"); |
|
180 |
XMPMetadata xmpMetadata = XMPMetadata.createXMPMetadata(); |
|
181 |
|
|
182 |
// PDF/A-1b properties |
|
183 |
PDFAIdentificationSchema pdfaSchema = xmpMetadata.createAndAddPFAIdentificationSchema(); |
|
184 |
pdfaSchema.setPart(1); |
|
185 |
pdfaSchema.setConformance("B"); |
|
186 |
|
|
187 |
// Dublin Core properties |
|
188 |
DublinCoreSchema dublinCoreSchema = xmpMetadata.createAndAddDublinCoreSchema(); |
|
189 |
dublinCoreSchema.setTitle(title); |
|
190 |
dublinCoreSchema.addCreator(creator); |
|
191 |
dublinCoreSchema.setDescription(subject); |
|
192 |
|
|
193 |
// XMP Basic properties |
|
194 |
XMPBasicSchema basicSchema = xmpMetadata.createAndAddXMPBasicSchema(); |
|
195 |
Calendar creationDate = Calendar.getInstance(); |
|
196 |
basicSchema.setCreateDate(creationDate); |
|
197 |
basicSchema.setModifyDate(creationDate); |
|
198 |
basicSchema.setMetadataDate(creationDate); |
|
199 |
basicSchema.setCreatorTool(creator); |
|
200 |
|
|
201 |
// Create and return XMP data structure in XML format |
|
202 |
try (ByteArrayOutputStream xmpOutputStream = new ByteArrayOutputStream(); |
|
203 |
OutputStream cosXMPStream = cosStream.createOutputStream()) |
|
204 |
{ |
|
205 |
new XmpSerializer().serialize(xmpMetadata, xmpOutputStream, true); |
|
206 |
cosXMPStream.write(xmpOutputStream.toByteArray()); |
|
207 |
return new PDMetadata(cosStream); |
|
208 |
} |
|
209 |
} |
|
210 |
|
|
211 |
|
|
212 |
} |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.impl/src/main/java/org/gvsig/pdf/lib/impl/PDFProfile.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.pdf.lib.impl; |
|
7 |
|
|
8 |
import java.io.File; |
|
9 |
import org.gvsig.fmap.dal.feature.AbstractDataProfile; |
|
10 |
import org.gvsig.pdf.lib.api.PDFDocument; |
|
11 |
import org.gvsig.pdf.lib.api.PDFLocator; |
|
12 |
import org.gvsig.pdf.lib.api.PDFManager; |
|
13 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
14 |
import org.gvsig.tools.dataTypes.DataType; |
|
15 |
import org.gvsig.tools.dataTypes.DataTypes; |
|
16 |
import org.gvsig.tools.dynobject.Tags; |
|
17 |
|
|
18 |
public class PDFProfile extends AbstractDataProfile { |
|
19 |
|
|
20 |
/*friend*/PDFProfile() { |
|
21 |
super("PDF", File.class); |
|
22 |
} |
|
23 |
|
|
24 |
@Override |
|
25 |
protected Object doCreateData(Object pdf, Tags tags) { |
|
26 |
PDFManager manager = PDFLocator.getPDFManager(); |
|
27 |
PDFDocument pdfdoc = manager.createPDFDocument(); |
|
28 |
pdfdoc.setSource(pdf); |
|
29 |
return pdfdoc; |
|
30 |
} |
|
31 |
|
|
32 |
@Override |
|
33 |
protected Object doCoerce(DataType dataType, Object data, Tags tags) throws CoercionException { |
|
34 |
PDFDocument document; |
|
35 |
try { |
|
36 |
switch (dataType.getType()) { |
|
37 |
case DataTypes.BYTEARRAY: |
|
38 |
document = PDFLocator.getPDFManager().createPDFDocument(); |
|
39 |
document.setSource(data); |
|
40 |
return document.toByteArray(); |
|
41 |
case DataTypes.FILE: |
|
42 |
if (data instanceof File) { |
|
43 |
return data; |
|
44 |
} |
|
45 |
case DataTypes.STRING: |
|
46 |
document = PDFLocator.getPDFManager().createPDFDocument(); |
|
47 |
document.setSource(data); |
|
48 |
return document.toHexString(); |
|
49 |
default: |
|
50 |
break; |
|
51 |
} |
|
52 |
} catch (Exception ex) { |
|
53 |
throw new CoercionException("Can't convert pdf to " + dataType.getName(), ex); |
|
54 |
} |
|
55 |
throw new CoercionException("Can't convert pdf to " + dataType.getName()); |
|
56 |
} |
|
57 |
|
|
58 |
} |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.impl/src/main/resources/META-INF/services/org.gvsig.tools.library.Library | ||
---|---|---|
1 |
org.gvsig.pdf.lib.impl.PDFImplLibrary |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.impl/src/main/resources/org/gvsig/pdf/lib/impl/dejavu/LICENSE | ||
---|---|---|
1 |
Fonts are (c) Bitstream (see below). DejaVu changes are in public domain. |
|
2 |
Glyphs imported from Arev fonts are (c) Tavmjong Bah (see below) |
|
3 |
|
|
4 |
Bitstream Vera Fonts Copyright |
|
5 |
------------------------------ |
|
6 |
|
|
7 |
Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera is |
|
8 |
a trademark of Bitstream, Inc. |
|
9 |
|
|
10 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
|
11 |
of the fonts accompanying this license ("Fonts") and associated |
|
12 |
documentation files (the "Font Software"), to reproduce and distribute the |
|
13 |
Font Software, including without limitation the rights to use, copy, merge, |
|
14 |
publish, distribute, and/or sell copies of the Font Software, and to permit |
|
15 |
persons to whom the Font Software is furnished to do so, subject to the |
|
16 |
following conditions: |
|
17 |
|
|
18 |
The above copyright and trademark notices and this permission notice shall |
|
19 |
be included in all copies of one or more of the Font Software typefaces. |
|
20 |
|
|
21 |
The Font Software may be modified, altered, or added to, and in particular |
|
22 |
the designs of glyphs or characters in the Fonts may be modified and |
|
23 |
additional glyphs or characters may be added to the Fonts, only if the fonts |
|
24 |
are renamed to names not containing either the words "Bitstream" or the word |
|
25 |
"Vera". |
|
26 |
|
|
27 |
This License becomes null and void to the extent applicable to Fonts or Font |
|
28 |
Software that has been modified and is distributed under the "Bitstream |
|
29 |
Vera" names. |
|
30 |
|
|
31 |
The Font Software may be sold as part of a larger software package but no |
|
32 |
copy of one or more of the Font Software typefaces may be sold by itself. |
|
33 |
|
|
34 |
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|
35 |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, |
|
36 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, |
|
37 |
TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME |
|
38 |
FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING |
|
39 |
ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, |
|
40 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF |
|
41 |
THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE |
|
42 |
FONT SOFTWARE. |
|
43 |
|
|
44 |
Except as contained in this notice, the names of Gnome, the Gnome |
|
45 |
Foundation, and Bitstream Inc., shall not be used in advertising or |
|
46 |
otherwise to promote the sale, use or other dealings in this Font Software |
|
47 |
without prior written authorization from the Gnome Foundation or Bitstream |
|
48 |
Inc., respectively. For further information, contact: fonts at gnome dot |
|
49 |
org. |
|
50 |
|
|
51 |
Arev Fonts Copyright |
|
52 |
------------------------------ |
|
53 |
|
|
54 |
Copyright (c) 2006 by Tavmjong Bah. All Rights Reserved. |
|
55 |
|
|
56 |
Permission is hereby granted, free of charge, to any person obtaining |
|
57 |
a copy of the fonts accompanying this license ("Fonts") and |
|
58 |
associated documentation files (the "Font Software"), to reproduce |
|
59 |
and distribute the modifications to the Bitstream Vera Font Software, |
|
60 |
including without limitation the rights to use, copy, merge, publish, |
|
61 |
distribute, and/or sell copies of the Font Software, and to permit |
|
62 |
persons to whom the Font Software is furnished to do so, subject to |
|
63 |
the following conditions: |
|
64 |
|
|
65 |
The above copyright and trademark notices and this permission notice |
|
66 |
shall be included in all copies of one or more of the Font Software |
|
67 |
typefaces. |
|
68 |
|
|
69 |
The Font Software may be modified, altered, or added to, and in |
|
70 |
particular the designs of glyphs or characters in the Fonts may be |
|
71 |
modified and additional glyphs or characters may be added to the |
|
72 |
Fonts, only if the fonts are renamed to names not containing either |
|
73 |
the words "Tavmjong Bah" or the word "Arev". |
|
74 |
|
|
75 |
This License becomes null and void to the extent applicable to Fonts |
|
76 |
or Font Software that has been modified and is distributed under the |
|
77 |
"Tavmjong Bah Arev" names. |
|
78 |
|
|
79 |
The Font Software may be sold as part of a larger software package but |
|
80 |
no copy of one or more of the Font Software typefaces may be sold by |
|
81 |
itself. |
|
82 |
|
|
83 |
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
84 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF |
|
85 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT |
|
86 |
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL |
|
87 |
TAVMJONG BAH BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
88 |
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL |
|
89 |
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
90 |
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM |
|
91 |
OTHER DEALINGS IN THE FONT SOFTWARE. |
|
92 |
|
|
93 |
Except as contained in this notice, the name of Tavmjong Bah shall not |
|
94 |
be used in advertising or otherwise to promote the sale, use or other |
|
95 |
dealings in this Font Software without prior written authorization |
|
96 |
from Tavmjong Bah. For further information, contact: tavmjong @ free |
|
97 |
. fr. |
|
98 |
|
|
99 |
$Id: LICENSE 2133 2007-11-28 02:46:28Z lechimp $ |
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.156/org.gvsig.pdf.lib/org.gvsig.pdf.lib.impl/pom.xml | ||
---|---|---|
1 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
2 |
<modelVersion>4.0.0</modelVersion> |
|
3 |
<parent> |
|
4 |
<groupId>org.gvsig</groupId> |
|
5 |
<artifactId>org.gvsig.pdf.lib</artifactId> |
|
6 |
<version>1.0.156</version> |
|
7 |
</parent> |
|
8 |
<artifactId>org.gvsig.pdf.lib.impl</artifactId> |
|
9 |
<name>${project.artifactId}</name> |
|
10 |
<dependencies> |
|
11 |
<dependency> |
|
12 |
<groupId>org.gvsig</groupId> |
|
13 |
<artifactId>org.gvsig.pdf.lib.api</artifactId> |
|
14 |
<scope>compile</scope> |
|
15 |
</dependency> |
|
16 |
<dependency> |
|
17 |
<groupId>org.gvsig</groupId> |
|
18 |
<artifactId>org.gvsig.tools.swing.api</artifactId> |
|
19 |
<scope>compile</scope> |
|
20 |
</dependency> |
|
21 |
<dependency> |
|
22 |
<groupId>org.gvsig</groupId> |
|
23 |
<artifactId>org.gvsig.tools.swing.impl</artifactId> |
|
24 |
<scope>test</scope> |
|
25 |
</dependency> |
|
26 |
<dependency> |
|
27 |
<groupId>org.gvsig</groupId> |
|
28 |
<artifactId>org.gvsig.tools.lib</artifactId> |
|
29 |
<scope>test</scope> |
|
30 |
<type>test-jar</type> |
|
31 |
</dependency> |
|
32 |
<dependency> |
|
33 |
<groupId>org.gvsig</groupId> |
|
34 |
<artifactId>org.gvsig.fmap.dal.api</artifactId> |
|
35 |
<scope>compile</scope> |
|
36 |
</dependency> |
|
37 |
<dependency> |
|
38 |
<groupId>org.gvsig</groupId> |
|
39 |
<artifactId>org.gvsig.fmap.dal.swing.api</artifactId> |
|
40 |
<scope>compile</scope> |
|
41 |
</dependency> |
|
42 |
<dependency> |
|
43 |
<groupId>commons-codec</groupId> |
|
44 |
<artifactId>commons-codec</artifactId> |
|
45 |
<scope>compile</scope> |
|
46 |
</dependency> |
|
47 |
<!-- <dependency> |
|
48 |
<groupId>com.lowagie</groupId> |
Also available in: Unified diff