Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么';当我使用iTextSharp将图像添加到文档中时,是否会显示图像?_C#_Itextsharp_Asp.net 2.0 - Fatal编程技术网

C# 为什么';当我使用iTextSharp将图像添加到文档中时,是否会显示图像?

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

Contextt:我正在打开一个包含AcroForm字段的现有交互式PDF表单。我尝试向PDF表单中的矩形字段添加图像,如下所示:

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个步骤”创建文档,如我的书中所述

  • 创建文档对象
  • 创建一个PdfWriter实例
  • 打开文档
  • 向文档中添加内容
  • 关闭文档
  • 这与您实际想做的事情相矛盾:我想在AcroForm字段定义的占位符中添加一个图像

    你为什么说你想要一件事,而做另一件事?我不知道。可能是因为您不想阅读文档

    你需要这样的东西:

  • 创建PdfReader实例
  • 创建一个PdfStamper实例
  • 向压模询问有关字段的信息
  • 使用stamper实例向页面添加内容
  • 合上压模
  • 回答您的问题:为什么我的图像没有显示在我的文档中

    支持现有文档中字段的坐标为左下角x=600,y=600和右上角x=700,y=700,然后将图像添加到所创建页面的可见区域之外。使用
    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页;压模关闭();