使用Apache POI和iText的JAVA中从Word(DOC)创建PDF [英] Creating PDF from Word (DOC) using Apache POI and iText in JAVA

查看:1338
本文介绍了使用Apache POI和iText的JAVA中从Word(DOC)创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成从的* .doc文档的PDF文档。
截至目前并感谢StackOverflow的我有成功生成,但有一些问题。

我下面的示例code产生不formatations和图像的PDF格式,只是文本。
该文件包括不包括在PDF空格和图像

下面是code:

  =中新的FileInputStream(sourceFile.getAbsolutePath());
        OUT =新的FileOutputStream(OUTPUTFILE);        WordExtractor WD =新WordExtractor(中);        字符串文本= wd.getText();        文件PDF =新的文件(PageSize.A4);        PdfWriter.getInstance(PDF,出);        pdf.open();
        pdf.add(新段落(文本));


解决方案

docx4j包含的创建使用iText的一个的docx一个PDF code 。它也可以使用POI到一个文档转换为DOCX。

曾几何时,我们同样支持两种方法的时间(以及通过XHTML PDF),但我们决定把重点放在XSL-FO。

如果它的一个选择,你会好得多使用docx4j通过XSL-FO和FOP到DOCX转换为PDF。

使用它像这样:

  wordMLPackage = Wordpro​​cessingMLPackage.load(新的java.io.File(inputfilepath));        //设置字体映射
        映射fontMapper =新IdentityPlusMapper();
        wordMLPackage.setFontMapper(fontMapper);        //映射缺少的字体阿尔及利亚的例子来安装的字体Comic Sans字体MS
        PhysicalFont字体
                = PhysicalFonts.getPhysicalFonts()获得(Comic Sans字体MS);
        。fontMapper.getFontMappings()把(阿尔及利亚,字体);        org.docx4j.convert.out.pdf.PdfConversionç
            =新org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
        // =新org.docx4j.convert.out.pdf.viaIText.Conversion(wordMLPackage);        OutputStream的OS =新java.io.FileOutputStream中(inputfilepath +.PDF);
        c.output(OS);

I am trying to generate a PDF document from a *.doc document. Till now and thanks to stackoverflow I have success generating it but with some problems.

My sample code below generates the pdf without formatations and images, just the text. The document includes blank spaces and images which are not included in the PDF.

Here is the code:

        in = new FileInputStream(sourceFile.getAbsolutePath());
        out = new FileOutputStream(outputFile);

        WordExtractor wd = new WordExtractor(in);

        String text = wd.getText();

        Document pdf= new Document(PageSize.A4);

        PdfWriter.getInstance(pdf, out);

        pdf.open();
        pdf.add(new Paragraph(text));

解决方案

docx4j includes code for creating a PDF from a docx using iText. It can also use POI to convert a doc to a docx.

There was a time when we supported both methods equally (as well as PDF via XHTML), but we decided to focus on XSL-FO.

If its an option, you'd be much better off using docx4j to convert a docx to PDF via XSL-FO and FOP.

Use it like so:

        wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));

        // Set up font mapper
        Mapper fontMapper = new IdentityPlusMapper();
        wordMLPackage.setFontMapper(fontMapper);

        // Example of mapping missing font Algerian to installed font Comic Sans MS
        PhysicalFont font 
                = PhysicalFonts.getPhysicalFonts().get("Comic Sans MS");
        fontMapper.getFontMappings().put("Algerian", font);             

        org.docx4j.convert.out.pdf.PdfConversion c 
            = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
        //  = new org.docx4j.convert.out.pdf.viaIText.Conversion(wordMLPackage);

        OutputStream os = new java.io.FileOutputStream(inputfilepath + ".pdf");         
        c.output(os);

这篇关于使用Apache POI和iText的JAVA中从Word(DOC)创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆