Revision 7210

View differences:

org.gvsig.pdf/tags/org.gvsig.pdf-1.0.103/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.103/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

  
30
/**
31
 *
32
 * @author jjdelcerro
33
 */
34
public interface PDFManager {
35
    
36
    public PDFDocument createPDFDocument(byte[] document);
37
    public PDFDocument createPDFDocument(String document);
38
    public PDFDocument createPDFDocument(File document);
39
    public PDFDocument createPDFDocument(); 
40
    public PDFWriter createPDFWriter();
41
    public void merge(List<PDFDocument> inputs, 
42
            OutputStream output, 
43
            long maxMemoryToUse, 
44
            String title, 
45
            String creator, 
46
            String subject
47
    );
48
    
49
    
50
}
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.103/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.103/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.103/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.103/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.103/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.103</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.fmap.dal.api</artifactId>
19
      <scope>compile</scope>
20
    </dependency>
21
  </dependencies>
22

  
23
</project>
0 24

  
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.103/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.103</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.103/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.103</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>
49
            <artifactId>itext</artifactId>
50
            <scope>compile</scope>
51
        </dependency>-->
52
        <dependency>
53
            <groupId>org.apache.pdfbox</groupId>
54
            <artifactId>pdfbox</artifactId>
55
        </dependency>
56
        <dependency>
57
            <groupId>de.rototor.pdfbox</groupId>
58
            <artifactId>graphics2d</artifactId>
59
        </dependency>
60
        <dependency>
61
            <groupId>org.apache.tika</groupId>
62
            <artifactId>tika-core</artifactId>
63
        </dependency>
64
    </dependencies>
65
</project>
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.103/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.103/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.103/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.File;
27
import java.io.IOException;
28
import java.io.OutputStream;
29
import java.util.List;
30
import java.util.logging.Level;
31
import java.util.logging.Logger;
32
import org.apache.pdfbox.io.MemoryUsageSetting;
33
import org.apache.pdfbox.multipdf.PDFMergerUtility;
34
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
35
import org.gvsig.pdf.lib.api.PDFDocument;
36
import org.gvsig.pdf.lib.api.PDFManager;
37
import org.gvsig.pdf.lib.api.PDFWriter;
38
import org.gvsig.tools.ToolsLocator;
39
import org.gvsig.tools.folders.FoldersManager;
40

  
41
public class DefaultPDFManager implements PDFManager {
42

  
43
    @Override
44
    public PDFDocument createPDFDocument(byte[] document) {
45
        DefaultPDFDocument doc = new DefaultPDFDocument();
46
        doc.setSource(document);
47
        return doc;
48
    }
49

  
50
    @Override
51
    public PDFDocument createPDFDocument(String document) {
52
        DefaultPDFDocument doc = new DefaultPDFDocument();
53
        doc.setSource(document);
54
        return doc;
55
    }
56

  
57
    @Override
58
    public PDFDocument createPDFDocument(File document) {
59
        DefaultPDFDocument doc = new DefaultPDFDocument();
60
        doc.setSource(document);
61
        return doc;
62
    }
63
   
64
    @Override
65
    public PDFDocument createPDFDocument() {
66
        DefaultPDFDocument doc = new DefaultPDFDocument();
67
        return doc;
68
    }
69

  
70
    @Override
71
    public PDFWriter createPDFWriter() {
72
        return new DefaultPDFWriter();
73
    }
74
    
75
    public void merge(List<PDFDocument> inputs, OutputStream output, long maxMemoryToUse, String title, String creator, String subject) {
76
        try {
77
            PDFMergerUtility pdfMerger = new PDFMergerUtility();
78
            pdfMerger.setDestinationStream(output);
79

  
80
            PDDocumentInformation documentInformation = new PDDocumentInformation();
81
            documentInformation.setTitle(title);
82
            documentInformation.setCreator(creator);
83
            documentInformation.setSubject(subject);
84

  
85
            for (PDFDocument input : inputs) {
86
                pdfMerger.addSource(input.getInputStream());
87
            }
88
            FoldersManager folderManager = ToolsLocator.getFoldersManager();
89
            pdfMerger.mergeDocuments(
90
                    MemoryUsageSetting.setupMixed(maxMemoryToUse)
91
                            .setTempDir(folderManager.getTemporaryFolder())
92
            );        
93
        } catch (IOException ex) {
94
            throw new RuntimeException("Can't merge pdf documents", ex);
95
        }
96
    }
97
}
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.103/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.103/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.103/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.103/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 java.awt.image.BufferedImage;
4
import java.io.ByteArrayInputStream;
5
import java.io.File;
6
import java.io.FileInputStream;
7
import java.io.FileNotFoundException;
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.net.URL;
11
import org.gvsig.pdf.lib.api.PDFDocument;
12
import org.slf4j.LoggerFactory;
13
import org.apache.commons.codec.binary.Hex;
14
import org.apache.commons.codec.DecoderException;
15
import org.apache.commons.codec.binary.Base64;
16
import org.apache.commons.io.IOUtils;
17
import org.apache.pdfbox.pdmodel.PDDocument;
18
import org.apache.pdfbox.rendering.ImageType;
19
import org.apache.pdfbox.rendering.PDFRenderer;
20
import org.gvsig.tools.swing.api.SimpleImage;
21
import org.gvsig.tools.swing.api.ToolsSwingLocator;
22

  
23
/**
24
 *
25
 * @author gvSIG Team
26
 */
27
@SuppressWarnings("UseSpecificCatch")
28
public class DefaultPDFDocument implements PDFDocument {
29

  
30
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DefaultPDFDocument.class);
31
    private Object source;
32
    private int nummberOfPages;
33
    private PDDocument document;
34

  
35
    public DefaultPDFDocument() {
36
        this.nummberOfPages = -1;
37
        this.document = null;
38
    }
39

  
40
    @Override
41
    public void setSource(Object source) {
42
        this.source = source;
43
        this.nummberOfPages = -1;
44
        this.document = null;
45
    }
46

  
47
    @Override
48
    public InputStream getInputStream() throws IOException {
49
        InputStream is = null;
50
        if (source == null) {
51
            return null;
52
        } else if (source instanceof InputStream) {
53
            return (InputStream) source;
54
        } else if (source instanceof File) {
55
            File fileSource = (File) source;
56
            try {
57
                is = new FileInputStream(fileSource);
58
            } catch (IOException ex) {
59
                LOGGER.warn("Not been able to convert file to input stream", ex);
60
                throw new IOException(ex);
61
            }
62
        } else if (source instanceof URL) {
63
            URL urlSource = (URL) source;
64
            try {
65
                is = urlSource.openStream();
66
            } catch (IOException ex) {
67
                LOGGER.warn("Not been able to open URL stream", ex);
68
                throw new IOException(ex);
69
            }
70
        } else if (source instanceof String) {
71
            String strSource = (String) source;
72
            File f = new File(strSource);
73
            if (f.exists()) {
74
                try {
75
                    is = new FileInputStream(f);
76
                } catch (FileNotFoundException ex) {
77
                    LOGGER.warn("Not been able to load existing file", ex);
78
                    throw new IOException(ex);
79
                }
80
            } else {
81
                try {
82
                    URL url = new URL(strSource);
83
                    is = url.openStream();
84
                    is.available(); // Force exception if is null
85
                } catch (Exception ex) {
86
                    try {
87
                        byte[] data = Hex.decodeHex(strSource.toCharArray());
88
                        is = new ByteArrayInputStream(data);
89
                    } catch (DecoderException ex2) {
90
                        try {
91
                            byte[] data = Base64.decodeBase64(strSource);
92
                            is = new ByteArrayInputStream(data);
93
                        } catch (Exception ex3) {
94
                            throw new IOException(ex3);
95
                        }
96
                    }
97
                }
98
            }
99
        } else if (source instanceof byte[]) {
100
            byte[] byteSource = (byte[]) source;
101
            is = new ByteArrayInputStream(byteSource);
102
        }
103

  
104
        return is;
105
    }
106

  
107
    @Override
108
    public byte[] toByteArray() {
109
        if (this.source != null) {
110
            try {
111
                InputStream is = this.getInputStream();
112
                byte[] bytes = new byte[is.available()];
113
                is.read(bytes);
114
                return bytes;
115
            } catch (IOException ex) {
116
                LOGGER.warn("Not been able to convert pdf to byte array", ex);
117
            }
118
        }
119
        return null;
120

  
121
    }
122

  
123
    private String bytearray_hex(byte[] data) {
124
        StringBuilder builder = new StringBuilder();
125
        for (byte abyte : data) {
126
            int v = abyte & 0xff;
127
            builder.append(String.format("%02x", v));
128
        }
129
        return builder.toString();
130
    }
131

  
132
    @Override
133
    public String toHexString() {
134
        byte[] data = this.toByteArray();
135
        return bytearray_hex(data);
136
    }    
137
    
138
    @Override
139
    public int getNumberOfPages() {
140
        if( this.source == null ) {
141
            return -1;
142
        }
143
        if( this.nummberOfPages>=0 ) {
144
            return this.nummberOfPages;
145
        }
146
        try {
147
            InputStream is = this.getInputStream();
148
            this.document = PDDocument.load(is);
149
            this.nummberOfPages = document.getNumberOfPages();
150
            return this.nummberOfPages;
151
        } catch (IOException ex) {
152
            return -1;
153
        }
154
    }
155
    
156
    @Override
157
    public SimpleImage toImage() {
158
        return this.toImage(0, 300);
159
    }
160
    
161
    @Override
162
    public SimpleImage toImage(int page) {
163
        return this.toImage(page, 300);
164
    }
165
    
166
    @Override
167
    public SimpleImage toImage(int page, int dpis) {
168
        if( this.source == null ) {
169
            return null;
170
        }
171
        if( page<0 ) {
172
            return null;
173
        }
174
        try {
175
            if (this.document == null) {
176
                InputStream is = this.getInputStream();
177
                this.document = PDDocument.load(is);
178
                this.nummberOfPages = document.getNumberOfPages();
179
            }
180
            if( page>=this.nummberOfPages ) {
181
                return null;
182
            }
183
            PDFRenderer pdfRenderer = new PDFRenderer(document);
184
            BufferedImage img = pdfRenderer.renderImageWithDPI(page, dpis, ImageType.RGB);
185
            return ToolsSwingLocator.getToolsSwingManager().createSimpleImage(img);
186
        } catch (Throwable ex) {
187
            return null;
188
        }
189
    }
190

  
191
    @Override
192
    public void dispose() {
193
        IOUtils.closeQuietly(document);
194
        this.source = null;
195
        this.nummberOfPages = -1;
196
        this.document = null;
197
    }
198
}
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.103/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
}
0 61

  
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.103/org.gvsig.pdf.swing/org.gvsig.pdf.swing.impl/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.pdf.swing.impl.PDFSwingImplLibrary
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.103/org.gvsig.pdf.swing/org.gvsig.pdf.swing.impl/src/main/java/org/gvsig/pdf/swing/impl/DefaultPDFSwingManager.java
1

  
2
package org.gvsig.pdf.swing.impl;
3

  
4
import org.gvsig.pdf.lib.api.PDFDocument;
5
import org.gvsig.pdf.swing.api.EmbeddedViewer;
6
import org.gvsig.pdf.swing.impl.viewer.DefaultPDFViewer;
7
import org.gvsig.pdf.swing.api.PDFSwingManager;
8
import org.gvsig.pdf.swing.api.PDFViewer;
9
import org.gvsig.pdf.swing.impl.viewer.EmbeddedViewerPDF;
10

  
11

  
12

  
13
/**
14
 *
15
 * @author jjdelcerro
16
 */
17
public class DefaultPDFSwingManager implements PDFSwingManager {
18

  
19
    @Override
20
    public PDFViewer createPDFViewer() {
21
        return new DefaultPDFViewer();
22
    }
23

  
24
    @Override
25
    public EmbeddedViewer<PDFDocument> createEmbeddedPDFViewer() {
26
        return new EmbeddedViewerPDF();
27
    }
28

  
29
    
30
}
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.103/org.gvsig.pdf.swing/org.gvsig.pdf.swing.impl/src/main/java/org/gvsig/pdf/swing/impl/viewer/DefaultPDFViewer.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.swing.impl.viewer;
7

  
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.util.Properties;
11
import java.util.ResourceBundle;
12
import javax.swing.JComponent;
13
import javax.swing.JPanel;
14
import org.apache.commons.io.IOUtils;
15
import org.gvsig.pdf.lib.api.PDFDocument;
16
import org.gvsig.pdf.swing.api.PDFViewer;
17
import org.gvsig.tools.dispose.DisposeUtils;
18
import org.icepdf.ri.common.SwingController;
19
import org.icepdf.ri.common.SwingViewBuilder;
20
import org.icepdf.ri.common.views.DocumentViewController;
21
import org.icepdf.ri.common.views.DocumentViewControllerImpl;
22
import org.icepdf.ri.util.PropertiesManager;
23
import org.slf4j.LoggerFactory;
24

  
25
public class DefaultPDFViewer implements PDFViewer {
26

  
27
    private PDFDocument pdf;
28
    private SwingController controller;
29
    private JPanel component;
30
    private int mode;
31
    private static final org.slf4j.Logger LOGGER = LoggerFactory
32
            .getLogger(DefaultPDFViewer.class);
33

  
34
    public DefaultPDFViewer() {
35
        this.component = null;
36
        this.pdf = null;
37
        this.controller = new SwingController();
38
        this.controller.setIsEmbeddedComponent(true);
39
        this.mode = PDFViewer.MODE_LIGHT;
40
    }
41

  
42
    public void setMode(int mode) {
43
        //si ya esta iniciado el component != null IllegalStateExcepcion
44
        this.mode = mode;
45
    }
46

  
47
    protected void buildLightViewer() {
48

  
49
        Properties props = new Properties();
50
        props.setProperty(PropertiesManager.PROPERTY_VIEWPREF_FORM_HIGHLIGHT, "false");
51
        props.setProperty(PropertiesManager.PROPERTY_SHOW_UTILITY_SAVE, "false");
52
//        props.setProperty(PropertiesManager.PROPERTY_DEFAULT_ZOOM_LEVEL, "0.7");
53
        props.setProperty(PropertiesManager.PROPERTY_SHOW_UTILITY_SAVE, "false");
54
        props.setProperty(PropertiesManager.PROPERTY_SHOW_UTILITY_OPEN, "false");
55
        props.setProperty(PropertiesManager.PROPERTY_SHOW_UTILITY_PRINT, "false");
56
        props.setProperty(PropertiesManager.PROPERTY_SHOW_UTILITY_UPANE, "false");
57
        props.setProperty(PropertiesManager.PROPERTY_SHOW_TOOLBAR_ANNOTATION_HIGHLIGHT, "false");
58
        props.setProperty(PropertiesManager.PROPERTY_SHOW_TOOLBAR_ANNOTATION_SELECTION, "false");
59
        props.setProperty(PropertiesManager.PROPERTY_SHOW_TOOLBAR_ANNOTATION_TEXT, "false");
60
        props.setProperty(PropertiesManager.PROPERTY_SHOW_TOOLBAR_ANNOTATION, "false");
61
        props.setProperty(PropertiesManager.PROPERTY_SHOW_TOOLBAR_TOOL, "false");
62

  
63
        props.setProperty("application.showLocalStorageDialogs", "false");
64
        PropertiesManager propertiesManager = new PropertiesManager(
65
                System.getProperties(),
66
                props,
67
                ResourceBundle.getBundle(PropertiesManager.DEFAULT_MESSAGE_BUNDLE));
68
        propertiesManager.setInt("document.viewtype", DocumentViewControllerImpl.ONE_COLUMN_VIEW);
69
        propertiesManager.setInt(PropertiesManager.PROPERTY_DEFAULT_PAGEFIT, DocumentViewController.PAGE_FIT_WINDOW_WIDTH);
70

  
71
        SwingViewBuilder viewBuilder = new SwingViewBuilder(controller, propertiesManager);
72
        this.component = viewBuilder.buildViewerPanel();
73
    }
74

  
75
    protected void buildCompleteViewer() {
76
        SwingViewBuilder frameBuilder = new SwingViewBuilder(controller);
77
        this.component = frameBuilder.buildViewerPanel();
78
    }
79

  
80
    public PDFDocument getPDF() {
81
        return this.pdf;
82
    }
83

  
84
    @Override
85
    public void put(PDFDocument pdf) throws IOException {
86
        this.pdf = pdf;
87
        if (pdf != null) {
88
            InputStream in = null;
89
            try {
90
                in = pdf.getInputStream();
91
                controller.openDocument(in, "PDF", null);
92
            } catch (IOException ex) {
93
                LOGGER.warn("Can't open document pdf", ex);
94
                throw new IOException(ex);
95
            } finally {
96
                IOUtils.closeQuietly(in);
97
            }
98
        }
99
    }
100

  
101
    @Override
102
    public JComponent asJComponent() {
103
        if (this.component == null) {
104

  
105
            if (this.mode == PDFViewer.MODE_LIGHT) {
106
                this.buildLightViewer();
107
            } else if (this.mode == PDFViewer.MODE_COMPLETE) {
108
                this.buildCompleteViewer();
109
            }
110
        }
111
        return this.component;
112
    }
113

  
114
    @Override
115
    public void clean() {
116
        this.controller.closeDocument();
117
    }
118

  
119
    @Override
120
    public void dispose() {
121
        this.controller.dispose();
122
        this.controller = null;
123
        DisposeUtils.disposeQuietly(this.pdf);
124
        this.pdf = null;
125
    }
126

  
127
}
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.103/org.gvsig.pdf.swing/org.gvsig.pdf.swing.impl/src/main/java/org/gvsig/pdf/swing/impl/viewer/EmbeddedViewerPDF.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.swing.impl.viewer;
7

  
8
import java.awt.Dimension;
9
import java.awt.event.ActionEvent;
10
import java.io.IOException;
11
import javax.swing.AbstractAction;
12
import javax.swing.Action;
13
import javax.swing.JComponent;
14
import org.gvsig.pdf.lib.api.PDFDocument;
15
import org.gvsig.pdf.swing.api.PDFSwingLocator;
16
import org.gvsig.pdf.swing.api.PDFViewer;
17
import org.gvsig.tools.ToolsLocator;
18
import org.gvsig.tools.i18n.I18nManager;
19
import org.gvsig.tools.swing.api.ToolsSwingLocator;
20
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
21
import org.gvsig.tools.swing.icontheme.IconTheme;
22
import org.gvsig.pdf.swing.api.EmbeddedViewer;
23
import org.gvsig.tools.util.Invocable;
24

  
25
/**
26
 *
27
 * @author jjdelcerro
28
 */
29
public class EmbeddedViewerPDF implements EmbeddedViewer<PDFDocument> {
30

  
31

  
32
    private class ShowInWindowAction extends AbstractAction {
33

  
34
        @SuppressWarnings("OverridableMethodCallInConstructor")
35
        public ShowInWindowAction() {
36
            I18nManager i18n = ToolsLocator.getI18nManager();
37
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent();
38
            putValue(Action.NAME, "");
39
            putValue(Action.SMALL_ICON, iconTheme.get("form-open-pdf"));
40
            putValue(Action.SHORT_DESCRIPTION, i18n.getTranslation("_Show_in_new_window"));
41
            putValue(ACTION_COMMAND_KEY, "ShowInWindow");
42
        }
43
        
44
        @Override
45
        public void actionPerformed(ActionEvent e) {
46
            showInWindow();
47
        }
48
    }
49
    
50
    private PDFViewer pdfViewer;
51
    private JComponent pdfComponent;
52
    private PDFDocument currentDocument;
53
    private Invocable messageHandler;
54

  
55
    public EmbeddedViewerPDF() {
56
        
57
    }
58

  
59
    @Override
60
    public void setMessageHandler(Invocable messageHandler) {
61
        this.messageHandler = messageHandler;
62
    }
63

  
64
    @Override
65
    public void setReadOnly(boolean readonly) {
66
        this.pdfComponent.setEnabled(!readonly);
67
    }
68

  
69
    @Override
70
    public boolean supportShowInWindow() {
71
        return true;
72
    }
73

  
74
    @Override
75
    public Action createShowInWindowAction() {
76
        AbstractAction action = new ShowInWindowAction();
77
        return action;
78
    }
79

  
80
    @Override
81
    public void put(PDFDocument value) {
82
        if( this.pdfViewer==null ) {
83
            return;
84
        }
85
        try {
86
            this.currentDocument = value;
87
            try {
88
                this.pdfViewer.clean();
89
            } catch (Exception ex) {
90

  
91
            }
92
            if (value == null) {
93
                this.pdfViewer.put(null);
94
            } else if (value instanceof PDFDocument) {
95
                PDFDocument document = (PDFDocument) value;
96
                this.pdfViewer.put(document);
97
            }
98
        } catch (Exception ex) {
99
            if( this.messageHandler!=null ) {
100
                this.messageHandler.call("Can't load PDF");
101
            }
102
        }
103
    }
104

  
105
    @Override
106
    public void clear() {
107
        if( this.pdfViewer==null ) {
108
            return;
109
        }
110
        try {
111
            this.pdfViewer.clean();
112
        } catch (Exception ex) {
113

  
114
        }
115
    }
116

  
117
    @Override
118
    public JComponent asJComponent() {
119
        this.pdfViewer = PDFSwingLocator.getPDFSwingManager().createPDFViewer(); 
120
        this.pdfComponent = this.pdfViewer.asJComponent();        
121
//        JComponent pdfViewerPanel = ;
122
//
123
//        pdfViewerPanel.setPreferredSize(new Dimension(300, this.fieldHeight));
124
//        pdfViewerPanel.setSize(300, this.fieldHeight);
125
//
126
//        this.contents.setVisible(true);
127
        return this.pdfComponent;
128
    }
129

  
130
    @Override
131
    public void showInWindow() {
132
        WindowManager wmanager = ToolsSwingLocator.getWindowManager();
133
        PDFViewer viewer = PDFSwingLocator.getPDFSwingManager().createPDFViewer();
134
        try {
135
            viewer.put(this.currentDocument);
136
        } catch (IOException ex) {
137
            if( this.messageHandler!=null ) {
138
                this.messageHandler.call("Not able to open pdf on new window.");
139
            }
140
        }
141
        viewer.setMode(PDFViewer.MODE_COMPLETE);        
142
        JComponent panel = viewer.asJComponent();
143
        panel.setPreferredSize(new Dimension(650, 600));
144
        wmanager.showWindow(
145
                panel, 
146
                "PDF Visor", 
147
                WindowManager.MODE.WINDOW
148
        );
149
    }
150
}
org.gvsig.pdf/tags/org.gvsig.pdf-1.0.103/org.gvsig.pdf.swing/org.gvsig.pdf.swing.impl/src/main/java/org/gvsig/pdf/swing/impl/PDFSwingImplLibrary.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.swing.impl;
25

  
26
import java.net.URL;
27
import org.gvsig.pdf.swing.impl.dynform.JDynFormFieldPDFFileFactory;
28
import org.gvsig.pdf.swing.api.PDFSwingLibrary;
29
import org.gvsig.pdf.swing.api.PDFSwingLocator;
30
import org.gvsig.tools.ToolsLibrary;
31
import org.gvsig.tools.library.AbstractLibrary;
32
import org.gvsig.tools.library.LibraryException;
33
import org.gvsig.pdf.lib.api.PDFLibrary;
34
import org.gvsig.pdf.swing.impl.dynform.JDynFormFieldPDFBytearrayFactory;
35
import org.gvsig.pdf.swing.impl.dynform.JDynFormFieldPDFURLFactory;
36
import org.gvsig.tools.swing.api.ToolsSwingLocator;
37
import org.gvsig.tools.swing.icontheme.IconTheme;
38

  
39
/**
40
 *
41
 * @author jjdelcerro
42
 */
43
public class PDFSwingImplLibrary extends AbstractLibrary {
44

  
45
    @Override
46
    public void doRegistration() {
47
        super.doRegistration();
48
        registerAsImplementationOf(PDFSwingLibrary.class);
49
        this.require(ToolsLibrary.class);
50
        this.require(PDFLibrary.class);
51
    }
52

  
53
    @Override
54
    protected void doInitialize() throws LibraryException {
55
        PDFSwingLocator.registerDefaultPDFSwingManager(DefaultPDFSwingManager.class);
56

  
57
    }
58

  
59
    @Override
60
    protected void doPostInitialize() throws LibraryException {
61
        JDynFormFieldPDFFileFactory.selfRegister();
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff