日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java将页面转为pdf和pdf上添加盖章

發布時間:2023/12/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java将页面转为pdf和pdf上添加盖章 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java將靜態頁面轉為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;
/**

  • 文件格式轉換工具類

  • @author lbj

  • 2015-10-8 上午10:52:22
    */
    @RestController
    @RequestMapping("/pdf")
    public class FileTypeConvertUtil {

    /**

    • 將HTML轉成PD格式的文件。html文件的格式比較嚴格

    • @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(“linux”.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("---------當前操作系統是-----------" + os);
    return os;
    }

    /**

    • 根據pdf中的關鍵字,獲取文字的絕對位置,并進行簽章

    • @param inputPath 未處理pdf

    • @param targetPath 已簽章pdf地址

    • @param imagePath 簽章圖片地址

    • @param inputPath pdf中的關鍵字

    • @param pageNum pdf頁數,可傳null,默認設置最大頁數

    • @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);
      // 根據域的大小縮放圖片
      image.scaleToFit(120, 120);

      if (null == pageNum) {
      pageNum = pdfReader.getNumberOfPages();
      }
      new PdfReaderContentParser(pdfReader).processContent(pageNum, new RenderListener() {
      public void beginTextBlock() {

      }public void renderText(TextRenderInfo textRenderInfo) {String text = textRenderInfo.getText();if (text != null && text.contains(keyWord)) {// 文字在page中的橫坐標、縱坐標Rectangle2D.Float textFloat = textRenderInfo.getBaseline().getBoundingRectange();float x = textFloat.x;float y = textFloat.y;// 設置圖片位置image.setAbsolutePosition(x + 50f, y - 30f);}else{image.setAbsolutePosition( 50f, 30f);}}public void endTextBlock() {}public void renderImage(ImageRenderInfo renderInfo) {}

      });
      // 獲取操作的頁面
      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”;
    //要轉的靜態頁面
    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) {
      // 調整異常數據
      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;
      }

}

總結

以上是生活随笔為你收集整理的java将页面转为pdf和pdf上添加盖章的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。