import com.aspose.words.Document; import com.aspose.words.SaveFormat; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class Word2PdfAsposeUtil { public static boolean doc2pdf(String inPath, String outPath) { FileOutputStream os = null; try { long old = System.currentTimeMillis(); File file = new File(outPath); // 新建一个空白pdf文档 os = new FileOutputStream(file); Document doc = new Document(inPath); // Address是将要被转化的word文档 doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, // EPUB, XPS, SWF 相互转换 long now = System.currentTimeMillis(); System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时 } catch (Exception e) { e.printStackTrace(); return false; }finally { if (os != null) { try { os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } public static void main(String[] arg){ String docPath = "D:\\testtool\\asd.docx"; String pdfPath = "D:\\testtool\\asd.pdf"; Word2PdfAsposeUtil.doc2pdf(docPath,pdfPath); } }在创建空白pdf时,加上两张空白页,给出修改后代码
时间: 2024-02-14 12:28:36 浏览: 206
AsposeWord for java 23.2
你可以使用Aspose.Words库来实现将Word文档转换为PDF,并在创建空白PDF时添加两张空白页。下面是修改后的代码:
```java
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.aspose.words.SectionStart;
import com.aspose.words.SectionType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Word2PdfAsposeUtil {
public static boolean doc2pdf(String inPath, String outPath) {
FileOutputStream os = null;
try {
long old = System.currentTimeMillis();
File file = new File(outPath); // 新建一个空白pdf文档
os = new FileOutputStream(file);
Document doc = new Document(inPath); // Address是将要被转化的word文档
// 添加两张空白页
for (int i = 0; i < 2; i++) {
doc.appendDocument(new Document(), SectionType.BLANK);
}
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
public static void main(String[] arg) {
String docPath = "D:\\testtool\\asd.docx";
String pdfPath = "D:\\testtool\\asd.pdf";
Word2PdfAsposeUtil.doc2pdf(docPath, pdfPath);
}
}
```
在原有的代码基础上,我们使用`appendDocument`方法向文档中添加两个空白页。这样,生成的PDF文件将包含这两张空白页。希望对你有帮助!
阅读全文