将文本添加到使用itextsharp关闭的现有pdf [英] Adding text to existing pdf which is closed using itextsharp

查看:183
本文介绍了将文本添加到使用itextsharp关闭的现有pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我正在使用itextsharp创建PDF。现在我的要求是在现有的pdf中添加更多文本。是否有可能,那么我该怎么办呢?

Hi I am creating PDF using itextsharp. Now my requirement is to add more text to the existing pdf. Is it possible if so then how can I do that?

谢谢Dipa

推荐答案

是的,有一些限制。

确定现有页面上的内容很困难,但并非不可能。

It is difficult, but not impossible, to determine what is already on an existing page.

如果您想要做的就是在所有页面的左下角添加Y页面X,那就是简单

If all you want to do is add "page X of Y" to the bottom left corner of all your pages, that's easy.

PdfReader reader = new PdfReader( inPath );
PdfStamper stamper = new PdfStamper( reader, new FileOutputStream( outPath ) );
BaseFont font = BaseFont.createFont(); // Helvetica, WinAnsiEncoding
for (int i = 0; i < reader.getNumberOfPages(); ++i) {
  PdfContentByte overContent = stamper.getOverContent( i + 1 );
  overContent.saveState();
  overContent.beginText();
  overContent.setFontAndSize( font, 10.0f );
  overContent.setTextMatrix( xLoc, yLoc );
  overContent.showText( "Page " + (i + 1) + " of " + reader.getNumberOfPages() );
  overContent.endText();
  overContent.restoreState();
}
stamper.close();

一个很大的水印并不困难。在一个或多个预定位置向PDF添加内容是非常可行的。

A big watermark isn't much more difficult. Adding things to a PDF at one or more predetermined locations is quite doable.

在频谱的另一端是更改现有段落中的文本并重排它们。这几乎是不可能的。使用新数据重建原始PDF会容易得多。

At the other end of the spectrum is "change text within existing paragraphs and reflow them". That's all but impossible. It would be much easier to rebuild the original PDF with the new data.

事实上,如果可能的话,只需重建它们即可。你做了一次,再做一次。

In fact, if at all possible, just rebuild them. You did it once, do it again.

这篇关于将文本添加到使用itextsharp关闭的现有pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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