pdf如何制作电子图书,如何制作pdf电子文档
发表于 ・ 服务项目
不少人都想知道关于pdf如何制作电子图书和如何制作pdf电子文档,接下来让小编详细讲解吧!
pdf如何制作电子图书
以前在写文档在线预览时留存下了1个小坑,那时对比推举的作法是将各种类型的文档都由后端一统转成pdf样式再由前面进行展现,可是那时并没有供应将各种类型的文档转pdf的办法,这一次就来填一下这一个坑呢。
事先计划编码根据 aspose-words(用在word.txt转pdf),itextpdf(用在ppt.照片.excel转pdf),因此预先要在行业里下面以下依靠
1.要的maven依靠word转pdf的办法比较简单,aspose-words根本都被帮咱们搞定了,doc.docx都能支-持啦。 编码 认证编码 转化结果如以下,样式.图文都没什么疑,doc.docx通过认证也都能转化成功 txt文件转pdf文件编码直-接复用word的便可 编码: 认证编码 转化结果如以下 PPT文件转pdf文件,听别人说他们公司不让用ppt,那就让咱们把ppt转成pdf再用吧了。本来从这边开始编码就开始繁杂起来了,这边用到了Apache poi.itextpdf.Graphics2D3个库,因此我连合这3个库同时间兼容ppt.pptx写出了第1版编码 认证编码 转化结果如以下 可以见到转化结果并不怎么好,ppt的内容展现不全了。因此我开始在网上找解决方案,结局找出了1个很神秘的解决方案,就绘制的照片先写在1个PdfPTable对象上,再把PdfPTable对象放到document离开,因此我依据这一个改了改编码写出了第二版编码 转化结果如以下 可以见到ppt内容早已经展现完全了,到此本来ppt转pdf功效早已经根本完成了,可是显现结果仍然不算完美终究咱们本来要的是在pdf里和在ppt看的是相同的结果,并且每页ppt的长宽本来都是相同的,因此我就在想能不可以设定pdf每页的长宽,把pdf每页的长宽设定成和ppt的长宽相同了。因此我开始看初始化pdf document的源码配置 随后发觉com.itextpdf.text.Document除去默许的构造函数外还有这那样1个构造函数 随后com.itextpdf.text.Rectangle类点进入就发觉了可以设定长宽的构造函数 因此我编码中的初始化Document进行如以下调理(依据第1版编码改,第二版的PdfPTable可以不用了) 转化结果如以下 现如今展现的结果早已经和ppt上相同了,并且通过认证ppt和pptx都是可以转化成功的啦。 照片转pdf用到了用到了Apache poi.itextpdf2个库,由于itextpdf支-持剖析的照片有限,点开c读取照片的办法com.itextpdf.text.Image.getInstance,咱们可以见到那样一段源码: 由此可以可知itextpdf支-持剖析的照片唯有gif.jpeg.png.bmp.wmf.tiff. jbig2这多种,这一些本来早已经根本包括了全部主要流派的图片格式(百度照片因此我用的webp样式是非主流样式呢?),并且图片格式不是光改后缀就行的,必需要用样式转换器转化啦。比方下面这张图尽管后缀是jpeg,但经过察看照片短信可知现实样式是webg样式itextpdf相同没法剖析 [外链照片转存失败,源站也许有防盗链机制,提议将照片保留下去直-接上传(img-BjLHOWSY-1685380307992)(C:\Users\jie\Desktop\作文\转pdf\pic\改后缀.png)] 话不多说咱们先连合Apache poi.itextpdf2个库简易协定版根本的照片转化pdf编码 认证编码 转化结果如以下 从结果可以咱们可以见到这一个照片本来是没有显现全部的, 本来小一点的照片是没什么疑的,可是由于pdf设定的每页都是A4大小,因此在照片过大时会显现不完全,因此咱们在照片过大时要对照片进行有些调理,调理后的编码如以下 转化结果如以下 可以见到现如今照片早已经完全的显现在pdf的页面中了,到这边您也许会有1个迷惑,为何这一次不愿上边ppt转化pdf相同把pdf的页面长宽设定成和照片相同,并且去调理照片的大小呢呢。之所以那样作的原因是因为在下面的多照片转换成1个pdf文件时,常常是不可以保证每张照片的长宽比率是相同的,为了保证每张照片都能完全的显现,因此只能调理照片的大小啦。 将照片1张1张的导成pdf终究很麻烦,比方我1个文件夹下面有好多张照片,我想将该文件夹下的全部照片都导入pdf中作个《美人谱》,我该怎样作呢吗?安排!因此编码调理成了下面那样 支-持多照片转pdf编码 认证编码 转化结果如以下 本来excel转pdf在现实的运用情景中应当对比罕有,可是前方也说了这么多文件转pdf的方法了,那excel转pdf也就一并说说吧啦。 编码如以下 认证编码 转化结果如以下 由于spire不在maven***库房里以及阿里云的maven库房中,因此在运用spire以前要现如今maven中配置新的maven库房位置,配置如以下; 随后再pom中引进依靠 收取费用 或许 不收取费用的 不收取费用版本基础功效都能用 编码 认证编码 转化结果如以下 好了将word.txt.ppt.excel.照片等文件转成pdf文件完成方法早已经全数说完了,谢谢阅览到这边的同伴! 含签字约束的PDF文档通常是不可以编写的,这么怎么样祛除签字呢吗?这边就要运用专长的编辑器了~ 咱们利用迅捷PDF编辑器便可放松实现祛除! 非常简单,您学会了吗吗?请点赞+关心来支-持我哦! pdf如何制作电子图书和如何制作pdf电子文档的话题就聊到这,希望对大家有所帮助。 <dependency> <groupId>com.luhuiguo</groupId> <artifactId>aspose-words</artifactId> <version>23.1</version> </dependency> <啦!-- poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-oo2.后面用到的工具类编码package com.fhey.service.common.utils.file;import cn.hutool.core.util.StrUtil;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.File;import java.io.FileInputStream;import java.io.IOException;/** * @author fhey * @date 2023-04-20 11:15:58 * @description: 文件工具类 */public class FileUtil File sourceFile = new File(sourceFilePath); String sourceFileName = sourceFile.getName(); if (sourceFile.isFile()) return destFilePath + File.separator + sourceFileName + StrUtil.DOT + ext; //判定文件是否是照片 public static boolean isImage(File file) throws IOException return false; //将文件头转换成16进制字符串 public static String bytesToHexString(byte[] src) for (int i = 0; i < src.length; i++) builder.append(hv); return builder.toString(); 一.word文件转pdf文件(支-持doc.docx)public static void wordToPdf(String wordPath, String pdfPath) throws Exception public static void main(String[] args) throws Exception public static void txtToPdf(String txtPath, String pdfPath) throws Exception public static void main(String[] args) throws Exception public static void pptToPdf(String pptPath, String pdfPath) throws IOException else Dimension dimension = slideShow.getPageSize(); fileOutputStream = new FileOutputStream(pdfPath); //document = new com.itextpdf.text.Document(new com.itextpdf.text.Rectangle((float) dimension.getWidth(), (float) dimension.getHeight())); document = new com.itextpdf.text.Document(); pdfWriter = PdfWriter.getInstance(document, fileOutputStream); document.open(); for (Slide<?, ?> slide : slideShow.getSlides()) catch (Exception e) finally if (fileOutputStream 啦!= null) if (pdfWriter 啦!= null) catch (IOException e) private static void setPPTFont(Slide<?, ?> slide, String fontFamily) public static void main(String[] args) throws Exception public static void pptToPdf(String pptPath, String pdfPath) throws IOException else Dimension dimension = slideShow.getPageSize(); fileOutputStream = new FileOutputStream(pdfPath); //document = new com.itextpdf.text.Document(new com.itextpdf.text.Rectangle((float) dimension.getWidth(), (float) dimension.getHeight())); document = new com.itextpdf.text.Document(); pdfWriter = PdfWriter.getInstance(document, fileOutputStream); document.open(); PdfPTable pdfPTable = new PdfPTable(1); for (Slide<?, ?> slide : slideShow.getSlides()) catch (Exception e) finally if (fileOutputStream 啦!= null) if (pdfWriter 呀!= null) catch (IOException e) com.itextpdf.text.Document document = new com.itextpdf.text.Document();public Document(Rectangle pageSize) public Rectangle(float urx, float ury) document = new com.itextpdf.text.Document();//改为如以下document = new com.itextpdf.text.Document(new com.itextpdf.text.Rectangle((float) dimension.getWidth(), (float) dimension.getHeight()));ppt转pdf第三版编码(最终版)public static void pptToPdf(String pptPath, String pdfPath) throws IOException else Dimension dimension = slideShow.getPageSize(); fileOutputStream = new FileOutputStream(pdfPath); //document = new com.itextpdf.text.Document(); document = new com.itextpdf.text.Document(new com.itextpdf.text.Rectangle((float) dimension.getWidth(), (float) dimension.getHeight())); pdfWriter = PdfWriter.getInstance(document, fileOutputStream); document.open(); for (Slide<?, ?> slide : slideShow.getSlides()) catch (Exception e) finally if (fileOutputStream 呀!= null) if (pdfWriter 啦!= null) catch (IOException e) Image img; if (c1 == 71 && c2 == 73 && c3 == 70) if (c1 == 255 && c2 == 216) Jpeg2000 var38; if (c1 == 0 && c2 == 0 && c3 == 0 && c4 == 12) if (c1 == 255 && c2 == 79 && c3 == 255 && c4 == 81) if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1] && c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) if (c1 == 215 && c2 == 205) if (c1 啦!= 66 || c2 吧!= 77) else img = TiffImage.getTiffImage(ra, 1); img.url = url; img = img; return img; catch (RuntimeException var32) throw var32; finally if (c1 == 151 && c2 == 74 && c3 == 66 && c4 == 50 && c5 == 13 && c6 == 10 && c7 == 26 && c8 == 10) else img = JBIG2Image.getJbig2Image(ra, 1); img.url = url; img = img; return img; finally public static void imageToPdf(String imgPath, String pdfPath) throws Exception public static void main(String[] args) throws Exception public static void imageToPdf(String imgPath, String pdfPath) throws Exception image.setAlignment(com.itextpdf.text.Image.ALIGN_CENTER); document.add(image); document.close(); public static void imageToPdf(String imagePath, String pdfPath) throws Exception else ; imageToPdf(files, pdfPath); public static void imageToPdf(File[] imageFiles, String pdfPath) throws Exception image.setAlignment(com.itextpdf.text.Image.ALIGN_CENTER); //document.setMargins(50, 150, 50, 50); //document.setPageSize(new com.itextpdf.text.Rectangle(width, height)); document.newPage(); document.add(image); catch (Exception e) document.close(); public static void main(String[] args) throws Exception public static void excelToPdf(String excelPath, String pdfPath) throws DocumentException, IOException else table.setHeaderRows(1); document.add(table); document.close(); public static void main(String[] args) throws Exception <repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories><groupId>e-iceblue</groupId> <artifactId>spire.office</artifactId> <version>5.3.1</version> </dependency><groupId>e-iceblue</groupId> <artifactId>spire.office.free</artifactId> <version>5.3.1</version> </dependency>public static void excelToPdf2(String excelPath, String pdfPath) throws DocumentException, IOException, InvalidFormatException public static void main(String[] args) throws Exception 如何制作pdf电子文档
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。