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

https://github.com/ekoz/kbase-doc · Java · 66 lines · 36 code · 6 blank · 24 comment · 11 complexity · 8f4ac78593b3f89aa3d4c2449be56db1 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.util.HashMap;
  7. import java.util.Map;
  8. import org.apache.commons.io.FilenameUtils;
  9. import org.springframework.util.Assert;
  10. import com.eastrobot.doc.util.FileExtensionUtils;
  11. /**
  12. * 中央处理器
  13. * @author <a href="mailto:eko.z@outlook.com">eko.zhan</a>
  14. * @date 2018年9月18日 上午9:40:29
  15. * @version 1.0
  16. */
  17. public class WatermarkProcessor {
  18. private static Map<String, File> map = new HashMap<String, File>();
  19. /**
  20. * 图片水印
  21. * @author eko.zhan at 2018年9月18日 上午9:48:57
  22. * @param file
  23. * @param imageFile
  24. * @throws WatermarkException
  25. */
  26. public static void process(File file, File imageFile) throws WatermarkException {
  27. AbstractProcessor processor = null;
  28. if (FileExtensionUtils.isWord(file.getName())) {
  29. processor = new WordProcessor(file, imageFile);
  30. } else if (FileExtensionUtils.isExcel(file.getName())) {
  31. processor = new ExcelProcessor(file, imageFile);
  32. } else if (FileExtensionUtils.isPpt(file.getName())) {
  33. processor = new PowerPointProcessor(file, imageFile);
  34. } else if (FileExtensionUtils.isPdf(file.getName())) {
  35. processor = new PdfProcessor(file, imageFile);
  36. } else if (FileExtensionUtils.isImage(file.getName())) {
  37. processor = new ImageProcessor(file, imageFile);
  38. }
  39. if (processor!=null) {
  40. processor.process();
  41. }else {
  42. throw new WatermarkException("不支持文件格式为 " + FilenameUtils.getExtension(file.getName()) + " 的水印处理");
  43. }
  44. }
  45. /**
  46. * 文本水印
  47. * @author eko.zhan at 2018年9月18日 上午11:08:08
  48. * @param file
  49. * @param text
  50. * @throws WatermarkException
  51. */
  52. public static void process(File file, String text) throws WatermarkException {
  53. Assert.hasText(text, "水印文本不能为空");
  54. //通过 text 生成 Image File
  55. if (map.get(text)==null) {
  56. map.put(text, FontImage.createImage(text));
  57. }
  58. process(file, map.get(text));
  59. }
  60. }