栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java:使用Apache POI如何将MS Word文件转换为PDF?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java:使用Apache POI如何将MS Word文件转换为PDF?

得到了解决

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 {        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(); }}      }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569366.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号