微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

生成PDF全攻略之在已有PDF上添加内容的实现方法

下面小编就为大家带来一篇生成PDF全攻略之在已有PDF上添加内容实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

项目在变,需求在变,不变的永远是敲击键盘的程序员.....

PDF 生成后,有时候需要在PDF上面添加一些其他的内容,比如文字,图片....

经历几次失败的尝试,终于获取到了正确的代码书写方式。

在此记录总结,方便下次以不变应万变,需要的 jar 请移步:生成PDF全攻略

PdfReader reader = new PdfReader("E:\A.pdf"); pdfstamper stamper = new pdfstamper(reader, new FileOutputStream("E:\B.pdf")); PdfContentByte overContent = stamper.getoverContent(1);

上述的这段代码算是在原有 PDF 上面添加内容的核心代码,具体流程如下

•如果看官老爷够仔细的话,该代码是将原 A.pdf 读取,然后将它写入 B.pdf,然后操作 B.pdf。

•可能有的看官老爷会说,将 A 读取,然后在写入 A 中,这样肯定是不行的,在读取的时候 A 已经被加载了,不能进行修改

•我不喜欢这种方式,因为原 PDF 的信息已经存储在数据库中,其中包括 PDF 的服务器路径、旧名称、新名称、类型......

•这样就会多出一次数据库变更操作,因为这里PDF名称需要变更,而且鬼知道后续需求还会怎么变。

•这里急需 只在 PDF 中添加内容,其他的什么都不变,将代码稍微调整了一下。

FileUtil.fileChannelcopy(A.pdf,A + "tmp".pdf)); PdfReader reader = new PdfReader(A + "tmp".pdf); pdfstamper stamper = new pdfstamper(reader, new FileOutputStream(A.pdf)); PdfContentByte overContent = stamper.getoverContent(1);

代码流程就变做下面这个样子

这里引入了管道复制文件,将A 复制一份,读取副本,然后写回到原 PDF A 中,最后当然需要删除副本文件

到这里,无论后续需求怎么变,保证了pdf 的其他属性不变,就能从容面对。

管道复制代码如下:

pubpc static void fileChannelcopy(File sources, File dest) { try { FileInputStream inputStream = new FileInputStream(sources); FileOutputStream outputStream = new FileOutputStream(dest); FileChannel fileChannepn = inputStream.getChannel();//得到对应的文件通道 FileChannel fileChannelout = outputStream.getChannel();//得到对应的文件通道 fileChannepn.transferTo(0, fileChannepn.size(), fileChannelout);//连接两个通道,并且从in通道读取,然后写入out通道 inputStream.close(); fileChannepn.close(); outputStream.close(); fileChannelout.close(); } catch (Exception e) { e.printstacktrace(); } }

完整PDF其他内容代码如下:

FileUtil.fileChannelcopy(new File("E:\A.pdf"),new File("E:\A+"tmp".pdf")); PdfReader reader = new PdfReader("E:\A+"tmp".pdf"); pdfstamper stamper = new pdfstamper(reader, new FileOutputStream("E:\A.pdf")); PdfContentByte overContent = stamper.getoverContent(1); //添加文字 BaseFont font = BaseFont.createFont("STSong-pght", "UniGB-UCS2-H", BaseFont.NOT_EMbedDED); overContent.beginText(); overContent.setFontAndSize(font, 10); overContent.setTextMatrix(200, 200); overContent.showtextApgned(Element.ApGN_CENTER,"需要添加文字",580,530,0); overContent.endText(); //添加图片 PdfDictionary pdfDictionary = reader.getPageN(1); PdfObject pdfObject = pdfDictionary.get(new PdfName("MediaBox")); PdfArray pdfArray = (PdfArray) pdfObject; Image image = Image.getInstance("D:\1.jpg"); image.setAbsolutePosition(100,100); overContent.addImage(image); //添加一个红圈 overContent.setRGBColorstroke(0xFF, 0x00, 0x00); overContent.setpneWidth(5f); overContent.elppse(250, 450, 350, 550); overContent.stroke(); stamper.close();

以上这篇生成PDF全攻略之在已有PDF上添加内容实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐