English 中文(简体)
使用iTextSharp PdfStamper在现有PDF上覆盖图像
原标题:Using iTextSharp PdfStamper to overlay an image on existing PDF
  • 时间:2011-02-08 20:29:07
  •  标签:
  • c#
  • itext

我能够使用PDFStamper和PdfContentByte content.AddImage方法将图像覆盖到现有的PDF文档上。

当现有文档的顶部已经覆盖了一个图像时,我的问题就出现了。实际上,你可以看到我试图覆盖的小图像的顶部边缘。它明显隐藏在现有的图像覆盖之下。

我在尝试将叠加的图像显示在现有图像叠加的顶部时遇到问题。

我的代码:

System.Drawing.Image bitmap

PdfReader pdfReader = new PdfReader(pathToOriginalPdf);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(pathToTimestampedPdf, FileMode.Create, FileAccess.Write, FileShare.None));

MemoryStream imageStream = new MemoryStream();
bitmap.Save(imageStream, ImageFormat.Bmp);
byte[] bitmapBytes = imageStream.ToArray();

iTextSharp.text.Image image = Image.GetInstance(bitmapBytes);

PdfContentByte underContent;

try
{
    underContent = pdfStamper.GetOverContent(1);
    underContent.AddImage(image);
}

我需要一种方法,要么将现有的图像叠加到PDF内容上,要么设置z顺序,使我新添加的叠加可以位于顶部。

出于某种原因,PdfStamper选择将新图像放置在现有图像的下方。

提前谢谢。

问题回答

如果我们能看到有问题的PDF,那将有所帮助。那么我们就不必猜测了,我们会知道的。

尽管如此,我怀疑您的“现有图像覆盖”是注释的一部分。您放入页面内容中的任何内容都不会出现在注释上方。

选项(如果我是对的):

Add your own annotation

为此,我将使用一个带有LAYOUT_ICON_ONLY的PushbuttonField。将您的图像绘制到PdfTemplate中,并将其用于按钮的“图标”。

注释的Z顺序由页面的注释数组的顺序决定。新的注释将附加到此数组。没问题。

PushbuttonField fld = new PushbuttonField(stamper.getWriter(), box, name);
fld.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
fld.setImage(myImage);

stamper.addAnnotation(fld.getField(), 1);

您可能需要使用setScaleIcon()、setHorizontalAdjustment()、setVerticalAdjustment)、setProportionalIcon(。

Flatten in one pass, add your image in another

如果现有的图像注释是iText可以压平的(也许,也许不是),那么您可以在两个过程中完成您想要的操作。第一次传递将简单地“setFormFlatting(true);close();”,而第二次传递将是您现在正在做的一切。

ByteArrayOutputStream output = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper( firstReader, output );
stamper.setFormFlattening(true);
stamper.setFreeTextFlatten(true);  // probably not needed.
stamper.close();

PdfReader secondReader = new PdfReader(output.toByteArray());
FileOutputStream finalOutput = new FileOutputStream( outputPath );
stamper = new PdfStamper(secondReader, finalOutput);
// do your thing here.
stamper.getOverContent(1).addImage(image);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签