Skip to content

Commit

Permalink
Update PdfConvertUtils.java
Browse files Browse the repository at this point in the history
  • Loading branch information
billchen198318 committed Feb 4, 2016
1 parent 9dbd8c1 commit 83a7496
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions core-base/src/com/netsteadfast/greenstep/util/PdfConvertUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.util.ImageIOUtil;
Expand Down Expand Up @@ -57,10 +59,12 @@ public static List<File> toImageFiles(File pdfFile, int resolution) throws Excep
File tmpDir = new File(Constants.getWorkTmpDir() + "/" + PdfConvertUtils.class.getSimpleName()
+ "/" + System.currentTimeMillis() + "/");
FileUtils.forceMkdir( tmpDir );
List<File> files = new LinkedList<File>();
List<File> files = new LinkedList<File>();
int len = String.valueOf(pages.size()+1).length();
for (int i=0; i<pages.size(); i++) {
String name = StringUtils.leftPad(String.valueOf(i+1), len, "0");
BufferedImage bufImage = pages.get(i).convertToImage(BufferedImage.TYPE_INT_RGB, resolution);
File imageFile = new File( tmpDir.getPath() + "/" + String.valueOf(i) + ".png" );
File imageFile = new File( tmpDir.getPath() + "/" + name + ".png" );
FileOutputStream fos = new FileOutputStream(imageFile);
ImageIOUtil.writeImage(bufImage, "png", fos, resolution);
fos.flush();
Expand All @@ -71,15 +75,19 @@ public static List<File> toImageFiles(File pdfFile, int resolution) throws Excep
tmpDir = null;
return files;
}

/*

public static void main(String args[]) throws Exception {
// for test
List<File> imageFiles = toImageFiles(new File("/home/git/bamboobsc/core-doc/install.pdf"), 300);
if (args == null || args.length != 2) {
System.out.println("PdfConvertUtils [FILE] [RESOLUTION]");
System.out.println("Example: PdfConvertUtils /test.pdf 120");
System.exit(1);
}
System.out.println("source document file: " + args[0]);
int res = NumberUtils.toInt(args[1], 300);
List<File> imageFiles = toImageFiles(new File(args[0]), res);
for (File file : imageFiles) {
System.out.println( file.getPath() );
}
}
*/
}

}

0 comments on commit 83a7496

Please sign in to comment.