iText中的图像定位 - Java [英] Image positioning in iText - Java

查看:506
本文介绍了iText中的图像定位 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试阅读一份PDF并将其数据复制到另一份PDF中。第一个PDF包含一些文本和图像,我希望在第二个PDF中准确地写出文本结束的位置(这基本上是PDF文件的结尾)。现在它只是打印在顶部。如何进行此更改?

I am trying to read one PDF and copy its data into another PDF. The first PDF contains some text and images and I wish to write an image in the second PDF exactly where the text ends(which is basically the end of the PDF file). RIght now it just prints at the top. How can I make this change?

PdfReader reader = null;
reader = new PdfReader(Var.input);
Document document=new Document();
PdfWriter writer = null;
writer = PdfWriter.getInstance(document,new FileOutputStream(Var.output));
PdfImportedPage page = writer.getImportedPage(reader, 1); 
reader.close();  
document.open();
PdfContentByte cb = writer.getDirectContent();
// Copy first page of existing PDF into output PDF
document.newPage();
cb.addTemplate(page, 0, 0);

// Add your new data / text here
Image image = null;
image = Image.getInstance (Var.qr);
document.add(image);
document.close();


推荐答案

你应该使用PdfStamper代替PdfWriter导入页面。您的方法会抛弃所有交互式内容。您也可以在那里使用sorifiend的想法。

You should use a PdfStamper instead of a PdfWriter with imported pages. Your approach throws away all interactive contents. You can use sorifiend's idea there, too.

要确定给定页面上文本的结束位置,请查看动作中的iText,第2版示例 ShowTextMargins 解析PDF并广告显示文本边距的矩形。

To determine where the text on the given page ends, have a look at the iText in Action, 2nd edition example ShowTextMargins which parses a PDF and ads a rectangle showing the text margin.

这篇关于iText中的图像定位 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆