合并两个文档并转换为pdf-APACHE POI

问题描述 投票:0回答:1

我设法使用Apache POI XWPFDocument和CTBody类合并了两个文档。但是,在打开合并文档时,它提供了不受支持的格式,因此我尝试将其另存为pdf格式,但是仍然存在相同的问题。

我正在使用https://mvnrepository.com/artifact/fr.opensagres.xdocreport/fr.opensagres.poi.xwpf.converter.pdf/2.0.2

我已经尝试过-https://www.programcreek.com/java-api-examples/index.php?api=org.apache.poi.xwpf.converter.pdf.PdfConverter

该应用程序的主要目标是合并特定文件夹中的所有文档,并将其转换为pdf文件。以下是我到目前为止所做的

        FileInputStream fis   = new FileInputStream("1.docx");
        FileInputStream fis1  = new FileInputStream("2.docx");

        XWPFDocument xdoc = new XWPFDocument(OPCPackage.open(fis));
        XWPFDocument xdoc1 = new XWPFDocument(OPCPackage.open(fis1));
        CTBody ct = xdoc.getDocument().getBody();
        CTBody ct1 = xdoc1.getDocument().getBody();

        appendBody(ct,ct1); // function merge two document to xdoc

        //saving as word document
        // the document is saved but getting this error when opening - image 1 below 
        FileOutputStream out = new FileOutputStream( new File("FL Divorce 223 Motion for Temp Family Law Order 2019_07.docx"));
        xdoc.write(out);
        out.close();

        // converting word to pdf
        FileInputStream str = new FileInputStream(new File("FL Divorce 223 Motion for Temp Family Law Order 2019_07.docx"));
        XWPFDocument document = new XWPFDocument(str);
        out = new FileOutputStream(new File("FL Divorce 223 Motion for Temp Family Law Order 2019_07.pdf"));
        PdfOptions options = PdfOptions.create();
        PdfConverter.getInstance().convert(document, out, options);
        out.close();

        File f = new File("FL Divorce 223 Motion for Temp Family Law Order 2019_07.docx");
        f.delete();

image 1

private static void appendBody(CTBody src, CTBody append) throws Exception {
    XmlOptions optionsOuter = new XmlOptions();
    optionsOuter.setSaveOuter();
    String appendString = append.xmlText(optionsOuter);
    String srcString = src.xmlText();
    String prefix = srcString.substring(0,srcString.indexOf(">")+1);
    String mainPart = srcString.substring(srcString.indexOf(">")+1,srcString.lastIndexOf("<"));
    String sufix = srcString.substring( srcString.lastIndexOf("<") );
    String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
    CTBody makeBody = CTBody.Factory.parse(prefix+mainPart+addPart+sufix);
    src.set(makeBody);
}

StackTrace:

   fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:71)
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:39)
at fr.opensagres.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:46)
at trafficMan.MainApp.mergeDocument(MainApp.java:515)
at trafficMan.MainApp$2.actionPerformed(MainApp.java:609)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at   fr.opensagres.poi.xwpf.converter.core.utils.XWPFTableUtil.getGridColList(XWPFTableUtil.java:184)
at fr.opensagres.poi.xwpf.converter.core.utils.XWPFTableUtil.computeColWidths(XWPFTableUtil.java:117)
at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTable(XWPFDocumentVisitor.java:970)
at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:267)
at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:215)
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:57)
... 40 more
fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException

我也尝试过这个。

        FileInputStream fis   = new FileInputStream("1.docx");
        FileInputStream fis1  = new FileInputStream("2.docx");

        XWPFDocument xdoc = new XWPFDocument(OPCPackage.open(fis));
        XWPFDocument xdoc1 = new XWPFDocument(OPCPackage.open(fis1));
        CTBody ct = xdoc.getDocument().getBody();
        CTBody ct1 = xdoc1.getDocument().getBody();

        appendBody(ct,ct1); // function 

        //saving as word document
        // the document is saved but getting this error when opening - image 1 below 
        FileOutputStream out = new FileOutputStream( new File("FL Divorce 223 Motion for Temp Family Law Order 2019_07.pdf"));

        PdfOptions options = PdfOptions.create();
        PdfConverter.getInstance().convert(xdoc, out, options);
        out.close();

StackTrace:

fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:71)
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:39)
at fr.opensagres.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:46)
at trafficMan.MainApp.mergeDocument(MainApp.java:510)
at trafficMan.MainApp$2.actionPerformed(MainApp.java:601)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
at org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(XmlObjectBase.java:1258)
at org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTPImpl.getPPr(Unknown Source)
at org.apache.poi.xwpf.usermodel.XWPFParagraph.getStyleID(XWPFParagraph.java:222)
at   fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:259)
at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:215)
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:57)
... 40 more

将此文件或直接将其另存为pdf都会使它在打开时成为无效文件。有人知道解决方案吗?

java pdf apache-poi converters
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.