简体   繁体   English

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

[英]PDF conversion lose format when is converted by Docx4j

I've a problem when pass file from .docx to pdf . .docx文件传递到pdf时出现问题。

I use docx4j 3.2.2, the code for conversion: 我使用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();
        }
    }
}

the conversion is realiced, but pdf lose the format, here the original docx and pdf conversion 实现了转换,但pdf丢失了格式,此处为原始docx和pdf转换

Original 原版的

Conversion 转换次数

This is because is needed some configuration? 这是因为需要一些配置吗?

regards. 问候。

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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM