Java的:使用Apache POI怎样的MS Word文件转换为PDF? [英] Java:using apache POI how to convert ms word file to pdf?
本文介绍了Java的:使用Apache POI怎样的MS Word文件转换为PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过使用的Apache POI
如何转换 MS字
文件复制到 PDF
?
我是用下面的code,但它不工作给予错误我想我导入了错误的类?
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileOutputStream中;
进口java.io.OutputStream中;进口org.apache.poi.hslf.record.Document;
进口org.apache.poi.hwpf.HWPFDocument;
进口org.apache.poi.hwpf.extractor.WordExtractor;
进口org.apache.poi.hwpf.usermodel.Paragraph;
进口org.apache.poi.hwpf.usermodel.Range;
进口org.apache.poi.poifs.filesystem.POIFSFileSystem;
公共类TestCon { / **
* @参数ARGS
* /
公共静态无效的主要(字串[] args){
// TODO自动生成方法存根 POIFSFileSystem FS =无效;
文档的文档=新的文件(); 尝试{
的System.out.println(开始测试);
FS =新POIFSFileSystem(新的FileInputStream(/文件/ test2.doc)); HWPFDocument DOC =新HWPFDocument(FS);
WordExtractor我们=新WordExtractor(DOC); OutputStream的文件=新的FileOutputStream(新文件(/文件/的test.pdf)); PdfWriter作家= PdfWriter.getInstance(文档,文件); 范围范围= doc.getRange();
document.open();
writer.setPageEmpty(真);
document.newPage();
writer.setPageEmpty(真); 的String [] =段we.getParagraphText();
的for(int i = 0; I< paragraphs.length;我++){ org.apache.poi.hwpf.usermodel.Paragraph PR = range.getParagraph(ⅰ);
// CharacterRun运行= pr.getCharacterRun(I)
// run.setBold(真);
// run.setCapitalized(真);
// run.setItalic(真);
段落[I] =段落[I] .replaceAll(\\\\ cM的\\ r \\ n?,);
的System.out.println(持续时间+段落[I]。长度());
的System.out.println(段落+ I +:+段落[I]的ToString()); //段落添加到文档中
document.add(新段落(段落[I]));
} 的System.out.println(文档测试完成);
}赶上(例外五){
的System.out.println(测试期间异常);
e.printStackTrace();
} {最后
//关闭文档
document.close();
}
}
}
解决方案
被它解决了
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileOutputStream中;
进口java.io.OutputStream中;进口com.lowagie.text.Document;
进口com.lowagie.text.DocumentException;
进口com.lowagie.text.Paragraph;
进口com.lowagie.text.pdf.PdfWriter;
进口org.apache.poi.hwpf.HWPFDocument;
进口org.apache.poi.hwpf.extractor.WordExtractor;进口org.apache.poi.hwpf.usermodel.Range;
进口org.apache.poi.poifs.filesystem.POIFSFileSystem;
公共类TestCon { / **
* @参数ARGS
* /
公共静态无效的主要(字串[] args){
// TODO自动生成方法存根 POIFSFileSystem FS =无效;
文档的文档=新的文件(); 尝试{
的System.out.println(开始测试);
FS =新POIFSFileSystem(新的FileInputStream(D:/Resume.doc)); HWPFDocument DOC =新HWPFDocument(FS);
WordExtractor我们=新WordExtractor(DOC); OutputStream的文件=新的FileOutputStream(新文件(D:/test.pdf)); PdfWriter作家= PdfWriter.getInstance(文档,文件); 范围范围= doc.getRange();
document.open();
writer.setPageEmpty(真);
document.newPage();
writer.setPageEmpty(真); 的String [] =段we.getParagraphText();
的for(int i = 0; I< paragraphs.length;我++){ org.apache.poi.hwpf.usermodel.Paragraph PR = range.getParagraph(ⅰ);
// CharacterRun运行= pr.getCharacterRun(I)
// run.setBold(真);
// run.setCapitalized(真);
// run.setItalic(真);
段落[I] =段落[I] .replaceAll(\\\\ cM的\\ r \\ n?,);
的System.out.println(持续时间+段落[I]。长度());
的System.out.println(段落+ I +:+段落[I]的ToString()); //段落添加到文档中
document.add(新段落(段落[I]));
} 的System.out.println(文档测试完成);
}赶上(例外五){
的System.out.println(测试期间异常);
e.printStackTrace();
} {最后
//关闭文档
document.close();
}
}
}
By using apache POI
how to convert ms word
file to pdf
?
I an using the following code but its not working giving errors I guess I am importing the wrong classes?
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.hslf.record.Document;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class TestCon {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
POIFSFileSystem fs = null;
Document document = new Document();
try {
System.out.println("Starting the test");
fs = new POIFSFileSystem(new FileInputStream("/document/test2.doc"));
HWPFDocument doc = new HWPFDocument(fs);
WordExtractor we = new WordExtractor(doc);
OutputStream file = new FileOutputStream(new File("/document/test.pdf"));
PdfWriter writer = PdfWriter.getInstance(document, file);
Range range = doc.getRange();
document.open();
writer.setPageEmpty(true);
document.newPage();
writer.setPageEmpty(true);
String[] paragraphs = we.getParagraphText();
for (int i = 0; i < paragraphs.length; i++) {
org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
// CharacterRun run = pr.getCharacterRun(i);
// run.setBold(true);
// run.setCapitalized(true);
// run.setItalic(true);
paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
System.out.println("Length:" + paragraphs[i].length());
System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());
// add the paragraph to the document
document.add(new Paragraph(paragraphs[i]));
}
System.out.println("Document testing completed");
} catch (Exception e) {
System.out.println("Exception during test");
e.printStackTrace();
} finally {
// close the document
document.close();
}
}
}
解决方案
Got It solved
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class TestCon {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
POIFSFileSystem fs = null;
Document document = new Document();
try {
System.out.println("Starting the test");
fs = new POIFSFileSystem(new FileInputStream("D:/Resume.doc"));
HWPFDocument doc = new HWPFDocument(fs);
WordExtractor we = new WordExtractor(doc);
OutputStream file = new FileOutputStream(new File("D:/test.pdf"));
PdfWriter writer = PdfWriter.getInstance(document, file);
Range range = doc.getRange();
document.open();
writer.setPageEmpty(true);
document.newPage();
writer.setPageEmpty(true);
String[] paragraphs = we.getParagraphText();
for (int i = 0; i < paragraphs.length; i++) {
org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
// CharacterRun run = pr.getCharacterRun(i);
// run.setBold(true);
// run.setCapitalized(true);
// run.setItalic(true);
paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
System.out.println("Length:" + paragraphs[i].length());
System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());
// add the paragraph to the document
document.add(new Paragraph(paragraphs[i]));
}
System.out.println("Document testing completed");
} catch (Exception e) {
System.out.println("Exception during test");
e.printStackTrace();
} finally {
// close the document
document.close();
}
}
}
这篇关于Java的:使用Apache POI怎样的MS Word文件转换为PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文