不少人都想知道关于pdf如何制作电子图书和如何制作pdf电子文档,接下来让小编详细讲解吧!

pdf如何制作电子图书

以前在写文档在线预览时留存下了1个小坑,那时对比推举的作法是将各种类型的文档都由后端一统转成pdf样式再由前面进行展现,可是那时并没有供应将各种类型的文档转pdf的办法,这一次就来填一下这一个坑呢。

事先计划

编码根据 aspose-words(用在word.txt转pdf),itextpdf(用在ppt.照片.excel转pdf),因此预先要在行业里下面以下依靠

1.要的maven依靠

<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)

word转pdf的办法比较简单,aspose-words根本都被帮咱们搞定了,doc.docx都能支-持啦。

编码

public static void wordToPdf(String wordPath, String pdfPath) throws Exception

认证编码

public static void main(String[] args) throws Exception

转化结果如以下,样式.图文都没什么疑,doc.docx通过认证也都能转化成功

二.txt文件转pdf文件

txt文件转pdf文件编码直-接复用word的便可

编码:

public static void txtToPdf(String txtPath, String pdfPath) throws Exception

认证编码

public static void main(String[] args) throws Exception

转化结果如以下

三.PPT文件转pdf文件(支-持ppt.pptx)

PPT文件转pdf文件,听别人说他们公司不让用ppt,那就让咱们把ppt转成pdf再用吧了。本来从这边开始编码就开始繁杂起来了,这边用到了Apache poi.itextpdf.Graphics2D3个库,因此我连合这3个库同时间兼容ppt.pptx写出了第1版编码

ppt转pdf第1版编码

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

转化结果如以下

可以见到转化结果并不怎么好,ppt的内容展现不全了。因此我开始在网上找解决方案,结局找出了1个很神秘的解决方案,就绘制的照片先写在1个PdfPTable对象上,再把PdfPTable对象放到document离开,因此我依据这一个改了改编码写出了第二版编码

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(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)

转化结果如以下

可以见到ppt内容早已经展现完全了,到此本来ppt转pdf功效早已经根本完成了,可是显现结果仍然不算完美终究咱们本来要的是在pdf里和在ppt看的是相同的结果,并且每页ppt的长宽本来都是相同的,因此我就在想能不可以设定pdf每页的长宽,把pdf每页的长宽设定成和ppt的长宽相同了。因此我开始看初始化pdf document的源码配置

com.itextpdf.text.Document document = new com.itextpdf.text.Document();

随后发觉com.itextpdf.text.Document除去默许的构造函数外还有这那样1个构造函数

public Document(Rectangle pageSize)

随后com.itextpdf.text.Rectangle类点进入就发觉了可以设定长宽的构造函数

public Rectangle(float urx, float ury)

因此我编码中的初始化Document进行如以下调理(依据第1版编码改,第二版的PdfPTable可以不用了)

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)

转化结果如以下

现如今展现的结果早已经和ppt上相同了,并且通过认证ppt和pptx都是可以转化成功的啦。

四.照片转pdf文件

照片转pdf用到了用到了Apache poi.itextpdf2个库,由于itextpdf支-持剖析的照片有限,点开c读取照片的办法com.itextpdf.text.Image.getInstance,咱们可以见到那样一段源码:

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

由此可以可知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第1版编码

public static void imageToPdf(String imgPath, String pdfPath) throws Exception

认证编码

public static void main(String[] args) throws Exception

转化结果如以下

从结果可以咱们可以见到这一个照片本来是没有显现全部的, 本来小一点的照片是没什么疑的,可是由于pdf设定的每页都是A4大小,因此在照片过大时会显现不完全,因此咱们在照片过大时要对照片进行有些调理,调理后的编码如以下

单照片转pdf第二版编码

public static void imageToPdf(String imgPath, String pdfPath) throws Exception image.setAlignment(com.itextpdf.text.Image.ALIGN_CENTER); document.add(image); document.close();

转化结果如以下

可以见到现如今照片早已经完全的显现在pdf的页面中了,到这边您也许会有1个迷惑,为何这一次不愿上边ppt转化pdf相同把pdf的页面长宽设定成和照片相同,并且去调理照片的大小呢呢。之所以那样作的原因是因为在下面的多照片转换成1个pdf文件时,常常是不可以保证每张照片的长宽比率是相同的,为了保证每张照片都能完全的显现,因此只能调理照片的大小啦。

将文件夹下的全部照片导成1个pdf

将照片1张1张的导成pdf终究很麻烦,比方我1个文件夹下面有好多张照片,我想将该文件夹下的全部照片都导入pdf中作个《美人谱》,我该怎样作呢吗?安排!因此编码调理成了下面那样

支-持多照片转pdf编码

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

转化结果如以下

五.excel文件转pdf文件

本来excel转pdf在现实的运用情景中应当对比罕有,可是前方也说了这么多文件转pdf的方法了,那excel转pdf也就一并说说吧啦。

方法1 运用itextpdf

编码如以下

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

转化结果如以下

方法2 运用spire

由于spire不在maven***库房里以及阿里云的maven库房中,因此在运用spire以前要现如今maven中配置新的maven库房位置,配置如以下;

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

随后再pom中引进依靠

收取费用

<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

转化结果如以下

好了将word.txt.ppt.excel.照片等文件转成pdf文件完成方法早已经全数说完了,谢谢阅览到这边的同伴!

如何制作pdf电子文档

含签字约束的PDF文档通常是不可以编写的,这么怎么样祛除签字呢吗?这边就要运用专长的编辑器了~

咱们利用迅捷PDF编辑器便可放松实现祛除!

1.打开有签字的PDF文件2.单击左上角文档→数字签名→「消除全部签字」便可啦。3.消除实现,PDF文档便可平常编写了。

非常简单,您学会了吗吗?请点赞+关心来支-持我哦!

pdf如何制作电子图书和如何制作pdf电子文档的话题就聊到这,希望对大家有所帮助。


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。