PDFSign.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Netca_PDFSign_COM;
  6. using System.IO;
  7. using System.Drawing;
  8. using Spire.Pdf;
  9. using System.Text;
  10. using Spire.Pdf.Graphics;
  11. namespace EmisTerminal
  12. {
  13. public class PDFSign
  14. {
  15. public const string PDFPath = "~/Content/PrintFile";
  16. public static string PDFAbsolutePath
  17. {
  18. get
  19. {
  20. return HttpContext.Current.Server.MapPath(PDFPath);
  21. }
  22. }
  23. static PDFSign()
  24. {
  25. if (!Directory.Exists(PDFAbsolutePath))
  26. {
  27. Directory.CreateDirectory(PDFAbsolutePath);
  28. }
  29. }
  30. private byte[] GetImage(string certEncoded)
  31. {
  32. if (!string.IsNullOrEmpty(certEncoded))
  33. {
  34. IUtilTool util = new UtilTool();
  35. byte[] imgBytes = util.GetImageFromDevicByCert(certEncoded);
  36. if (imgBytes == null)
  37. {
  38. throw new Exception("找不到可用的签章图片!");
  39. }
  40. return imgBytes;
  41. }
  42. else
  43. {
  44. throw new Exception("找不到可用的外观,请确定已连接设备!");
  45. }
  46. }
  47. public string Sign(string targetFileName, Stream pageStream)
  48. {
  49. var pageBytes = new byte[pageStream.Length];
  50. var monthString = DateTime.Today.ToString("yyyy-MM");
  51. var monthPath = PDFAbsolutePath + @"\" + monthString;
  52. if (!Directory.Exists(monthPath))
  53. {
  54. Directory.CreateDirectory(monthPath);
  55. }
  56. var destFileName = "";
  57. var printTimes = Directory.GetFiles(monthPath).Where(x => x.Contains(targetFileName)).ToArray();
  58. if (printTimes.Length > 0)
  59. {
  60. destFileName = targetFileName + "(" + (printTimes.Length).ToString() + ").pdf";
  61. }
  62. else
  63. {
  64. destFileName = targetFileName + ".pdf";
  65. }
  66. var destFilePath = monthPath + @"\" + destFileName;
  67. pageStream.Read(pageBytes, 0, pageBytes.Length);
  68. ISignatureCreator creator = new SignatureCreator();
  69. var certEncoded = creator.SelectCert(null, 0);
  70. creator.SetSignPDFBytes(pageBytes, null);
  71. var pageCount = creator.GetPagesCount();
  72. var imgBytes = this.GetImage(certEncoded);
  73. MemoryStream ms = new MemoryStream(imgBytes);
  74. Image image = Image.FromStream(ms);
  75. PdfDocument pdf = new PdfDocument(pageStream);
  76. creator.SetSignCert(certEncoded, "sha-256", Const.NATCA_PASSWORD);
  77. for (int i = 1; i <= pageCount; i++)
  78. {
  79. try
  80. {
  81. var position = pdf.Pages[i - 1].FindAllText().Finds.OrderByDescending(x => x.Bounds.Bottom).First().Bounds;
  82. creator.SealPosition(destFilePath, i, Convert.ToInt32(Math.Floor(position.X - 20)), Convert.ToInt32(Math.Floor(position.Y - 99)), 119, 119, null, Convert.ToBase64String(imgBytes));
  83. }
  84. catch (Exception)
  85. {
  86. }
  87. }
  88. var destPath = PDFPath + "/" + monthString + "/" + destFileName;
  89. return destPath;
  90. }
  91. }
  92. }