最近一直找发票合并的小工具,没想到这玩意儿还收费,想着就自己用pdfbox的库弄一个。最终效果如图:
1.maven依赖
org.apache.pdfbox pdfbox2.0.24 org.projectlombok lombok1.16.10 cn.hutool hutool-all5.5.7
2.合并代码
网上绝大多数都是多个PDF文件合并成一个文件,每个PDF是单独的页面。想两张合成一张放到一个页面就只能自己改造了。
多文件合并成多页:
public static void mergeDocument(String path) throws Exception { File file = new File(path); if (file.isDirectory()){ PDFMergerUtility merger = new PDFMergerUtility(); File[] files = file.listFiles(); for (File tmp: files){ System.out.println(tmp.getAbsolutePath()); merger.addSource(tmp); } String out = path + File.separator + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + "合并发票.pdf"; merger.setDestinationFileName(out); merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly()); } }
多文件合成单页两张:
public class Reader { /** * 计算发票金额 * @param tmp * @return */ @SneakyThrows public static BigDecimal summary(File tmp) { PDDocument doc = PDDocument.load(tmp); //创建检索对象 PDFTextStripper pdfStripper = new PDFTextStripper(); String content = pdfStripper.getText(doc); doc.close(); Pattern pattern = Pattern.compile("[¥¥]\\s*\\d+\\.\\d{2}"); Matcher matcher = pattern.matcher(content); ListamountList = new ArrayList<>(); String str = ""; while (matcher.find()){ str = matcher.group(); String amountStr = str.replaceAll("[¥¥]\\s*", ""); amountList.add(new BigDecimal(amountStr)); } System.out.println(tmp.getName() +","+ str); Collections.sort(amountList); return amountList.get(amountList.size()-1); } }
public class Test { /** * 文件列表 * @param path * @return */ public static ListfileList(String path){ List result = new ArrayList (); File file = new File(path); if (file.isDirectory()){ File[] files = file.listFiles(); BigDecimal sum = BigDecimal.ZERO; for (File tmp: files){ if (!tmp.getName().startsWith("test")){ result.add(tmp.getAbsolutePath()); BigDecimal ele = Reader.summary(tmp); sum = sum.add(ele); } } System.out.println("总金额:¥ "+sum.toPlainString()); } return result; } public static void main(String[] args) throws Exception { String path = "E:\\文档\\发票\\test\\test_"+ DateUtil.format(new Date(), "yyyyMMdd_HHmmss")+".pdf"; //创建空白文档 PDDocument document = new PDDocument(); //布局 LayerUtility layerUtility = new LayerUtility(document); //添加文档 List files = fileList("E:\\文档\\发票\\test"); //缩放比例 float scaleX = 0.95f; float scaleY = 0.95f; //创建空白页面 PDPage blankPage = new PDPage(); document.addPage(blankPage); PDRectangle rectangle = blankPage.getMediaBox(); float height = rectangle.getHeight(); float width = rectangle.getWidth(); for (int i=0;i 说明下,这个PDF的文档坐标是左下角是原点坐标,向右X轴增加,向上Y轴增加。
还没有评论,来说两句吧...