Java 将PDF转为PDF/A

通过将PDF格式转换为PDF/A格式,可保护文档布局、格式、字体、大小等不受更改,从而实现文档安全保护的目的,同时又能保证文档可读、可访问。本篇文章,将通过Java后端程序代码展示如何将PDF转为符合PDF/A1A, 2A, 3A, 1B, 2B和3B标准的PDF。以下是具体方法及步骤。

Jar包导入

本次程序中导入的是Spire.Pdf.jar。可在Maven程序中配置pom.xml如下内容来实现下载导入:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

或者,可将Jar下载到本地,解压,找到lib文件夹下的jar文件;然后在Java程序中执行如下操作手动导入:

 将PDF转为PDF/A

Spire.PDF for Java中的 PdfStandardsConverter 类下面枚举了可将PDF转为本地PDF/A文件和流文件的多种方法,如下表:

方法解释
PdfStandardsConverter.toPdfA1A(String filePath)保存为PDF/A1A到指定路径
PdfStandardsConverter.toPdfA1A(OutputStream stream)保存为PDF/A1A到流
PdfStandardsConverter.toPdfA1B (String filePath)保存为PDF/A1B到指定路径
PdfStandardsConverter.toPdfA1B(OutputStream stream)保存为PDF/A1B到流
PdfStandardsConverter.toPdfA2A(String filePath)保存为PDF/A2A到指定路径
PdfStandardsConverter.toPdfA2A(OutputStream stream)保存为PDF/A2A到流
PdfStandardsConverter.toPdfA2B(String filePath)保存为PDF/A2B到指定路径
PdfStandardsConverter.toPdfA2B(OutputStream stream)保存为PDF/A2B到流
PdfStandardsConverter.toPdfA3A(String filePath)保存为PDF/A3A到指定路径
PdfStandardsConverter.toPdfA3A(OutputStream stream)保存为PDF/A3A到流
PdfStandardsConverter.toPdfA3B(String filePath)保存为PDF/A3B到指定路径
PdfStandardsConverter.toPdfA3B(OutputStream stream)保存为PDF/A3B到流

实现格式转换时,只需要两行代码即可完成转换,得到目标PDF/A格式。以下是实现转换的代码示例步骤:

  1. 实例化PdfStandardsConverter类的对象,并加载PDF源文档。
  2. 通过调用上述表格中的指定方法将PDF转为指定标准格式的PDF/A,并保存。

Java

import com.spire.pdf.conversion.PdfStandardsConverter;

public class PDFtoPDFA {
    public static void main(String[]args){

        //创建PdfStandardsConverter类的对象,传入文档路径
        PdfStandardsConverter converter = new PdfStandardsConverter("sample.pdf");

        //转换为PdfA1A
        converter.toPdfA1A("ToPdfA1A.pdf");

        //转换为PdfA1B
        converter.toPdfA1B("ToPdfA1B.pdf");

        //转换为PdfA2A
        converter.toPdfA2A("ToPdfA2A.pdf");

        //转换为PdfA2B
        converter.toPdfA2B( "ToPdfA2B.pdf");

        //转换为PdfA3A
        converter.toPdfA3A( "ToPdfA3A.pdf");

        //转换为PdfA3B
        converter.toPdfA3B( "ToPdfA3B.pdf");
    }
}

PDF/A格式转换效果,如图:

 

—End—

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Excel转为PDF,可以使用Java中的Apache POI和iText库。 首先,需要从Apache POI官网下载poi-bin和poi-ooxml两个库,并将它们放在项目的classpath中。 接下来,使用POI库读取Excel文件内容,然后使用iText库将内容写入到PDF文件中。具体代码如下: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfWriter; public class ExcelToPDFConverter { public static void main(String[] args) { String excelFilePath = "path/to/excel/file.xlsx"; String pdfFilePath = "path/to/pdf/file.pdf"; try { // Load Excel file Workbook workbook = WorkbookFactory.create(new File(excelFilePath)); // Create PDF document Document document = new Document(PageSize.A4); PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath)); document.open(); // Loop through all sheets in Excel file for (int i = 0; i < workbook.getNumberOfSheets(); i++) { // Get current sheet org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(i); // Loop through all rows in current sheet for (int j = 0; j <= sheet.getLastRowNum(); j++) { // Get current row org.apache.poi.ss.usermodel.Row row = sheet.getRow(j); // Loop through all cells in current row for (int k = 0; k < row.getLastCellNum(); k++) { // Get current cell value String cellValue = row.getCell(k).getStringCellValue(); // Write cell value to PDF document document.add(new com.itextpdf.text.Paragraph(cellValue)); } } } // Close PDF document and Excel workbook document.close(); workbook.close(); System.out.println("Excel file converted to PDF successfully!"); } catch (IOException | DocumentException e) { e.printStackTrace(); } } } ``` 在执行该代码之前,需要确保已经安装了iText库,并将其添加到项目的classpath中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值