问题描述
我想创建一个PdfWriter对象并为页眉和页脚设置事件。
问题是如果我创建一个新的PDF它是有效的。但我的问题是我已经在输出流中有一个PDF。请在下面找到我的示例代码。
凭证凭证=新凭证();
try {
//第2步:
FileInputStream =新FileInputStream(D://2.pdf);
int nRead;
byte [] data = new byte [16384];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
while((nRead = is.read(data,0,data.length))!= -1){
buffer.write(data,0,nRead);
}
buffer.flush();
PdfWriter writer = PdfWriter.getInstance(document,buffer);
writer.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);
writer.setPageEvent(new DossierPortalUtil());
document.setMargins(36,36,54,72);
//第3步:
document.open();
document.add(new Chunk(testing));
} catch(Exception de){
de.printStackTrace();
}
finally {
document.close();
}
如果我评论该行
document.add(new Chunk(testing));
我得到例外
没有评论没有例外,但它没有添加页眉和页脚。任何线索都非常受欢迎。
问候,
Tina
在这里输入代码
是的。
您尝试使用 PdfWriter
修改现有PDF,当您应该使用 PdfStamper 使用
PdfWriter
和文件
。
您需要创建一个 ColumnText
对象,并通过调用 myStamper.getOverContent(pageNum)
获得 PdfContentByte
。
您将段落/块/等添加到 ColumnText
,并将 PdfContentByte
传递给它(和一些位置参数)来绘制文本。
或者,你可以用你的文本(和其他任何东西)创建一个单独的PDF,然后使用 PdfStamper
& PdfImportedPage
导入这些页面并将其写在现有页面之上。 PDF页面背景是透明的,直到你在它们上面绘制一些东西,因此文本(和东西)将显示在现有页面的顶部。这显然效率较低,因为必须将第二个文档转换为PDF语法中的字节数组(如果您使用 ByteArrayOutputStream
而不是写入文件,会更慢),再次解析,然后添加到原始文档并第二次写出。
使用<$ c $值得多花一点力气c> ColumnText 。
您还需要使用 PdfContentByte $ c直接编写页眉和页脚$ c>调用,但您必须在
PdfPageEvent
中执行此操作,因此这些更改应该非常简单。
I want to create a PdfWriter Object and set Events for Header and Footer.The problem is it works if I create a new PDF. But my problem is I already have a PDF in Output Stream. Please find my sample code below.
Document document = new Document();
try {
// step 2:
FileInputStream is = new FileInputStream("D://2.pdf");
int nRead;
byte[] data = new byte[16384];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
PdfWriter writer = PdfWriter.getInstance(document,buffer);
writer.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);
writer.setPageEvent(new DossierPortalUtil());
document.setMargins(36, 36, 54, 72);
// step 3:
document.open();
document.add( new Chunk("testing"));
} catch (Exception de) {
de.printStackTrace();
}
finally{
document.close();
}
If I comment the line
document.add( new Chunk("testing"));
I get an exception
Without commenting there are no exceptions but it doesnt add the Header and Footer. Any clues are highly appreciated.
Regards,Tina
enter code here
Yep.
You're trying to modify an existing PDF with PdfWriter
, when you should be using PdfStamper
.
Adding text with a stamper is Far Less Trivial than doing so with PdfWriter
and a Document
.
You need to create a ColumnText
object, and get a PdfContentByte
by calling myStamper.getOverContent(pageNum)
.
You add the paragraphs/chunks/etc to the ColumnText
, and pass it the PdfContentByte
(and some positional parameters) to draw the text.
Alternatively, you can create a separate PDF with your text (and anything else), then use PdfStamper
& PdfImportedPage
to import those pages and write them over top of the existing ones. PDF page backgrounds are transparent until you draw something over them, so the text (and stuff) will appear over top of the existing page. This is noticeably less efficient, as the second document has to be converted to a byte array in PDF syntax (if you're using a ByteArrayOutputStream
instead of writing to a file, which would be even slower), parsed again, and then added to the original doc and written out a second time.
It's worth a little extra effort to use ColumnText
.
You'll also need to write your header and footer directly with PdfContentByte
calls, but you have to do that already within your PdfPageEvent
, so those changes should be quite simple.
这篇关于PdfWriter和事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!