试图在c#中将图像插入pdf [英] Trying to insert an image into a pdf‏ in c#

查看:124
本文介绍了试图在c#中将图像插入pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据生成的条形码文件插入图像。

I need to insert an image based on a generated barcode file.

我遇到的问题是使用iTextSharp库我通常可以填写文本,例如

The problem I'm having is when using the iTextSharp library I can normally fill in text such as

PdfReader pdfReader = new PdfReader(oldFile);
PdfStamper pdfStamper = new PdfStamper(pdfReader, outFile);
AcroFields fields = pdfStamper.AcroFields;
fields.SetField("topmostSubform[0].Page1[0].BARCODE[0]", "X974005-1");

虽然有一个字段在pdf中,如果我点击它,它会提示我插入一个图像字段,但我似乎无法以编程方式完成此操作。基于一些谷歌搜索和绊倒在stackoverflow页面,我插入以下代码,期望它按照需要工作:

though there's one field where in pdf if I click onto it it prompts me for an image to insert into field, but I can't seem to programmatically accomplish this. Based on some google searches and stumbling upon a stackoverflow page, I inserted the following code expecting it to work as desired:

string fieldName = "topmostSubform[0].Page1[0].BARCODE[0]";
string imageFile = "test-barcode.jpg";
AcroFields.FieldPosition fieldPosition = pdfStamper.AcroFields.GetFieldPositions(fieldName)[0];
PushbuttonField imageField = new PushbuttonField(pdfStamper.Writer, fieldPosition.position, fieldName);
imageField.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
imageField.Image = iTextSharp.text.Image.GetInstance(imageFile);
imageField.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
imageField.ProportionalIcon = false;
imageField.Options = BaseField.READ_ONLY;
pdfStamper.AcroFields.RemoveField(fieldName);               
pdfStamper.AddAnnotation(imageField.Field, fieldPosition.page);

我遇到的问题是,当我打开新创建的时候,它会按预期删除现有字段PDF文件我没有看到这个新的按钮字段与预期的图像文件,而是作为一个空白,但当我通过调试模式执行此操作时,我可以看到它至少拾取了图像文件的正确尺寸,所以我不喜欢我不知道我在这里做错了什么。

The problem I am having is while it removes the existing field as intended, when I open the newly created PDF file I don't see this new push button field with the intended image file but rather as a blank but when I perform this through debug mode I can see that it's at least picking up the correct dimensions of the image file, so I don't know what I'm doing wrong here.

请指教,谢谢。

推荐答案

如果你看了官方文档(即:我的书),你会发现这个例子: ReplaceIcon.cs

If you read the official documentation (that is: my book), you'll find this example: ReplaceIcon.cs

您正在使用 pdfStamper.AcroFields.RemoveField(fieldName); 删除该字段,然后尝试使用<$ c $添加新字段c> pdfStamper.AddAnnotation(imageField.Field,fieldPosition.page);

You're removing the field using pdfStamper.AcroFields.RemoveField(fieldName); and subsequently you try adding the new field using pdfStamper.AddAnnotation(imageField.Field, fieldPosition.page);

这是错的。您应该使用 pdfStamper.AcroFields.ReplacePushbuttonField(fieldname,imageField.Field)替换字段;

That's wrong. You should replace the field using pdfStamper.AcroFields.ReplacePushbuttonField(fieldname, imageField.Field);

ReplacePushbuttonField()方法在幕后复制大量设置。

The ReplacePushbuttonField() method copies plenty of settings behind the scenes.

这篇关于试图在c#中将图像插入pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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