5

あるPDFを読み取って、そのデータを別のPDFにコピーしようとしています。最初のPDFにはいくつかのテキストと画像が含まれているので、2番目のPDFのテキストの終わり(基本的にはPDFファイルの終わり)に画像を書きたいと思います。右は、上部に印刷されるだけです。どうすればこの変更を行うことができますか?

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();
4

2 に答える 2

8

これを試して:

最初に画像を配置する必要のある場所/座標を取得し、次に下から2行目をコードに追加して、画像がその場所「X、Y」に挿入されるようにします。

Image image = Image.getInstance(String RESOURCE);
image.setAbsolutePosition(X, Y);
writer.getDirectContent().addImage(image);

iText 5のいくつかの例については、こちらをご覧ください:https ://itextpdf.com/en/resources/examples/itext-5-legacy/chapter-3-adding-content-absolute-positions

于 2012-10-23T00:58:50.683 に答える
2

インポートされたページでは、PdfWriterの代わりにPdfStamperを使用する必要があります。あなたのアプローチはすべてのインタラクティブなコンテンツを捨てます。そこでもsorifiendのアイデアを使うことができます。

特定のページのテキストがどこで終了するかを判断するには、iText in Action、第2版の例ShowTextMarginsを見てください。これは、PDFを解析し、テキストの余白を示す長方形を広告します。

于 2012-10-23T11:53:40.223 に答える