public class DocumentConvertTest {
public static void main(String argv[]) {
try {
// docConvert2Html("E:/文档/gan.doc", "E:/文档/", "1.html", "UTF-8");
pdfConvert2Img("E:/文档/", "zhy.pdf", 50);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void pdfConvert2Img(String filePath, String fileName, int dpi) throws IOException {
PDDocument document = PDDocument.load(new FileInputStream(new File(filePath + fileName)));
PDFRenderer pdfRenderer = new PDFRenderer(document);
String savePath = filePath + fileName.substring(0, fileName.indexOf(".")) + "_2_images/";
File savePathDir = new File(savePath);
if (!savePathDir.exists()) {
savePathDir.mkdirs();
}
for (int page = 0; page < document.getNumberOfPages(); ++page) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, dpi, ImageType.RGB);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
Iterator iter = ImageIO.getImageWritersBySuffix("jpg");
ImageWriter writer = (ImageWriter) iter.next();
// File outFile = new File(savePath + page + ".jpg");
ImageOutputStream outImage = ImageIO.createImageOutputStream(bs);
writer.setOutput(outImage);
writer.write(new IIOImage(bim, null, null));
writer.dispose();
byte[] bArray = bs.toByteArray();
InputStream is = new ByteArrayInputStream(bArray);
}
document.close();
}
public static void docConvert2Html(String fileName, final String outPutFilePath, final String outPutFileName, String encode)
throws TransformerException, IOException,
ParserConfigurationException {
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument());
final String imgPath = outPutFilePath + outPutFileName.substring(0, outPutFileName.indexOf(".")) + "_images/";
wordToHtmlConverter.setPicturesManager(new PicturesManager() {
public String savePicture(byte[] content,
PictureType pictureType, String suggestedName,
float widthInches, float heightInches) {
return imgPath + suggestedName;
}
});
wordToHtmlConverter.processDocument(wordDocument);
//save pictures
List pics = wordDocument.getPicturesTable().getAllPictures();
if (pics != null) {
for (int i = 0; i < pics.size(); i++) {
Picture pic = (Picture) pics.get(i);
System.out.println(imgPath + pic.suggestFullFileName());
File imgPathDir = new File(imgPath);
if (!imgPathDir.exists()) {
imgPathDir.mkdirs();
}
pic.writeImageContent(new FileOutputStream(imgPath + pic.suggestFullFileName()));
}
}
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, encode);
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
out.close();
writeFile(new String(out.toByteArray()), outPutFilePath + outPutFileName, encode);
}
public static void writeFile(String content, String name, String encode) {
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File(name);
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos, encode));
bw.write(content);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fos != null)
fos.close();
} catch (IOException ie) {
}
}
}
}
maven依赖:PDFbox,POI的HWPFDocument
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.0-RC3</version>
</dependency>