【发布时间】:2017-05-23 14:05:17
【问题描述】:
目标:创建一个简单的 docx 文件并将其转换为 PDF。
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.*;
public class doc3 {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
//Start - Added Styles in word document
XWPFStyles styles = document.createStyles();
styles.setSpellingLanguage("English");
CTFonts def = CTFonts.Factory.newInstance();
styles.setDefaultFonts(def);
//End
XWPFParagraph tmpParagraph = document.createParagraph();
XWPFRun tmpRun = tmpParagraph.createRun();
tmpRun.setText("Hellow World");
tmpRun.setFontSize(18);
File inFile = new File("test.docx");
FileOutputStream out = new FileOutputStream(inFile);
document.write(out);
PdfOptions options = PdfOptions.create();
//The error occurs in the below line.
PdfConverter.getInstance().convert(document,out,options);
}
}
我现在收到的错误如下:
Exception in thread "main" org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:70)
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:38)
at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)
at be.quodlibet.boxable.doc3.main(doc3.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NullPointerException
at org.apache.poi.xwpf.converter.core.MasterPageManager.visitHeadersFooters(MasterPageManager.java:200)
at org.apache.poi.xwpf.converter.core.MasterPageManager.addSection(MasterPageManager.java:180)
at org.apache.poi.xwpf.converter.core.MasterPageManager.compute(MasterPageManager.java:127)
at org.apache.poi.xwpf.converter.core.MasterPageManager.initialize(MasterPageManager.java:90)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:232)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:199)
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:56)
... 8 more
我不确定我是否遗漏了 PDFOption 中的某些内容或其他内容。 任何帮助将不胜感激。
更新: 共享 POM 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>9</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.dhorions</groupId>
<artifactId>boxable</artifactId>
<version>1.4</version>
<packaging>jar</packaging>
<name>Boxable, a high-level API to creates table on top of Apache Pdfbox</name>
<description>Easily creates tables in pdf.</description>
<dependencies>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<bootclasspath>${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>external.atlassian.jgitflow</groupId>
<artifactId>jgitflow-maven-plugin</artifactId>
<version>1.0-m4.3</version>
<configuration>
<enableSshAgent>true</enableSshAgent>
<pushReleases>true</pushReleases>
<pushFeatures>true</pushFeatures>
<pushHotfixes>true</pushHotfixes>
<autoVersionSubmodules>true</autoVersionSubmodules>
<allowUntracked>true</allowUntracked>
<allowSnapshots>true</allowSnapshots>
<noDeploy>true</noDeploy>
</configuration>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
</project>
【问题讨论】:
-
我只将转换器与我用 MS Word 创建的 docx 一起使用。似乎如果您没有样式,则会导致问题,请尝试在使用 Java 创建 docx 时添加(空)样式(从未这样做过)。
-
我在word文档中添加了样式,代码也更新了。展望未来,我遇到了另一个问题......并且该问题已更新。它是一个空指针异常错误。将 docx 文件转换为 zip 文件。提取它并转到 word/document.xml。如果我们在那里检查,它不包含 w:sectPr。这个 sectPr 给出了页面的宽度/高度。为此,我们需要以在 document.xml 中存在标签 w:sectPr 的方式创建 docx 文件。正在研究它。
-
“正在研究它。”:是的,这就是你必须做的。但这表明,当两个彼此不相关的项目使用相同的名称空间时,这绝对不是一个好主意。
org.apache.poi.xwpf.converter.pdf不包含在apache poi项目中。这是一个fr.opensagres.xdocreport项目。 -
好的..谢谢@Axel。我们在 Apache POI 中是否有任何可用于将 docx 转换为 pdf 的类。我已经使用 Apache POI 项目 (
org.apache.poi.xwpf.usermodel) 创建了一个 word 文档,并通过 xdocreport 项目 (org.apache.poi.xwpf.converter),我正在尝试将 docx 转换为 pdf。 -
“我们在 Apache POI 中是否有任何类可用于将 docx 转换为 pdf。”:据我所知没有。正如您所说,您需要研究缺少从
apache poi创建的XWPFDocument,但从xdocreport使用的XWPFDocument必须需要什么。这是你作为开发人员的职责,因为这两个项目都没有互相照顾。
标签: java apache apache-poi xdocreport