Guest User

Untitled

a guest
Mar 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. public FileResult Test(EventArgs e)
  2. {
  3. string TemplateLoc = "templates\test.pdf";)
  4.  
  5. var stream = new MemoryStream();
  6. string sourceFile = Path.Combine(path);
  7. string destinationFile = Path.Combine(_hostingEnvironment.WebRootPath, "templates\temp" + ".pdf");
  8. System.IO.File.Copy(sourceFile, destinationFile);
  9.  
  10. Dictionary<string, string> keyValues = new Dictionary<string, string>();
  11.  
  12. keyValues.Add("#name#", "test");
  13.  
  14.  
  15. using (var existingFileStream = new FileStream(sourceFile, FileMode.Open))
  16. using (var newFileStream = new FileStream(destinationFile, FileMode.Create))
  17. {
  18.  
  19. var pdfReader = new PdfReader(existingFileStream);
  20.  
  21.  
  22. var stamper = new PdfStamper(pdfReader, newFileStream);
  23.  
  24. var form = stamper.AcroFields;
  25. var fieldKeys = form.Fields.Keys;
  26.  
  27. foreach (string fieldKey in fieldKeys)
  28. {
  29. foreach (KeyValuePair<string, string> i in keyValues)
  30. {
  31. if (fieldKey == i.Key)
  32. {
  33. form.SetField(fieldKey, i.Value);
  34. }
  35. }
  36. }
  37.  
  38.  
  39. stamper.FormFlattening = true;
  40.  
  41. stamper.Close();
  42. pdfReader.Close();
  43. }
  44.  
  45. byte[] fileBytes = System.IO.File.ReadAllBytes(destinationFile);
  46.  
  47. System.IO.File.Delete(destinationFile);
  48. string fileName = "test" + ".pdf";
  49.  
  50. return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
  51.  
  52. }
Add Comment
Please, Sign In to add comment