C# 在文档中生成/添加图像形状

C# 在文档中生成/添加图像形状,c#,asp.net,model-view-controller,itext,pdf-generation,C#,Asp.net,Model View Controller,Itext,Pdf Generation,我创建了一个PDF,并在PDF中添加了Textbox表单字段,并给出了诸如“#name#”之类的名称,稍后我会将这些值放入控制器中 我的代码 public FileResult Test(EventArgs e) { string TemplateLoc = "templates\\test.pdf";) var stream = new MemoryStream(); string sourceFile =

我创建了一个PDF,并在PDF中添加了Textbox表单字段,并给出了诸如“#name#”之类的名称,稍后我会将这些值放入控制器中

我的代码

    public FileResult Test(EventArgs e)
    {
           string TemplateLoc = "templates\\test.pdf";)

            var stream = new MemoryStream();
            string sourceFile = Path.Combine(path);
            string destinationFile =  Path.Combine(_hostingEnvironment.WebRootPath, "templates\\temp" + ".pdf");
            System.IO.File.Copy(sourceFile, destinationFile);

            Dictionary<string, string> keyValues = new Dictionary<string, string>();

            keyValues.Add("#name#", "test");


            using (var existingFileStream = new FileStream(sourceFile, FileMode.Open))
            using (var newFileStream = new FileStream(destinationFile, FileMode.Create))
            {

                var pdfReader = new PdfReader(existingFileStream);


                var stamper = new PdfStamper(pdfReader, newFileStream);

                var form = stamper.AcroFields;
                var fieldKeys = form.Fields.Keys;

                foreach (string fieldKey in fieldKeys)
                {
                    foreach (KeyValuePair<string, string> i in keyValues)
                    {
                        if (fieldKey == i.Key)
                        {
                            form.SetField(fieldKey, i.Value);
                        }
                    }
                }


                stamper.FormFlattening = true;

                stamper.Close();
                pdfReader.Close();
            }

        byte[] fileBytes = System.IO.File.ReadAllBytes(destinationFile);

        System.IO.File.Delete(destinationFile);
        string fileName = "test" + ".pdf";

        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);

  }
公共文件结果测试(EventArgs e)
{
字符串TemplateLoc=“templates\\test.pdf”;)
var stream=newmemoryStream();
字符串sourceFile=Path.Combine(路径);
字符串destinationFile=Path.Combine(_hostingEnvironment.WebRootPath,“templates\\temp”+“.pdf”);
System.IO.File.Copy(sourceFile,destinationFile);
Dictionary keyValues=新字典();
keyValues.Add(“#name#“,“test”);
使用(var existingFileStream=newfilestream(sourceFile,FileMode.Open))
使用(var newFileStream=newFileStream(destinationFile,FileMode.Create))
{
var pdfReader=新的pdfReader(现有文件流);
var stamper=新的PdfStamper(pdfReader,newFileStream);
var form=stamper.AcroFields;
var fieldKeys=form.Fields.Keys;
foreach(字段键中的字符串字段键)
{
foreach(keyValues中的KeyValuePair i)
{
如果(字段键==i.Key)
{
表单设置字段(字段键,即值);
}
}
}
stamper.FormFlatting=真;
压模关闭();
pdfReader.Close();
}
byte[]fileBytes=System.IO.File.ReadAllBytes(destinationFile);
System.IO.File.Delete(destinationFile);
字符串fileName=“test”+“.pdf”;
返回文件(fileBytes,System.Net.Mime.MediaTypeNames.Application.Octet,fileName);
}
我不需要附加文本框字段,而是需要将桌面上的图片/图像添加到文档中,比如在文档的特定位置添加照片。

这有帮助吗?