java实现pdf转ofd,“Evaluation Warning : The do cument was created with Spire.PDF for j ava”这个东西就很烦

本文介绍了如何在SpringBoot项目中使用Maven依赖库e-iceblue的spire.pdf.free进行PDF到OFD的转换,包括开发环境配置、代码示例和遇到的问题及解决方案,如免费版的限制和付费版的水印问题。
摘要由CSDN通过智能技术生成

场景说明

场景

由于业务要求需要将pdf文件转为ofd文件类型,而使用一些在线的,或其他的工具会有限制,收费等缺陷

开发版本

1.java版本:jdk1.8
截图如下
在这里插入图片描述

2.springboot版本:2.5.3
截图如下
在这里插入图片描述

使用到的Maven依赖以及版本

<!-- lombok插件 -->
<dependency>
	<artifactId>lombok</artifactId>
	<groupId>org.projectlombok</groupId>
	<scope>provided</scope>
	<version>1.18.10</version>
</dependency>

<!-- pdf转ofd需要使用 -->
<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.pdf.free</artifactId>
    <version>5.1.0</version>
</dependency>

<repositories>
	<repository>
	    <id>com.e-iceblue</id>
	    <name>e-iceblue</name>
	    <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
	</repository>
</repositories>

注意:要指定e-iceblue仓库,不然会报以仓库中找不到依赖错误

完整pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>pdf</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>pdf</name>
    <description>pdf</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- lombok插件 -->
        <dependency>
            <artifactId>lombok</artifactId>
            <groupId>org.projectlombok</groupId>
            <scope>provided</scope>
            <version>1.18.10</version>
        </dependency>

        <!-- pdf转ofd -->
        <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.pdf.free</artifactId>
            <version>5.1.0</version>
        </dependency>

    </dependencies>

    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

代码示例

注意:指定文件路径,pfd源文件路径:E:\Desktop\temps\test.pdf,生成ofd文件路径以及文件名称:E:\Desktop\temps\test.ofd

package com.example.pdf.convert;

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @author
 * @date 2024/3/29 15:27
 * @describe
 */
@Slf4j
public class PDFToOFD {

    public static void main(String[] args) {
        transPdfToOfd("E:\\Desktop\\temps\\test.pdf", "E:\\Desktop\\temps\\test.ofd");
    }

    /**
     * @Description pdf文档 转 ofd文档
     * @date 2024/3/29 15:27
     * @Param pdfPath pdf文件全路径
     * @Param ofdPath ofd 输出全路径
     **/
    public static void transPdfToOfd(String pdfPath, String ofdPath) {
        if (StringUtils.isEmpty(ofdPath) || StringUtils.isEmpty(pdfPath)) {
            log.error("pdf,ofd文档地址不能为空");
            throw new RuntimeException("pdf或ofd文档地址不能为空");
        }

        byte[] bytes = getBytesFromFile(new File(pdfPath));
        transPdfToOfd(bytes, ofdPath);
    }

    /**
     * @Description pdf byte数组转ofd文档输出
     * @date 2024/3/29 15:27
     * @Param pdfBytes 字节数组
     * @Param ofdPath ofd文档输出全路径
     **/
    public static void transPdfToOfd(byte[] pdfBytes, String ofdPath) {
        if (pdfBytes == null || pdfBytes.length <= 0) {
            log.error("当前待转化的pdf文件错误,或者内容为空");
            throw new RuntimeException("pdf转ofd转化失败,pdf文件错误,pdf文件内容不能为空");
        }

        if (StringUtils.isEmpty(ofdPath)) {
            log.error("ofd文档输出地址不能为空");
            throw new RuntimeException("ofd文档输出地址不能为空");
        }

        long startTime = LocalDateTime.now().atOffset(ZoneOffset.of("+8")).toInstant().toEpochMilli();
        //构建PDF内容
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromBytes(pdfBytes);

        pdf.saveToFile(ofdPath, FileFormat.OFD);

        long endTime = LocalDateTime.now().atOffset(ZoneOffset.of("+8")).toInstant().toEpochMilli();
        log.info("pdf转化为ofd耗时:{} 毫秒", endTime - startTime);

    }

    /**
     * @return byte[]
     * @Description 把一个文件转化为byte字节数组
     * @date 2024/3/29 15:27
     * @Param file 文件
     **/
    public static byte[] getBytesFromFile(File file) {
        byte[] data;
        try (InputStream inputStream = Files.newInputStream(file.toPath());
             BufferedInputStream bis = new BufferedInputStream(inputStream);
             ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            int len;
            byte[] buffer = new byte[1024];
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }

            data = bos.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return data;
    }
}

代码运行结果

在这里插入图片描述

效果展示

转换前:

在这里插入图片描述

转换后:

在这里插入图片描述

ofd文件展示

在这里插入图片描述

遇到的问题以及解决方案

1.使用的是pdf非免费版导致的转换好了的ofd文件有水印,并且部分标题被模糊处理了
2.原来使用的Maven依赖

<dependency>
	<groupId>e-iceblue</groupId>
	<artifactId>spire.pdf</artifactId>
	<version>10.2.0</version>
</dependency>

3.截图示例
在这里插入图片描述
4.使用免费版的就好了

<dependency>
	<groupId>e-iceblue</groupId>
	<artifactId>spire.pdf.free</artifactId>
	<version>5.1.0</version>
</dependency>

注意:如果pdf文件页数超过10,则后面的文件将会转换失败或者缺失。转换量小可以使用免费的

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值