转载:Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
原文:http://blog.csdn.net/ChengKing/archive/2006/12/21/1452134.aspx
(一). 功能
(一). 功能
创建PDF文件
(二). 代码
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.IO;
11using System.Text;
12using System.Collections;
13
14public partial class _Default : System.Web.UI.Page
15{
16 static float pageWidth = 594.0f;
17 static float pageDepth = 828.0f;
18 static float pageMargin = 30.0f;
19 static float fontSize = 10.0f;
20 static float leadSize = 10.0f;
21
22 //指定要生成PDF文件的路径
23 static StreamWriter pPDF = new StreamWriter("E:\\net\\WebSites\\Create_PDF_File\\test.pdf");//指定生成的路径
24
25 static MemoryStream mPDF = new MemoryStream();
26
27 //Convert the Text Data to PDF format and write back to
28 //Memory Stream
29 static void ConvertToByteAndAddtoStream(string strMsg)
30 {
31 Byte[] buffer = null;
32 buffer = ASCIIEncoding.ASCII.GetBytes(strMsg);
33 mPDF.Write(buffer, 0, buffer.Length);
34 buffer = null;
35 }
36
37 //Format the data length in xRef Format
38 static string xRefFormatting(long xValue)
39 {
40 string strMsg = xValue.ToString();
41 int iLen = strMsg.Length;
42 if (iLen < 10)
43 {
44 StringBuilder s = new StringBuilder();
45 int i = 10 - iLen;
46 s.Append('0', i);
47 strMsg = s.ToString() + strMsg;
48 }
49 return strMsg;
50 }
51 protected void Page_Load(object sender, EventArgs e)
52 {
53
54 }
55 protected void Button1_Click(object sender, EventArgs e)
56 {
57 //Create a ArrayList for xRefs of PDF Document
58 ArrayList xRefs = new ArrayList();
59 float yPos = 0f;
60 long streamStart = 0;
61 long streamEnd = 0;
62 long streamLen = 0;
63 string strPDFMessage = null;
64 //PDF Header Message
65 strPDFMessage = "%PDF-1.1\n";
66 ConvertToByteAndAddtoStream(strPDFMessage);
67
68 //ID 1 For Containt
69 //ID 2 For Length of the Stream
70 //write the Text
71
72 //1> Start a new Page
73 xRefs.Add(mPDF.Length);
74 strPDFMessage = "1 0 obj\n";
75 ConvertToByteAndAddtoStream(strPDFMessage);
76 strPDFMessage = "<< /Length 2 0 R >>\n";
77 ConvertToByteAndAddtoStream(strPDFMessage);
78 strPDFMessage = "stream\n";
79 ConvertToByteAndAddtoStream(strPDFMessage);
80
81 //Get the start of the stream
82 streamStart = mPDF.Length;
83 strPDFMessage = "BT\n/F0 " + fontSize + " Tf\n";
84 ConvertToByteAndAddtoStream(strPDFMessage);
85 yPos = pageDepth - pageMargin;
86 strPDFMessage = pageMargin + " " + yPos + " Td\n";
87 ConvertToByteAndAddtoStream(strPDFMessage);
88 strPDFMessage = leadSize + " TL\n";
89 ConvertToByteAndAddtoStream(strPDFMessage);
90
91 //Add the text data to the PDF memory stream
92 strPDFMessage = "(Do a test)Tj\n";
93 ConvertToByteAndAddtoStream(strPDFMessage);
94 strPDFMessage = "ET\n";
95 ConvertToByteAndAddtoStream(strPDFMessage);
96 //Get the End of the stream
97 streamEnd = mPDF.Length;
98 //Get the Length of the stream
99 streamLen = streamEnd - streamStart;
100 strPDFMessage = "endstream\nendobj\n";
101 ConvertToByteAndAddtoStream(strPDFMessage);
102
103 //Add 2 object to xRef
104 xRefs.Add(mPDF.Length);
105 strPDFMessage = "2 0 obj\n" + streamLen + "\nendobj\n";
106 ConvertToByteAndAddtoStream(strPDFMessage);
107
108 //Add Page to xRefs
109 xRefs.Add(mPDF.Length);
110 strPDFMessage = "3 0 obj\n<</Type/Page/Parent 4 0 R/Contents 1 0 R>>\nendobj\n";
111 ConvertToByteAndAddtoStream(strPDFMessage);
112
113 //Build the Pages
114 xRefs.Add(mPDF.Length);
115 strPDFMessage = "4 0 obj\n<</Type /Pages /Count 1\n";
116 ConvertToByteAndAddtoStream(strPDFMessage);
117 strPDFMessage = "/Kids[\n3 0 R\n]\n";
118 ConvertToByteAndAddtoStream(strPDFMessage);
119 strPDFMessage = "/Resources<</ProcSet[/PDF/Text]/Font<</F0 5 0 R>> >>\n";
120 ConvertToByteAndAddtoStream(strPDFMessage);
121 strPDFMessage = "/MediaBox [ 0 0 " + pageWidth + " " + pageDepth + " ]\n>>\nendobj\n";
122 ConvertToByteAndAddtoStream(strPDFMessage);
123
124 //Add font to xRefs
125 xRefs.Add(mPDF.Length);
126 strPDFMessage = "5 0 obj\n<</Type/Font/Subtype/Type1/BaseFont/Courier/Encoding/WinAnsiEncoding>>\nendobj\n";
127 ConvertToByteAndAddtoStream(strPDFMessage);
128
129 //Add the catalog to xRefs
130 xRefs.Add(mPDF.Length);
131 strPDFMessage = "6 0 obj\n<</Type/Catalog/Pages 4 0 R>>\nendobj\n";
132 ConvertToByteAndAddtoStream(strPDFMessage);
133
134 //xRefs Entry
135 streamStart = mPDF.Length;
136 strPDFMessage = "xref\n0 7\n0000000000 65535 f \n";
137 for (int i = 0; i < xRefs.Count; i++)
138 {
139 strPDFMessage += xRefFormatting((long)xRefs[i]) + " 00000 n \n";
140 }
141 ConvertToByteAndAddtoStream(strPDFMessage);
142 //Trailer for the PDF
143 strPDFMessage = "trailer\n<<\n/Size " + (xRefs.Count + 1) + "\n/Root 6 0 R\n>>\n";
144 ConvertToByteAndAddtoStream(strPDFMessage);
145 //xRef location entry
146 strPDFMessage = "startxref\n" + streamStart + "\n%%EOF\n";
147 ConvertToByteAndAddtoStream(strPDFMessage);
148 //Write the PDF from Memory Stream to File Stream
149 mPDF.WriteTo(pPDF.BaseStream);
150 //Close the Stream
151 mPDF.Close();
152 pPDF.Close();
153 }
154}
155
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.IO;
11using System.Text;
12using System.Collections;
13
14public partial class _Default : System.Web.UI.Page
15{
16 static float pageWidth = 594.0f;
17 static float pageDepth = 828.0f;
18 static float pageMargin = 30.0f;
19 static float fontSize = 10.0f;
20 static float leadSize = 10.0f;
21
22 //指定要生成PDF文件的路径
23 static StreamWriter pPDF = new StreamWriter("E:\\net\\WebSites\\Create_PDF_File\\test.pdf");//指定生成的路径
24
25 static MemoryStream mPDF = new MemoryStream();
26
27 //Convert the Text Data to PDF format and write back to
28 //Memory Stream
29 static void ConvertToByteAndAddtoStream(string strMsg)
30 {
31 Byte[] buffer = null;
32 buffer = ASCIIEncoding.ASCII.GetBytes(strMsg);
33 mPDF.Write(buffer, 0, buffer.Length);
34 buffer = null;
35 }
36
37 //Format the data length in xRef Format
38 static string xRefFormatting(long xValue)
39 {
40 string strMsg = xValue.ToString();
41 int iLen = strMsg.Length;
42 if (iLen < 10)
43 {
44 StringBuilder s = new StringBuilder();
45 int i = 10 - iLen;
46 s.Append('0', i);
47 strMsg = s.ToString() + strMsg;
48 }
49 return strMsg;
50 }
51 protected void Page_Load(object sender, EventArgs e)
52 {
53
54 }
55 protected void Button1_Click(object sender, EventArgs e)
56 {
57 //Create a ArrayList for xRefs of PDF Document
58 ArrayList xRefs = new ArrayList();
59 float yPos = 0f;
60 long streamStart = 0;
61 long streamEnd = 0;
62 long streamLen = 0;
63 string strPDFMessage = null;
64 //PDF Header Message
65 strPDFMessage = "%PDF-1.1\n";
66 ConvertToByteAndAddtoStream(strPDFMessage);
67
68 //ID 1 For Containt
69 //ID 2 For Length of the Stream
70 //write the Text
71
72 //1> Start a new Page
73 xRefs.Add(mPDF.Length);
74 strPDFMessage = "1 0 obj\n";
75 ConvertToByteAndAddtoStream(strPDFMessage);
76 strPDFMessage = "<< /Length 2 0 R >>\n";
77 ConvertToByteAndAddtoStream(strPDFMessage);
78 strPDFMessage = "stream\n";
79 ConvertToByteAndAddtoStream(strPDFMessage);
80
81 //Get the start of the stream
82 streamStart = mPDF.Length;
83 strPDFMessage = "BT\n/F0 " + fontSize + " Tf\n";
84 ConvertToByteAndAddtoStream(strPDFMessage);
85 yPos = pageDepth - pageMargin;
86 strPDFMessage = pageMargin + " " + yPos + " Td\n";
87 ConvertToByteAndAddtoStream(strPDFMessage);
88 strPDFMessage = leadSize + " TL\n";
89 ConvertToByteAndAddtoStream(strPDFMessage);
90
91 //Add the text data to the PDF memory stream
92 strPDFMessage = "(Do a test)Tj\n";
93 ConvertToByteAndAddtoStream(strPDFMessage);
94 strPDFMessage = "ET\n";
95 ConvertToByteAndAddtoStream(strPDFMessage);
96 //Get the End of the stream
97 streamEnd = mPDF.Length;
98 //Get the Length of the stream
99 streamLen = streamEnd - streamStart;
100 strPDFMessage = "endstream\nendobj\n";
101 ConvertToByteAndAddtoStream(strPDFMessage);
102
103 //Add 2 object to xRef
104 xRefs.Add(mPDF.Length);
105 strPDFMessage = "2 0 obj\n" + streamLen + "\nendobj\n";
106 ConvertToByteAndAddtoStream(strPDFMessage);
107
108 //Add Page to xRefs
109 xRefs.Add(mPDF.Length);
110 strPDFMessage = "3 0 obj\n<</Type/Page/Parent 4 0 R/Contents 1 0 R>>\nendobj\n";
111 ConvertToByteAndAddtoStream(strPDFMessage);
112
113 //Build the Pages
114 xRefs.Add(mPDF.Length);
115 strPDFMessage = "4 0 obj\n<</Type /Pages /Count 1\n";
116 ConvertToByteAndAddtoStream(strPDFMessage);
117 strPDFMessage = "/Kids[\n3 0 R\n]\n";
118 ConvertToByteAndAddtoStream(strPDFMessage);
119 strPDFMessage = "/Resources<</ProcSet[/PDF/Text]/Font<</F0 5 0 R>> >>\n";
120 ConvertToByteAndAddtoStream(strPDFMessage);
121 strPDFMessage = "/MediaBox [ 0 0 " + pageWidth + " " + pageDepth + " ]\n>>\nendobj\n";
122 ConvertToByteAndAddtoStream(strPDFMessage);
123
124 //Add font to xRefs
125 xRefs.Add(mPDF.Length);
126 strPDFMessage = "5 0 obj\n<</Type/Font/Subtype/Type1/BaseFont/Courier/Encoding/WinAnsiEncoding>>\nendobj\n";
127 ConvertToByteAndAddtoStream(strPDFMessage);
128
129 //Add the catalog to xRefs
130 xRefs.Add(mPDF.Length);
131 strPDFMessage = "6 0 obj\n<</Type/Catalog/Pages 4 0 R>>\nendobj\n";
132 ConvertToByteAndAddtoStream(strPDFMessage);
133
134 //xRefs Entry
135 streamStart = mPDF.Length;
136 strPDFMessage = "xref\n0 7\n0000000000 65535 f \n";
137 for (int i = 0; i < xRefs.Count; i++)
138 {
139 strPDFMessage += xRefFormatting((long)xRefs[i]) + " 00000 n \n";
140 }
141 ConvertToByteAndAddtoStream(strPDFMessage);
142 //Trailer for the PDF
143 strPDFMessage = "trailer\n<<\n/Size " + (xRefs.Count + 1) + "\n/Root 6 0 R\n>>\n";
144 ConvertToByteAndAddtoStream(strPDFMessage);
145 //xRef location entry
146 strPDFMessage = "startxref\n" + streamStart + "\n%%EOF\n";
147 ConvertToByteAndAddtoStream(strPDFMessage);
148 //Write the PDF from Memory Stream to File Stream
149 mPDF.WriteTo(pPDF.BaseStream);
150 //Close the Stream
151 mPDF.Close();
152 pPDF.Close();
153 }
154}
155
(三). 示例代码下载
[本文核心代码为引用,方便查看用]