Last active
March 8, 2017 08:28
-
-
Save jsgao0/ace917b07eaf4dfef38b06b07cd77288 to your computer and use it in GitHub Desktop.
The sample of pdfbox. How to convert pdf to images and how to set protect password using Java pdfbox.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Date; | |
import org.apache.pdfbox.pdmodel.PDDocument; | |
import org.apache.pdfbox.pdmodel.PDPage; | |
import org.apache.pdfbox.pdmodel.encryption.AccessPermission; | |
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy; | |
import org.apache.pdfbox.rendering.ImageType; | |
import org.apache.pdfbox.rendering.PDFRenderer; | |
import org.apache.pdfbox.tools.imageio.ImageIOUtil; | |
import org.apache.pdfbox.pdmodel.PDDocumentInformation; | |
public class PDFOperator { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
try { | |
PDDocument doc = PDDocument.load(new File("/home/user/test.pdf")); | |
copyPDFWithKey(doc, "123", "456", "/home/user/protected-test.pdf"); | |
System.out.println(new Date().toString()); | |
pdf2Images(doc, "/home/user/images/"); | |
System.out.println(new Date().toString()); | |
doc.close(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
private static void pdf2Images(PDDocument doc, String outputDir) throws IOException { | |
PDFRenderer pdfRenderer = new PDFRenderer(doc); | |
int pageCounter = 0; | |
PDDocumentInformation df = doc.getDocumentInformation(); | |
for (PDPage page : doc.getPages()) | |
{ | |
// note that the page number parameter is zero based | |
BufferedImage bim = pdfRenderer.renderImageWithDPI(pageCounter, 300, ImageType.RGB); | |
// suffix in filename will be used as the file format | |
ImageIOUtil.writeImage(bim, outputDir + "/test-" + (pageCounter++) + ".png", 300); | |
} | |
} | |
private static void copyPDFWithKey(PDDocument doc, String ownerPassword, String readerPassword, String outputDir) throws IOException { | |
int keyLength = 128; | |
AccessPermission ap = new AccessPermission(); | |
ap.setReadOnly(); | |
StandardProtectionPolicy spp = new StandardProtectionPolicy(ownerPassword, readerPassword, ap); | |
spp.setEncryptionKeyLength(keyLength); | |
spp.setPermissions(ap); | |
doc.protect(spp); | |
doc.save(outputDir); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>PDFOperator</groupId> | |
<artifactId>PDFOperator</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.apache.pdfbox</groupId> | |
<artifactId>pdfbox</artifactId> | |
<version>2.0.4</version> | |
</dependency> | |
<dependency> | |
<groupId>org.bouncycastle</groupId> | |
<artifactId>bcprov-jdk15on</artifactId> | |
<version>1.56</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.pdfbox</groupId> | |
<artifactId>pdfbox-tools</artifactId> | |
<version>2.0.4</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<sourceDirectory>src</sourceDirectory> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.5.1</version> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment