由Docx4j转换时,PDF转换丢失格式

时间:2016-12-30 21:04:15

标签: java pdf docx docx4j

将文件从 .docx 传递到 pdf 时出现问题。

我使用docx4j 3.2.2,转换代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.docx4j.Docx4J;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

public class PDFConverter {

    public static void main(String[] args) {
        createPDF();
    }

    private static void createPDF() {
        try {
            long start = System.currentTimeMillis();
            InputStream is = new FileInputStream(new File("docxFile"));
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
            OutputStream out = new FileOutputStream(new File("pdfFile"));
            Docx4J.toPDF(wordMLPackage, out);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

转换是真实的,但pdf会丢失格式,这里是原始的docx和pdf转换

Original

Conversion

这是因为需要一些配置吗?

问候。

1 个答案:

答案 0 :(得分:-1)

package com.xxx.ecm.converter

import com.xxx.ecm.api.object.model.BaseContent
import org.docx4j.Docx4J
import org.docx4j.openpackaging.packages.WordprocessingMLPackage

class Docx2PdfConverter extends Converter 
{
    InputStream convert(BaseContent content) {
        try {
            byte[] bytes = content.inputStream.bytes
            InputStream is = new ByteArrayInputStream(bytes)
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bytes.size())
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is)
            Docx4J.toPDF(wordMLPackage, outputStream);
            new ByteArrayInputStream(outputStream.toByteArray())
        } catch (Exception e) {
          throw e
        }
    }
}