1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Java OFD文件转换 OFD转PDF 图片 SVG HTML工具类(2.0.0版本之前)

Java OFD文件转换 OFD转PDF 图片 SVG HTML工具类(2.0.0版本之前)

时间:2023-01-09 20:46:01

相关推荐

Java OFD文件转换 OFD转PDF 图片 SVG HTML工具类(2.0.0版本之前)

OFDRW提供了将OFD文档导出为其他格式文档的能力,如导出为图片、SVG、PDF、文本等。

OFDRW 转换模块在 2.0.0 之后抽象了多种文档导出接口,使用统一的 API 实现 OFD 文档导出功能,使用起来更加方便,可参考OFD文件转换工具类(2.0.0版本之后)

注意:java >= 1.8

一、导入jar依赖包

1.1 Maven项目引入jar包

<dependency><groupId>org.ofdrw</groupId><artifactId>ofdrw-converter</artifactId><version>1.17.18</version></dependency>

1.2 非Maven项目直接下载导入jar包

点击下载:ofdrw-converter-1.17.18.jar

二、OFD转PDF

通过对OFD的文档进行解析,使用 Apache Pdfbox生成并转换OFD中的元素为PDF内的元素实现PDF的转换。

public class OfdUtil {/*** 将ofd转换为pdf* @param originPath 源文件路径* @param pdfPath 目标文件路径*/public static void ofdToPdf(String originPath, String pdfPath) {// 1. 文件输入路径Path src = Paths.get(originPath);// 2. 转换后文件输出位置Path dst = Paths.get(pdfPath);try {// 3. OFD转换PDFConvertHelper.toPdf(src, dst);System.out.println("生成文档位置: " + dst.toAbsolutePath());} catch (GeneralConvertException e) {// pom引入相关模块GeneralConvertException 类型错误表明转换过程中发生异常e.printStackTrace();}}public static void main(String[] args) throws IOException {OfdUtil.ofdToPdf("D:/ruoyi/daima.ofd", "D:/ruoyi/daima.pdf");}}

三、OFD转图片

通过对OFD的文档进行解析,采用java.awt绘制图片,支持转换为PNG、JPEG图片格式。

注意:OFD转图片需引入OFD文档解析包(ofdrw-reader-1.17.18.jar),点击下载

public class OfdUtil {/*** 将ofd转换为图片* @param originPath 源文件路径* @param imgPath 目标文件路径*/public static void ofdToImg(String originPath, String imgPath) {// 1. 文件输入路径Path src = Paths.get("发票示例.ofd");// 2. 加载指定目录字体(非必须)// FontLoader.getInstance().scanFontDir(new File("src/test/resources/fonts"));// 3. 创建转换转换对象,设置 每毫米像素数量(Pixels per millimeter)try(OFDReader reader = new OFDReader(src);) {ImageMaker imageMaker = new ImageMaker(reader, 15);for (int i = 0; i < imageMaker.pageSize(); i++) {// 4. 指定页码转换图片BufferedImage image = imageMaker.makePage(i);Path dist = Paths.get("target", imgPath + "_" + i + ".png");// 5. 存储为指定格式图片ImageIO.write(image, "PNG", dist.toFile());}}// 6. Close OFDReader 删除工作过程中的临时文件 try close 语法}public static void main(String[] args) throws IOException {OfdUtil.ofdToImg("D:/ruoyi/daima.ofd", "D:/ruoyi/daima");}}

四、OFD转SVG

使用Apachebatik-transcoder提供的图形绘制实现java.awtAPI绘制,最终生成SVG矢量图形。

注意:OFD转SVG类似于“OFD转图片”,也需引入OFD文档解析包(ofdrw-reader-1.17.18.jar),点击下载

public class OfdUtil {/*** 将ofd转换为SVG* @param originPath 源文件路径* @param svgPath 目标文件路径*/public static void ofdToSvg(String originPath, String svgPath) {// 1. 文件输入路径Path src = Paths.get("发票示例.ofd");// 2. 加载指定目录字体(非必须)// FontLoader.getInstance().scanFontDir(new File("src/test/resources/fonts"));// 3. 创建转换转换对象,设置 每毫米像素数量(Pixels per millimeter)try(OFDReader reader = new OFDReader(src)) {SVGMaker svgMaker = new SVGMaker(reader, 20d);for (int i = 0; i < imageMaker.pageSize(); i++) {// 4. 指定页码转换SVG,得到SVG(XML)String svg = svgMaker.makePage(i);Path dist = Paths.get("target", svgPath + "_" + i + ".svg");// 5. 存储到文件。Files.write(dist, svg.getBytes());}}// 6. Close OFDReader 删除工作过程中的临时文件 try close 语法}public static void main(String[] args) throws IOException {OfdUtil.ofdToSvg("D:/ruoyi/daima.ofd", "D:/ruoyi/daima");}}

五、OFD转HTML

使用上述SVG矢量图形转换作为显示效果层A,再将OFD文档中的文字(仅)解析为SVG作为文字复制层B,B置于A层之上,文字颜色transparent,无需关心字体,在移动端同样正常显示。

public class OfdUtil {/*** 将ofd转换为HTML* @param originPath 源文件路径* @param htmlPath 目标文件路径*/public static void ofdToHtml(String originPath, String htmlPath) {try {// 1. 提供文档Path ofdIn = Paths.get(originPath);Path htmlOut = Paths.get(htmlPath);// 2. [可选]配置字体,别名,扫描目录等// FontLoader.getInstance().addAliasMapping(null, "小标宋体", "方正小标宋简体", "方正小标宋简体")// FontLoader.getInstance().scanFontDir(new File("src/test/resources/fonts"));// 3. 配置参数(HTML页面宽度(px)),转换并存储HTML到文件。ConvertHelper.toHtml(ofdIn, htmlOut, 1000);} catch (GeneralConvertException | IOException e) {e.printStackTrace();}}public static void main(String[] args) throws IOException {OfdUtil.ofdToHtml("D:/ruoyi/daima.ofd", "D:/ruoyi/daima.html");}}

大功告成,扫码下方二维码,查阅更多OFD读写库知识~

█▀▀▀▀▀▀▀█████▀▀████▀▀▀▀▀▀▀██ █▀▀▀█ █▄█▄▀█▄ █ █▀▀▀█ ██ █ █ █▄ ▄▀█ ▄█▀█ █ █ ██ ▀▀▀▀▀ █ ▄▀█ ▄ ▄ █ ▀▀▀▀▀ ██▀▀▀▀▀█▀▀▀ ▄▄▄▀ █▄▀█▀█▀█▀███ ▄▄▄▄▀█▀█ ▄▀▀▀▀ ██ ▀▀█ ▀██▀▀▀█ ▀██▀▀█▀▀▀▀█ ▄▀█▀▀ ██ █▀▄ ▄▀█▀▄▀▄▀█▀▀▄ ▀ ▀ █ ▀██ █ █▀▄█▄█▄▀▀▀█ ▀ █▀█▄██▀▀▀▀▀▀▀█ ▀▀▄█▀▄▄ █▀█ ▄▀▀▀██ █▀▀▀█ █▀█▀ ▀▀▀ ▀▀▀ ▄▀ ▄██ █ █ █ ▀▄█▀ ██▄▄█▄ ▄ ██ ▀▀▀▀▀ █ ▀▄ ▀▀▄▄▄▀▀ █▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。