xml 使用apche poi,xdocreport和itext扩展将Docx转换为PDF

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml 使用apche poi,xdocreport和itext扩展将Docx转换为PDF相关的知识,希望对你有一定的参考价值。

/**
     * convert to pdf using xdocreport, apache poi and itext extension
     * note: not compatible with RTL languages
     */
    public static void ConvertToPDFUsingXDocAndPoiAndItext(String docPath, String pdfPath) {
        try {
            InputStream doc = new FileInputStream(new File(docPath));
            XWPFDocument document = new XWPFDocument(doc);
            PdfOptions options = PdfOptions.create();
            OutputStream out = new FileOutputStream(new File(pdfPath));
            PdfConverter.getInstance().convert(document, out, options);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }
    
  // convert docx to pdf using apache poi
  // note: RTL Languages are not supported
  public byte[] getPDFByXWPF() {
        try {
            XWPFDocument document = new XWPFDocument(new ByteArrayInputStream(file.toByteArray()));
            PdfOptions options = PdfOptions.create();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            PdfConverter.getInstance().convert(document, outputStream, options);
            return outputStream.toByteArray();
        }catch (IOException e){
            return null;
        }
    }
        <!-- Note: not sure if these are all of the dependencies --> 
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.itext.extension</artifactId>
            <version>2.0.2</version>
        </dependency>

        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
            <version>1.0.6</version>
        </dependency>
        
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.6</version>
        </dependency>

以上是关于xml 使用apche poi,xdocreport和itext扩展将Docx转换为PDF的主要内容,如果未能解决你的问题,请参考以下文章

POI:重复条目:org / apache / xmlbeans / xml / stream / BindigConfig.class

Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)

使用POI解析Excel时,出现org.xml.sax.SAXParseException: duplicate attribute 'o:relid'的解决办法

Apache POI XML外部实体(XML External Entity,XXE)攻击

POI 读大文件日志

利用POi3.8导出excel产生大量xml临时文件怎么办?