C# 为什么';当我使用iTextSharp将图像添加到文档中时,是否会显示图像?
Contextt:我正在打开一个包含AcroForm字段的现有交互式PDF表单。我尝试向PDF表单中的矩形字段添加图像,如下所示:C# 为什么';当我使用iTextSharp将图像添加到文档中时,是否会显示图像?,c#,itextsharp,asp.net-2.0,C#,Itextsharp,Asp.net 2.0,Contextt:我正在打开一个包含AcroForm字段的现有交互式PDF表单。我尝试向PDF表单中的矩形字段添加图像,如下所示: string path = HttpContext.Current.Server.MapPath("includes"); string newFile = HttpContext.Current.Server.MapPath("Tmp") + "/completed_gray" +".pdf"; string imagepath = HttpContext.Cur
string path = HttpContext.Current.Server.MapPath("includes");
string newFile = HttpContext.Current.Server.MapPath("Tmp") + "/completed_gray" +".pdf";
string imagepath = HttpContext.Current.Server.MapPath("Tmp");
Document doc = new Document();
try {
PdfWriter.GetInstance(doc, new FileStream(newFile, FileMode.Open));
doc.Open();
iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(imagepath + "/CUstomRep_Eng_Col_1_V1.png");
iTextSharp.text.Rectangle rect = pdfStamper.AcroFields.GetFieldPositions("img_1_space")[0].position;
gif.ScaleAbsolute(rect.Width, rect.Height);
gif.SetAbsolutePosition(rect.Left, rect.Bottom);
doc.Add(gif);
}
catch (Exception ex) {
//Log error;
}
finally {
doc.Close();
}
图像不会显示在生成的PDF中。您正在使用“创建PDF文档的5个步骤”创建文档,如我的书中所述
newdocument()时代码>,您正在创建一个文档,其中左下角为x=0,y=0,右上角为x=595,y=842
在这种情况下,您正在将图像添加到文档中,但它不可见,因为您已将其添加到定义页面的矩形之外。请阅读一些格式问题以及我提出的问题-您是否看到错误?错误消息是什么?此外:您说您正在填写现有的PDF,并且正在使用文档和PdfWriter类?很明显,您没有阅读文档,否则,您将使用PdfStamper!是什么让你认为rect位于A4纸上的某个地方?也许原始文档有完全不同的维度?我投票结束这个问题;这不是一个真正的问题。@bruno,请给我一些我可以尝试的示例代码。PDF中有一个AcroForm字段(img_1_空格)。我想将wmf文件(左上1,1)右下228669添加到此字段。该字段具有相同的坐标。非常感谢,正如您所提到的,我尝试使用以下代码pdfReader pdfReader=new pdfReader(pdfTemplate);PdfStamper PdfStamper=newpdfstamper(pdfReader,newfilestream(newFile,FileMode.Create));AcroFields pdfFormFields=pdfStamper.AcroFields;iTextSharp.text.Image img=iTextSharp.text.Image.GetInstance(pathh+“/ADE_RepBtn_Nor.png”);iTextSharp.text.Rectangle rect=pdfStamper.AcroFields.GetFieldPositions(“img_1_space”)[0]。位置//按可缩放溶质(矩形宽度、矩形高度)的比例缩放//将其放置在img.SetAbsolutePosition(正左、正下);////将其添加到文档pdfStamper.GetOverContent(1.AddImage(img))的第1页;压模关闭();