java将页面转为pdf和pdf上添加盖章
java將靜態(tài)頁(yè)面轉(zhuǎn)為pdf 并在指定位置加上蓋章
直接上代碼
package com.ewaytek.edf.web.modules.test.pdf;
import com.itextpdf.text.pdf.BaseFont;
import org.springframework.web.bind.annotation.*;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.awt.geom.Rectangle2D;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
import sun.misc.BASE64Decoder;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.io.FileOutputStream;
import java.io.IOException;
/**
-
文件格式轉(zhuǎn)換工具類(lèi)
-
@author lbj
-
2015-10-8 上午10:52:22
*/
@RestController
@RequestMapping("/pdf")
public class FileTypeConvertUtil {/**
-
將HTML轉(zhuǎn)成PD格式的文件。html文件的格式比較嚴(yán)格
-
@param htmlFile
-
@param pdfFile
-
@throws Exception
*/
//
public static void html2pdf(String htmlFile, String pdfFile) throws Exception {
// step 1
String url = new File(htmlFile).toURI().toURL().toString();
System.out.println(url);
// step 2
OutputStream os = new FileOutputStream(pdfFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);// step 3 解決中文支持
ITextFontResolver fontResolver = renderer.getFontResolver();
if(“l(fā)inux”.equals(getCurrentOperatingSystem())){
fontResolver.addFont("/usr/share/fonts/chiness/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}else{
fontResolver.addFont(“c:/Windows/Fonts/simsun.ttc”, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}renderer.layout();
renderer.createPDF(os);
os.close();System.out.println(“create pdf done!!”);
}
public static String getCurrentOperatingSystem(){
String os = System.getProperty(“os.name”).toLowerCase();
System.out.println("---------當(dāng)前操作系統(tǒng)是-----------" + os);
return os;
}/**
-
根據(jù)pdf中的關(guān)鍵字,獲取文字的絕對(duì)位置,并進(jìn)行簽章
-
@param inputPath 未處理pdf
-
@param targetPath 已簽章pdf地址
-
@param imagePath 簽章圖片地址
-
@param inputPath pdf中的關(guān)鍵字
-
@param pageNum pdf頁(yè)數(shù),可傳null,默認(rèn)設(shè)置最大頁(yè)數(shù)
-
@return float的x與y值
-
@throws IOException
*/
private static void addSignImg(String inputPath, String targetPath, final String imagePath, final String keyWord, Integer pageNum) throws IOException, DocumentException {
PdfReader pdfReader = new PdfReader(inputPath);
// 讀圖片
final Image image = Image.getInstance(imagePath);
// 根據(jù)域的大小縮放圖片
image.scaleToFit(120, 120);if (null == pageNum) {
}public void renderText(TextRenderInfo textRenderInfo) {String text = textRenderInfo.getText();if (text != null && text.contains(keyWord)) {// 文字在page中的橫坐標(biāo)、縱坐標(biāo)Rectangle2D.Float textFloat = textRenderInfo.getBaseline().getBoundingRectange();float x = textFloat.x;float y = textFloat.y;// 設(shè)置圖片位置image.setAbsolutePosition(x + 50f, y - 30f);}else{image.setAbsolutePosition( 50f, 30f);}}public void endTextBlock() {}public void renderImage(ImageRenderInfo renderInfo) {}
pageNum = pdfReader.getNumberOfPages();
}
new PdfReaderContentParser(pdfReader).processContent(pageNum, new RenderListener() {
public void beginTextBlock() {});
// 獲取操作的頁(yè)面
PdfStamper stamper = new PdfStamper(pdfReader, new FileOutputStream(targetPath));
PdfContentByte under = stamper.getOverContent(pageNum);
under.addImage(image);
stamper.close();
pdfReader.close();
}
public static void main(String[] args) {
// String htmlFile = “/home/lbj/sign.jsp”;
// String pdfFile = “/home/lbj/sign.pdf”;
//要轉(zhuǎn)的靜態(tài)頁(yè)面
String htmlFile = “d:/table.html”;
String pdfFile = “C:\Users\Administrator\Desktop\part1.pdf”;
try {
FileTypeConvertUtil.html2pdf(htmlFile, pdfFile);
String imagePath = “C:\Users\Administrator\Desktop\zhang.png”;
String targetPath = “C:\Users\Administrator\Desktop\part2.pdf”;
addSignImg(pdfFile, targetPath, imagePath, “”, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}/**
- 生成PDF文件
- @author shilun.zhan
- @date 2018年2月23日 上午10:40:23
- @param request
- @param attachments
- PDFbase64格式的字符串
- @return
*/
@RequestMapping(value = “uploadBillClient”, method = RequestMethod.POST)
@ResponseBody
public Result uploadBillClient(HttpServletRequest request,
@RequestParam(value = “fileName”, required = false) String attachments) {
if (attachments == null) {
return null;
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解碼
byte[] b = decoder.decodeBuffer(attachments);
for (int i = 0; i < b.length; ++i) {
// 調(diào)整異常數(shù)據(jù)
if (b[i] < 0) {
b[i] += 256;
}
}
String imgFilePath = “C:\Users\Administrator\Desktop\hello.pdf”;
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
String imagePath = “C:\Users\Administrator\Desktop\zhang.png”;
String targetPath = “C:\Users\Administrator\Desktop\part2.pdf”;
addSignImg(imgFilePath, targetPath, imagePath, “”, null);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
-
}
總結(jié)
以上是生活随笔為你收集整理的java将页面转为pdf和pdf上添加盖章的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 招商银行笔试题之修塔游戏
- 下一篇: 招行网络科技笔试