PDFHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using iTextSharp.awt.geom;
  2. using iTextSharp.text;
  3. using iTextSharp.text.pdf;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.util;
  9. namespace XYY.Core.Standard.PDF
  10. {
  11. public static class PDFHelper
  12. {
  13. public static byte[] SetPdfSize(byte[] data, double x, double y, float width, float height, double angle = 0, int qty = 0, double scale = 1.0)
  14. {
  15. var ms = new MemoryStream();
  16. PdfReader reader = new PdfReader(data);
  17. Document doc = new Document();
  18. doc.SetPageSize(new iTextSharp.text.Rectangle(width, height));
  19. PdfWriter writer = PdfWriter.GetInstance(doc, ms);
  20. doc.Open();
  21. PdfContentByte cb = writer.DirectContent;
  22. AffineTransform transform = new AffineTransform();
  23. transform.Translate(x, y);
  24. transform.Scale(scale, scale);
  25. if (angle > 0)
  26. transform.SetToRotation(angle);
  27. if (qty == 0)
  28. qty = reader.NumberOfPages;
  29. for (int i = 1; i <= reader.NumberOfPages; i++)
  30. {
  31. doc.NewPage();
  32. PdfImportedPage page = writer.GetImportedPage(reader, i);
  33. cb.AddTemplate(page, transform);
  34. }
  35. doc.Close();
  36. return ms.ToArray();
  37. }
  38. public static byte[] CutOutPackage(byte[] data, int index)
  39. {
  40. var ms = new MemoryStream();
  41. PdfReader reader = new PdfReader(data);
  42. Document doc = new Document(reader.GetPageSizeWithRotation(index));
  43. doc.NewPage();
  44. PdfWriter writer = PdfWriter.GetInstance(doc, ms);
  45. doc.Open();
  46. PdfContentByte cb = writer.DirectContent;
  47. AffineTransform transform = new AffineTransform();
  48. for (int i = index; i <= index; i++)
  49. {
  50. PdfImportedPage page = writer.GetImportedPage(reader, i);
  51. var rotation = reader.GetPageRotation(index);
  52. if (rotation == 90)
  53. {
  54. cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(index).Height);
  55. }
  56. else if (rotation == 180)
  57. {
  58. cb.AddTemplate(page, -1f, 0, 0, -1f, reader.GetPageSizeWithRotation(index).Width, reader.GetPageSizeWithRotation(index).Height);
  59. }
  60. else if (rotation == 270)
  61. {
  62. cb.AddTemplate(page, 0, 1f, -1f, 0, reader.GetPageSizeWithRotation(index).Width, 0);
  63. }
  64. else
  65. {
  66. cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
  67. }
  68. }
  69. doc.Close();
  70. return ms.ToArray();
  71. }
  72. /// <summary>
  73. /// PDF合并方法
  74. /// </summary>
  75. /// <param name="outMergeFile">输出文件路径</param>
  76. /// <param name="lstFile">需要合并的PDF文件</param>
  77. /// <param name="isStretch">是否缩放文件尺寸到第一页</param>
  78. public static byte[] SetPageSize(byte[] data, int width, int height, int rotation)
  79. {
  80. Document document = new Document();
  81. using (var ms = new System.IO.MemoryStream())
  82. {
  83. PdfWriter writer = PdfWriter.GetInstance(document, ms);
  84. document.Open();
  85. PdfContentByte cb = writer.DirectContent;
  86. PdfImportedPage newPage;
  87. var reader = new PdfReader(data);
  88. int iPageNum = reader.NumberOfPages;
  89. int startPage = 1;
  90. while (startPage <= iPageNum)
  91. {
  92. Rectangle rectrangle = null;
  93. rectrangle = new Rectangle(width, height);
  94. if (rotation == 90)
  95. {
  96. rectrangle = new Rectangle(height, width);
  97. }
  98. document.SetPageSize(rectrangle);
  99. document.NewPage();
  100. newPage = writer.GetImportedPage(reader, startPage);
  101. if (rotation == 90)
  102. {
  103. cb.AddTemplate(newPage, 0, -1f, 1f, 0, 0, width);
  104. }
  105. else if (rotation == 180)
  106. {
  107. cb.AddTemplate(newPage, -1f, 0, 0, -1f, reader.GetPageSizeWithRotation(startPage).Width, reader.GetPageSizeWithRotation(startPage).Height);
  108. }
  109. else if (rotation == 270)
  110. {
  111. cb.AddTemplate(newPage, 0, 1f, -1f, 0, reader.GetPageSizeWithRotation(startPage).Width, 0);
  112. }
  113. else
  114. {
  115. cb.AddTemplate(newPage, 1f, 0, 0, 1f, 0, 0);
  116. }
  117. startPage++;
  118. }
  119. document.Close();
  120. return ms.ToArray();
  121. }
  122. }
  123. public static void PDFFileRepeat(string outMergeFile, byte[] file)
  124. {
  125. if (!string.IsNullOrEmpty(outMergeFile))
  126. {
  127. try
  128. {
  129. if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(outMergeFile)))
  130. {
  131. System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(outMergeFile));
  132. }
  133. var document = new Document(new PdfReader(new MemoryStream(file)).GetPageSizeWithRotation(1));
  134. PdfCopy copy = new PdfCopy(document, new System.IO.FileStream(outMergeFile, FileMode.Create));
  135. document.Open();
  136. PdfReader reader = new PdfReader(file);
  137. int n = reader.NumberOfPages;// 获得总页码
  138. for (int j = 1; j <= 2; j++)
  139. {
  140. document.NewPage();
  141. PdfImportedPage page = copy.GetImportedPage(reader, 1);// 从当前Pdf,获取第j页
  142. copy.AddPage(page);
  143. }
  144. document.Close();
  145. }
  146. catch (Exception ex)
  147. {
  148. }
  149. }
  150. }
  151. public static void MergePDFFiles(string outMergeFile, List<byte[]> files, bool isStretch = true, int qty = 0)
  152. {
  153. if (!string.IsNullOrEmpty(outMergeFile))
  154. {
  155. try
  156. {
  157. if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(outMergeFile)))
  158. {
  159. System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(outMergeFile));
  160. }
  161. var document = new Document(new PdfReader(new MemoryStream(files[0])).GetPageSizeWithRotation(1));
  162. PdfCopy copy = new PdfCopy(document, new System.IO.FileStream(outMergeFile, FileMode.Create));
  163. document.Open();
  164. for (int i = 0; i < files.Count; i++)
  165. {
  166. PdfReader reader = new PdfReader(files[i]);
  167. int n = qty == 0 ? reader.NumberOfPages : qty;// 获得总页码
  168. for (int j = 1; j <= n; j++)
  169. {
  170. document.NewPage();
  171. PdfImportedPage page = copy.GetImportedPage(reader, j);// 从当前Pdf,获取第j页
  172. copy.AddPage(page);
  173. }
  174. }
  175. document.Close();
  176. }
  177. catch (Exception ex)
  178. {
  179. }
  180. }
  181. }
  182. public class InvoiceOrder
  183. {
  184. public string senderPhone
  185. {
  186. get; set;
  187. }
  188. public string senderCountry
  189. {
  190. get; set;
  191. }
  192. public string senderState
  193. {
  194. get; set;
  195. }
  196. public string senderCity
  197. {
  198. get; set;
  199. }
  200. public string senderPostCode
  201. {
  202. get; set;
  203. }
  204. public string senderName
  205. {
  206. get; set;
  207. }
  208. public string senderCompany
  209. {
  210. get; set;
  211. }
  212. public string senderStree
  213. {
  214. get; set;
  215. }
  216. public string HSCode
  217. {
  218. get; set;
  219. }
  220. public string TackingNumber
  221. {
  222. get; set;
  223. }
  224. public string Phone
  225. {
  226. get; set;
  227. }
  228. public string Name
  229. {
  230. get; set;
  231. }
  232. public string Stree
  233. {
  234. get; set;
  235. }
  236. public string Weight
  237. {
  238. get; set;
  239. }
  240. public string Number
  241. {
  242. get; set;
  243. }
  244. public string EnName
  245. {
  246. get; set;
  247. }
  248. public string Country
  249. {
  250. get; set;
  251. }
  252. public string Qty
  253. {
  254. get; set;
  255. }
  256. public string UV
  257. {
  258. get; set;
  259. }
  260. public string SV
  261. {
  262. get; set;
  263. }
  264. public string SUV
  265. {
  266. get; set;
  267. }
  268. public string SSV
  269. {
  270. get; set;
  271. }
  272. public DateTime Time
  273. {
  274. get; set;
  275. }
  276. }
  277. public static byte[] Zoom(string pdfPath, int width, int height)
  278. {
  279. System.IO.MemoryStream ms = new MemoryStream();
  280. PdfReader reader = new PdfReader(pdfPath);
  281. Document document = new Document();
  282. document.SetPageSize(new Rectangle(new RectangleJ(0, 0, width, height)));
  283. PdfWriter writer = PdfWriter.GetInstance(document, ms);
  284. document.Open();
  285. PdfContentByte cb = writer.DirectContent;
  286. PdfImportedPage page = writer.GetImportedPage(reader, 1); //page #1
  287. float Scale = Math.Max(width / page.Width, height / page.Height);
  288. cb.AddTemplate(page, Scale, 0, 0, Scale, 0, 0);
  289. document.Close();
  290. //ms.Position = 0;
  291. return ms.ToArray();
  292. }
  293. public class FCLabelInfo
  294. {
  295. public string TransferNumber
  296. {
  297. get; set;
  298. }
  299. public string TrackngNumber
  300. {
  301. get; set;
  302. }
  303. public string TrackngNumber2
  304. {
  305. get; set;
  306. }
  307. public string Name
  308. {
  309. get; set;
  310. }
  311. public string Stree
  312. {
  313. get; set;
  314. }
  315. public string City
  316. {
  317. get; set;
  318. }
  319. public string State
  320. {
  321. get; set;
  322. }
  323. public string ReceiverZipCode
  324. {
  325. get; set;
  326. }
  327. public string Weight
  328. {
  329. get; set;
  330. }
  331. }
  332. public static byte[] Watermark(byte[] data, string text, int package, int x, int y, int fontSize, BaseColor color)
  333. {
  334. try
  335. {
  336. using (PdfReader pdfReader = new PdfReader(new System.IO.MemoryStream(data)))
  337. {
  338. using (var outputStream = new System.IO.MemoryStream())
  339. {
  340. PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);
  341. PdfContentByte content;
  342. BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  343. PdfGState gs = new PdfGState();
  344. int numberOfPackage = pdfReader.NumberOfPages;
  345. numberOfPackage = package;
  346. content = pdfStamper.GetOverContent(numberOfPackage);
  347. gs.FillOpacity = 1f;
  348. content.SetGState(gs);
  349. content.BeginText();
  350. content.SetColorFill(color);
  351. content.SetFontAndSize(font, fontSize);
  352. content.SetTextMatrix(0, 0);
  353. content.ShowTextAligned(Element.ALIGN_LEFT, text, x, y, 0);
  354. content.EndText();
  355. pdfStamper.Close();
  356. return outputStream.ToArray();
  357. }
  358. }
  359. }
  360. catch (FileNotFoundException)
  361. {
  362. throw new InvalidOperationException("Label的PDF文件不存在");
  363. }
  364. catch (Exception ex)
  365. {
  366. throw new InvalidOperationException("水印生成错误" + ex.Message);
  367. }
  368. }
  369. }
  370. }