以下 XML 架构片段定义了 CT_Document 元素的内容:<复杂内容><extension base="CT_DocumentBase"><序列><元素名称="body" type="CT_Body" minOccurs="0" maxOccurs="1"/></序列></扩展名></complexContent></complexType>如您所见:CT_Body 最多出现 1 次.合并两个 Word 文档不仅仅是简单地连接文档正文.正文中的所有元素都需要合并到一个 CTBody 元素中.并且单个 Word 文件系统的其他部分(主题、样式、字体表、注释、编号、媒体等)也需要合并.除了 OpenOffice 或 LibreOffice 之外,我不知道任何免费的 Java 库可以正确执行此操作.Instead of appending the content of a document to CTBody Class, I am transfering all the data from a word document to an empty document using XWPFDocument class butI am getting an error as below. the error is pointed on XWPFDocument doc when I convert it to pdffr.opensagres.poi.xwpf.converter.core.XWPFConverterException: org.apache.xmlbeans.XmlException: error: Premature end of file. 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(); XWPFDocument doc = new XWPFDocument(); doc.createStyles(); doc.getDocument().addNewBody().set(ct); doc.getDocument().addNewBody().set(ct1); FileOutputStream out = new FileOutputStream( new File("test.pdf")); PdfOptions opt = PdfOptions.create(); PdfConverter.getInstance().convert(doc, out, opt); doc.write(out); doc.close(); out.close();this is the stacktrace. fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: org.apache.xmlbeans.XmlException: error: Premature end of file.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:513)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: org.apache.xmlbeans.XmlException: error: Premature end of file.at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3448)at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1272)at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1259)at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)at org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument$Factory.parse(Unknown Source)at org.apache.poi.xwpf.usermodel.XWPFDocument.getStyle(XWPFDocument.java:557)at fr.opensagres.poi.xwpf.converter.core.styles.XWPFStylesDocument.<init>(XWPFStylesDocument.java:196)at fr.opensagres.poi.xwpf.converter.core.styles.XWPFStylesDocument.<init>(XWPFStylesDocument.java:190)at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.createStylesDocument(XWPFDocumentVisitor.java:182)at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.<init>(XWPFDocumentVisitor.java:175)at fr.opensagres.poi.xwpf.converter.pdf.internal.PdfMapper.<init>(PdfMapper.java:155)at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:56)... 40 more Caused by: org.xml.sax.SAXParseException; systemId: file://; lineNumber: 1; columnNumber: 1; Premature end of file.at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3422)... 51 more 解决方案 Even this code does nothing else than simply appending multiple document bodies. It first creates a new XWPFDocument which contains one CTBody already. Then it uses XWPFDocument.getDocument() which gets the org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1 of that new XWPFDocument and then it adds two new CTBody elements to that. After that the CTDocument1 will have three CTBody elements.But according to Office Open XML a CT_Document can only have one element of type CT_Body.The following XML Schema fragment defines the contents of the CT_Document element:<complexType name="CT_Document"> <complexContent> <extension base="CT_DocumentBase"> <sequence> <element name="body" type="CT_Body" minOccurs="0" maxOccurs="1"/> </sequence> </extension> </complexContent></complexType>As you see: CT_Body occurs 1 times max.Merging two Word documents is more than simply concatenating the document bodies. All the elements in the bodies needs to be merged in one CTBody element. And there are other parts of the single Word file systems (themes, styles, font table, comments, numbering, media, ...) which also needs merged. I do not know any free Java library except OpenOffice or LibreOffice which can do this properly. 这篇关于文件提前结束 - XWPFDocument 到 PDFConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 21:27