查看: 2043|回复: 0

[.NET开发] C#编程简单实现生成PDF文档的方法示例

发表于 2018-2-9 08:00:00

本文实例讲述了C#编程简单实现生成PDF文档的方法。分享给大家供大家参考,具体如下:

  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Collections;
  5. namespace PDFGenerator
  6. {
  7. public class PDFGenerator
  8. {
  9. static float pageWidth = 594.0f;
  10. static float pageDepth = 828.0f;
  11. static float pageMargin = 30.0f;
  12. static float fontSize = 20.0f;
  13. static float leadSize = 10.0f;
  14. static StreamWriter pPDF = new StreamWriter("E:\\myPDF.pdf");
  15. static MemoryStream mPDF = new MemoryStream();
  16. static void ConvertToByteAndAddtoStream(string strMsg)
  17. {
  18. Byte[] buffer = null;
  19. buffer = ASCIIEncoding.ASCII.GetBytes(strMsg);
  20. mPDF.Write(buffer, 0, buffer.Length);
  21. buffer = null;
  22. }
  23. static string xRefFormatting(long xValue)
  24. {
  25. string strMsg = xValue.ToString();
  26. int iLen = strMsg.Length;
  27. if (iLen < 10)
  28. {
  29. StringBuilder s = new StringBuilder();
  30. int i = 10 - iLen;
  31. s.Append('0', i);
  32. strMsg = s.ToString() + strMsg;
  33. }
  34. return strMsg;
  35. }
  36. static void Main(string[] args)
  37. {
  38. ArrayList xRefs = new ArrayList();
  39. //Byte[] buffer=null;
  40. float yPos = 0f;
  41. long streamStart = 0;
  42. long streamEnd = 0;
  43. long streamLen = 0;
  44. string strPDFMessage = null;
  45. //PDF文档头信息
  46. strPDFMessage = "%PDF-1.1\n";
  47. ConvertToByteAndAddtoStream(strPDFMessage);
  48. xRefs.Add(mPDF.Length);
  49. strPDFMessage = "1 0 obj\n";
  50. ConvertToByteAndAddtoStream(strPDFMessage);
  51. strPDFMessage = "<< /Length 2 0 R >>\n";
  52. ConvertToByteAndAddtoStream(strPDFMessage);
  53. strPDFMessage = "stream\n";
  54. ConvertToByteAndAddtoStream(strPDFMessage);
  55. ////////PDF文档描述
  56. streamStart = mPDF.Length;
  57. //字体
  58. strPDFMessage = "BT\n/F0 " + fontSize + " Tf\n";
  59. ConvertToByteAndAddtoStream(strPDFMessage);
  60. //PDF文档实体高度
  61. yPos = pageDepth - pageMargin;
  62. strPDFMessage = pageMargin + " " + yPos + " Td\n";
  63. ConvertToByteAndAddtoStream(strPDFMessage);
  64. strPDFMessage = leadSize + " TL\n";
  65. ConvertToByteAndAddtoStream(strPDFMessage);
  66. //实体内容
  67. strPDFMessage = "(http://www.wenhui.org)Tj\n";
  68. ConvertToByteAndAddtoStream(strPDFMessage);
  69. strPDFMessage = "ET\n";
  70. ConvertToByteAndAddtoStream(strPDFMessage);
  71. streamEnd = mPDF.Length;
  72. streamLen = streamEnd - streamStart;
  73. strPDFMessage = "endstream\nendobj\n";
  74. ConvertToByteAndAddtoStream(strPDFMessage);
  75. //PDF文档的版本信息
  76. xRefs.Add(mPDF.Length);
  77. strPDFMessage = "2 0 obj\n" + streamLen + "\nendobj\n";
  78. ConvertToByteAndAddtoStream(strPDFMessage);
  79. xRefs.Add(mPDF.Length);
  80. strPDFMessage = "3 0 obj\n<</Type/Page/Parent 4 0 R/Contents 1 0 R>>\nendobj\n";
  81. ConvertToByteAndAddtoStream(strPDFMessage);
  82. xRefs.Add(mPDF.Length);
  83. strPDFMessage = "4 0 obj\n<</Type /Pages /Count 1\n";
  84. ConvertToByteAndAddtoStream(strPDFMessage);
  85. strPDFMessage = "/Kids[\n3 0 R\n]\n";
  86. ConvertToByteAndAddtoStream(strPDFMessage);
  87. strPDFMessage = "/Resources<</PRocSet[/PDF/Text]/Font<</F0 5 0 R>> >>\n";
  88. ConvertToByteAndAddtoStream(strPDFMessage);
  89. strPDFMessage = "/MediaBox [ 0 0 " + pageWidth + " " + pageDepth + " ]\n>>\nendobj\n";
  90. ConvertToByteAndAddtoStream(strPDFMessage);
  91. xRefs.Add(mPDF.Length);
  92. strPDFMessage = "5 0 obj\n<</Type/Font/Subtype/Type1/BaseFont/Courier/Encoding/WinAnsiEncoding>>\nendobj\n";
  93. ConvertToByteAndAddtoStream(strPDFMessage);
  94. xRefs.Add(mPDF.Length);
  95. strPDFMessage = "6 0 obj\n<</Type/Catalog/Pages 4 0 R>>\nendobj\n";
  96. ConvertToByteAndAddtoStream(strPDFMessage);
  97. streamStart = mPDF.Length;
  98. strPDFMessage = "xref\n0 7\n0000000000 65535 f \n";
  99. for (int i = 0; i < xRefs.Count; i++)
  100. {
  101. strPDFMessage += xRefFormatting((long)xRefs[i]) + " 00000 n \n";
  102. }
  103. ConvertToByteAndAddtoStream(strPDFMessage);
  104. strPDFMessage = "trailer\n<<\n/Size " + (xRefs.Count + 1) + "\n/Root 6 0 R\n>>\n";
  105. ConvertToByteAndAddtoStream(strPDFMessage);
  106. strPDFMessage = "startxref\n" + streamStart + "\n%%EOF\n";
  107. ConvertToByteAndAddtoStream(strPDFMessage);
  108. mPDF.WriteTo(pPDF.BaseStream);
  109. mPDF.Close();
  110. pPDF.Close();
  111. }
  112. }
  113. }
复制代码

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#文件操作常用技巧汇总》、《C#遍历算法与技巧总结》、《C#程序设计之线程使用技巧总结》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》及《C#面向对象程序设计入门教程》

希望本文所述对大家C#程序设计有所帮助。

您可能感兴趣的文章:

  • C# 生转换网页为pdf
  • C# 中使用iTextSharp组件创建PDF的简单方法
  • C# 利用Aspose.Words.dll将 Word 转成PDF
  • c#开发word批量转pdf源码分享
  • word ppt excel文档转换成pdf的C#实现代码
  • c#实现pdf的另存为功能
  • C#将jpg转换为pdf的方法
  • C#编程读取文档Doc、Docx及Pdf内容的方法
  • C#使用iTextSharp从PDF文档获取内容的方法
  • 用C#来解析PDF文件
  • C#实现HTML转WORD及WORD转PDF的方法
  • C# WinForm打开PDF文件并在窗体中显示


回复

使用道具 举报