/src/main/java/com/eastrobot/doc/watermark/PdfProcessor.java

https://github.com/ekoz/kbase-doc · Java · 90 lines · 70 code · 7 blank · 13 comment · 4 complexity · a7557e04061ac586d6d12d0250d9065f MD5 · raw file

  1. /*
  2. * Power by www.xiaoi.com
  3. */
  4. package com.eastrobot.doc.watermark;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.net.MalformedURLException;
  11. import org.apache.poi.util.IOUtils;
  12. import com.itextpdf.text.BadElementException;
  13. import com.itextpdf.text.DocumentException;
  14. import com.itextpdf.text.Image;
  15. import com.itextpdf.text.Rectangle;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfGState;
  18. import com.itextpdf.text.pdf.PdfReader;
  19. import com.itextpdf.text.pdf.PdfStamper;
  20. /**
  21. * @author <a href="mailto:eko.z@outlook.com">eko.zhan</a>
  22. * @date 2018年9月17日 下午4:36:50
  23. * @version 1.0
  24. */
  25. public class PdfProcessor extends AbstractProcessor {
  26. public PdfProcessor(File file, File imageFile) {
  27. super(file, imageFile);
  28. }
  29. @Override
  30. public void process() throws WatermarkException {
  31. PdfReader reader = null;
  32. PdfStamper stamper = null;
  33. try {
  34. reader = new PdfReader(new FileInputStream(file));
  35. stamper = new PdfStamper(reader, new FileOutputStream(file));
  36. int pageNo = reader.getNumberOfPages();
  37. // image watermark
  38. Image img = Image.getInstance(IOUtils.toByteArray(new FileInputStream(imageFile)));
  39. float w = Math.min(img.getScaledWidth(), 460);
  40. float h = Math.min(img.getScaledHeight(), 300);
  41. for (float f : img.matrix()) {
  42. System.out.println(f);
  43. }
  44. // transparency
  45. PdfGState gs = new PdfGState();
  46. gs.setFillOpacity(0.1f);
  47. // properties
  48. PdfContentByte over;
  49. Rectangle pagesize;
  50. float x, y;
  51. // loop over every page
  52. for (int i = 1; i <= pageNo; i++) {
  53. pagesize = reader.getPageSizeWithRotation(i);
  54. x = (pagesize.getLeft() + pagesize.getRight()) / 2;
  55. y = (pagesize.getTop() + pagesize.getBottom()) / 2;
  56. over = stamper.getOverContent(i);
  57. over.saveState();
  58. over.setGState(gs);
  59. System.out.println(w + ", " + h + ", " + (x - (w / 2)) + ", " + (y - (h / 2)));
  60. // 617.0, 400.0, -10.839996, 220.95999
  61. over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2));
  62. over.restoreState();
  63. }
  64. if (stamper!=null) {
  65. stamper.close();
  66. }
  67. } catch (FileNotFoundException e) {
  68. throw new WatermarkException("FileNotFoundException", e);
  69. } catch (BadElementException e) {
  70. throw new WatermarkException("BadElementException", e);
  71. } catch (MalformedURLException e) {
  72. throw new WatermarkException("MalformedURLException", e);
  73. } catch (IOException e) {
  74. throw new WatermarkException("IOException", e);
  75. } catch (DocumentException e) {
  76. throw new WatermarkException("DocumentException", e);
  77. } finally {
  78. if (reader!=null) {
  79. reader.close();
  80. }
  81. }
  82. }
  83. }