Word转换pdf,word里面的水印转成PDF后格式错误

image.png (110.7 KB)

test.docx (13.9 KB)
test.pdf (14.4 KB)

代码:

    public static void main(String[] args) throws Exception {
        String filePath = "C:\\Users\\mcsca\\Desktop\\test.docx";
        InputStream is = new FileInputStream(filePath);
        String s = docToPdf(is);
        byte[] decode = Base64.getDecoder().decode(s);
        byteToFile(decode, "C:\\Users\\mcsca\\Desktop\\test.pdf");
    }

    /**
     * Word 转 PDF
     *
     * @param inputStream word文件
     */
    public static String docToPdf(InputStream inputStream) throws Exception {
        logger.info("Word转换PDF开始");
        // 去除水印
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return null;
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);
        DocumentBuilder builder = new DocumentBuilder(doc);

        //文档主体内容设置段后和行距
        builder.moveToDocumentStart();
        builder.getParagraphFormat().setLineSpacing(12);// 单倍行距 = 12 , 1.5 倍 = 18
        builder.getParagraphFormat().setSpaceAfter(0);//段后
        //页眉设置段后和行距
        builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
        builder.getParagraphFormat().setLineSpacing(12);
        builder.getParagraphFormat().setSpaceAfter(0);
        //页脚设置段后和行距
        builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
        builder.getParagraphFormat().setLineSpacing(12);
        builder.getParagraphFormat().setSpaceAfter(0);
        //表格设置段后和行距
        //builder.moveToCell(0,0,0,0);
        //builder.getParagraphFormat().setLineSpacing(12);
        //builder.getParagraphFormat().setSpaceAfter(0);
        try {
            //开始时间
            long old = System.currentTimeMillis();
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF
            doc.save(bos, SaveFormat.PDF);
            byte[] bytes = bos.toByteArray();
            String returnBase64 = Base64.getEncoder().encodeToString(bytes);

            //结束时间
            long now = System.currentTimeMillis();
            logger.info("Word转换PDF成功,共耗时:" + (now - old) + "ms");
            return returnBase64;
        } catch (Exception e) {
            logger.info("Word转换PDF失败!");
            e.printStackTrace();
            throw e;
        } finally {
            // 关闭流
            bos.close();
        }
    }

    /**
     * 去除水印
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            InputStream is = ResourceUtil.getResourceObj("files/word/Aspose.Words.Java.lic").getStream();
            License asposeLic = new License();
            asposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

依赖jar包:

        <!-- word转pdf -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>15.12.0</version>
            <classifier>jdk16</classifier>
        </dependency>

@ranlinqing 您使用的是非常旧版本的 Aspose.Words。 我无法使用最新 22.7 版本的 Aspose.Words for Java 重现该问题。
https://repository.aspose.com/words/
这是我这边产生的输出:out.pdf (10.4 KB)

1 Like