todo
This commit is contained in:
@@ -1,28 +1,109 @@
|
||||
package com.point.strategy.common;
|
||||
|
||||
import org.ofdrw.converter.ConvertHelper;
|
||||
import org.ofdrw.converter.export.PDFExporterPDFBox;
|
||||
import org.ofdrw.converter.export.ImageExporter;
|
||||
import org.ofdrw.reader.OFDReader;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import com.spire.pdf.PdfDocument;
|
||||
import com.spire.pdf.graphics.PdfImage;
|
||||
|
||||
/**
|
||||
* OFD转PDF工具类
|
||||
* 通过图片转换方式保留颜色信息
|
||||
*/
|
||||
public class OfdToPdfUtil {
|
||||
|
||||
/**
|
||||
* ofd转pdf
|
||||
* ofd转pdf - 使用高质量图片转换保留颜色
|
||||
* @param resourceFilePath OFD源文件地址
|
||||
* @param targetFilePath 需要输出PDF的目标地址
|
||||
*/
|
||||
public static void ofdToPdf(String resourceFilePath, String targetFilePath){
|
||||
try {
|
||||
// 按照OFD导出器的标准模式使用
|
||||
ConvertHelper.toPdf(
|
||||
Paths.get(resourceFilePath),
|
||||
Paths.get(targetFilePath)
|
||||
);
|
||||
// 创建临时目录存放图片
|
||||
Path ofdPath = Paths.get(resourceFilePath);
|
||||
Path tempDir = Paths.get(new File(targetFilePath).getParent(), "temp_" + System.currentTimeMillis());
|
||||
tempDir.toFile().mkdirs();
|
||||
|
||||
// 使用高质量设置将OFD转换为PNG图片
|
||||
try (OFDReader reader = new OFDReader(ofdPath)) {
|
||||
ImageExporter exporter = new ImageExporter(ofdPath, tempDir, "PNG", 30.0);
|
||||
exporter.export();
|
||||
exporter.close();
|
||||
|
||||
List<Path> imagePaths = exporter.getImgFilePaths();
|
||||
if (imagePaths.isEmpty()) {
|
||||
throw new RuntimeException("未能从OFD文件中提取图片");
|
||||
}
|
||||
|
||||
// 将图片合并为PDF,保留颜色
|
||||
imagesToPdf(imagePaths, targetFilePath);
|
||||
}
|
||||
|
||||
// 清理临时文件
|
||||
deleteDirectory(tempDir.toFile());
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("OFD转PDF失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将图片列表合并为PDF,保留原始颜色
|
||||
*/
|
||||
private static void imagesToPdf(List<Path> imagePaths, String targetFilePath) {
|
||||
try {
|
||||
PdfDocument pdf = new PdfDocument();
|
||||
|
||||
for (Path imagePath : imagePaths) {
|
||||
// 使用Spire.PDF的PdfImage.fromFile方法直接从文件加载图片
|
||||
com.spire.pdf.graphics.PdfImage image = com.spire.pdf.graphics.PdfImage.fromFile(imagePath.toString());
|
||||
|
||||
if (image != null) {
|
||||
// 创建新页面,大小与图片相同
|
||||
com.spire.pdf.PdfPageBase page = pdf.getPages().add();
|
||||
|
||||
// 计算图片适配页面的缩放比例
|
||||
double widthFitRate = image.getPhysicalDimension().getWidth() / page.getCanvas().getClientSize().getWidth();
|
||||
double heightFitRate = image.getPhysicalDimension().getHeight() / page.getCanvas().getClientSize().getHeight();
|
||||
float fitRate = Math.max((float)widthFitRate, (float)heightFitRate);
|
||||
|
||||
// 计算适配后的尺寸
|
||||
double fitWidth = image.getPhysicalDimension().getWidth() / fitRate;
|
||||
double fitHeight = image.getPhysicalDimension().getHeight() / fitRate;
|
||||
|
||||
// 将图片绘制到页面,保留原始颜色
|
||||
page.getCanvas().drawImage(image, 0, 30, fitWidth, fitHeight);
|
||||
}
|
||||
}
|
||||
|
||||
pdf.saveToFile(targetFilePath);
|
||||
pdf.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("图片合并PDF失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归删除目录
|
||||
*/
|
||||
private static void deleteDirectory(File directory) {
|
||||
File[] files = directory.listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
deleteDirectory(file);
|
||||
} else {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
directory.delete();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user