生成/添加图像形状到文档中

时间:2018-03-18 13:25:47

标签: c# asp.net model-view-controller itext pdf-generation

我创建了一个PDF并在pdf中添加了Textbox Form Field并给出了名称,例如“#name#,稍后在Controller中我将值放入。

我的代码

    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);

  }

我不需要附加文本框字段,而是需要将桌面上的图片/图像添加到文档中,假设在文档的特定位置添加Photo。

0 个答案:

没有答案