因签章平台需要,将word文档和图片文件转成pdf保存。
代码如下:
- 需要导入的mavenjar包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jdom/jdom -->
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.0.3</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.20</version>
</dependency>
<!--压缩包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.20</version>
</dependency>
<dependency>
<groupId>org.ofdrw</groupId>
<artifactId>ofdrw-full</artifactId>
<version>1.12.0</version>
</dependency>
- 工具类
package com.saxon;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Paths;
import org.ofdrw.converter.ConvertHelper;
import org.ofdrw.converter.FontLoader;
import com.aspose.words.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import com.saxon.pdfToOfdBao.OFDRender;
/***
* 相关格式转pdf
* @author saxon
* @date 2021年7月15日
*/
public class PdfUtils {
/***
* word转pdf
* @param wordFile word文件路径
* @param pdfFile pdf文件路径
*/
public static boolean wordToPdf(String wordFile,String pdfFile){
boolean bool=true;
try {
//doc路径
Document document = new Document(wordFile);
//pdf路径
File outputFile = new File(pdfFile);
//操作文档保存
document.save(outputFile.getAbsolutePath(), com.aspose.words.SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
bool=false;
}
return bool;
}
/***
* 图片转pdf
* @param imgFile
* @param pdfFile
*/
public static boolean imageToPdf(String imgFile, String pdfFile){
boolean bool=true;
try {
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
OutputStream os = new FileOutputStream(new File(pdfFile));
PdfWriter.getInstance(document,os);
document.open();
Image image = Image.getInstance(imgFile);
float documentWidth = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin();
float documentHeight = documentWidth / 580 * 450;//重新设置宽高
image.scaleAbsolute(documentWidth, documentHeight);//重新设置宽高
document.add(image);
document.close();
} catch (Exception ex) {
ex.printStackTrace();
bool=false;
}
return bool;
}
/***
* pdf转ofd
* @param pdfFile pdf文件路径
* @param ofdFile ofd文件路径
*/
public static boolean pdfToOfd(String pdfFile,String ofdFile){
boolean bool=true;
try {
//转换
OFDRender.convertPdfToOfd(new FileInputStream(pdfFile), new FileOutputStream(ofdFile));
} catch (Exception e) {
e.printStackTrace();
bool=false;
}
return bool;
}
/***
* ofd转pdf
* @param ofdFile ofd文件路径
* @param pdfFile pdf文件路径
*/
public static boolean ofdTopdf(String ofdFile,String pdfFile){
boolean bool=true;
try {
FontLoader.getInstance()
.addAliasMapping(null, "小标宋体", "方正小标宋简体", "方正小标宋简体")
.addAliasMapping(null, "KaiTi_GB2312", "楷体", "楷体")
.addSimilarFontReplaceRegexMapping(null, ".*Kai.*", null, "楷体")
.addSimilarFontReplaceRegexMapping(null, ".*Kai.*", null, "楷体")
.addSimilarFontReplaceRegexMapping(null, ".*MinionPro.*", null, "SimSun")
.addSimilarFontReplaceRegexMapping(null, ".*SimSun.*", null, "SimSun")
.addSimilarFontReplaceRegexMapping(null, ".*Song.*", null, "宋体")
.addSimilarFontReplaceRegexMapping(null, ".*MinionPro.*", null, "SimSun");
FontLoader.setSimilarFontReplace(true);
//转换
ConvertHelper.toPdf(Paths.get(ofdFile), Paths.get(pdfFile));
} catch (Exception e) {
e.printStackTrace();
bool=false;
}
return bool;
}
public static void main(String[] args) {
// word转pdf
// String wordFile = "D:\\一些资料\\签章加解密接口.docx";
// String pdfFile1 = "D:\\\\一些资料\\\\签章加解密接口.pdf";
// if(wordToPdf(wordFile,pdfFile1)){
// System.out.println("word转pdf成功");
// }else{
// System.out.println("word转pdf失败");
// }
// String imgFile="C:\\Users\\Saxon\\Desktop\\logo.png";
// String pdfFile2="C:\\Users\\Saxon\\Desktop\\logo.pdf";
// if(imageToPdf(imgFile,pdfFile2)){
// System.out.println("img转pdf成功");
// }else{
// System.out.println("img转pdf失败");
// }
//pdfToOfd("D:\\gitee_java_space\\PDF2OFD\\render\\src\\test\\resources\\n.pdf","d:\\n.ofd");
}
}
以上就是word和图片转pdf的全部代码了。需要完整代码的同学可通过我的gitee仓库word-img2pdf中获取,同时欢迎大家添加我的微信骚扰哦!拜拜!