Word转Pdf
public static void main(String[] args) throws Exception {
Document doc = new Document("C:\\Users\\w4523\\Desktop\\安全问题.docx");
doc.save("C:\\Users\\w4523\\Desktop\\1.pdf");
}
html转pdf
import com.aspose.words.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.javassist.CannotCompileException;
import org.apache.ibatis.javassist.NotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URL;
/**
* html转pdf工具类
* Created by liul on 2016-07-20.
*/
public class Html2PdfUtils {
private static final Logger log = LoggerFactory.getLogger(Html2PdfUtils.class);
private static final int wdFormatPDF = 17;// PDF 格式
/**
* 调用aspose.words方式将 html 转为 pdf 文件
*
* @param content html 内容
* @param outPath pdf存储路径
* @return true 转换成功,false 转换失败
*/
public static boolean html2pdfByAspose(String content, String outPath) {
FileOutputStream os = null;
try {
long start = System.currentTimeMillis();
File file = new File(outPath); // 新建一个空白pdf文档
os = new FileOutputStream(file);
LoadOptions loadOptions = new LoadOptions();
loadOptions.setLoadFormat(SaveFormat.HTML);
InputStream inputStream = new ByteArrayInputStream(content.getBytes());
Document doc = new Document(inputStream, loadOptions); // Address是将要被转化的word文档
DocumentBuilder builder = new DocumentBuilder(doc);
PageSetup pageSetup = builder.getPageSetup();
pageSetup.setLeftMargin(15.0);
pageSetup.setRightMargin(15.0);
pageSetup.setTopMargin(35.0);
pageSetup.setBottomMargin(15.0);
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
log.info("html转pdf共耗时:{} ms", System.currentTimeMillis() - start); // 转化用时
os.close();
return true;
} catch (Exception e) {
log.error("html转pdf异常: {}", e);
} finally {
if (os != null) {
IOUtils.closeQuietly(os);
}
}
return false;
}
/**
* 调用aspose.words方式将 html 转成 pdf 文件流
*
* @param content 文件内容
* @return pdf文件流
*/
public static ByteArrayOutputStream html2pdfStreamByAspose(String content) {
ByteArrayOutputStream os = null;
try {
long start = System.currentTimeMillis();
os = new ByteArrayOutputStream();
LoadOptions loadOptions = new LoadOptions();
loadOptions.setLoadFormat(SaveFormat.HTML);
InputStream inputStream = new ByteArrayInputStream(content.getBytes());
Document doc = new Document(inputStream, loadOptions); // Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
log.info("html转pdf共耗时:{} ms", System.currentTimeMillis() - start); // 转化用时
} catch (Exception e) {
log.error("html转pdf异常: {}", e);
} finally {
if (os != null) {
IOUtils.closeQuietly(os);
}
}
return os;
}
public static void main(String[] args) throws IOException, NotFoundException, CannotCompileException {
InputStream inputStream = Html2PdfUtils.class.getClassLoader().getResourceAsStream("templates/pt/template/ele_seal_collection_form.html");
int available = inputStream.available();
byte[] bytes = new byte[available];
inputStream.read(bytes);
String content = new String(bytes);
// String inPath = "E:\\testfile\\test_table2.html";
// String content = FileUtils.readFileToString(new File(inPath), "utf-8");
content = HtmlUtils.formatPdfHtml(content);
System.out.println(content);
String outPath = "E:\\testfile\\test_table2.pdf";
html2pdfByAspose(content, outPath);
//
// ByteArrayOutputStream outputStream = html2pdfStreamByAspose(content);
//
// FileOutputStream fileOutputStream = new FileOutputStream("E:\\testfile\\test_table22222.pdf");
// fileOutputStream.write(outputStream.toByteArray());
// fileOutputStream.flush();
// fileOutputStream.close();
}
}
pdf转word
String input="C:\\Users\\HHH\\Desktop\\会议记录.pdf";
String output="C:\\Users\\HHH\\Desktop\\会议记录123.docx";
@Test
public void test1() throws Exception {
// Load source PDF file
Document doc = new Document(input);
// Save resultant DOCX file
doc.save(output, SaveFormat.DocX);
System.out.println("--------------------------------");
}
格式有问题,错位,并且文字是文本框,使用DocSaveOptions
解决
DocSaveOptions
设置,官方文档:https://reference.aspose.com/pdf/zh/java/com.aspose.pdf/docsaveoptions/
@Test
public void test2() throws Exception {
// Load source PDF file
Document doc = new Document(input);
// Instantiate DocSaveOptions instance
DocSaveOptions saveOptions = new DocSaveOptions();
// Set output format
saveOptions.setFormat(DocSaveOptions.DocFormat.DocX);
// Set the recognition mode as Flow
saveOptions.setMode(DocSaveOptions.RecognitionMode.Flow);
// Save resultant DOCX file
doc.save(output, saveOptions);
}