ASP.net 2.0 create pdf file with C # [reference] (sample code download)

Source: Internet
Author: User
Tags count tostring
Asp.net| Create | sample | download

(i). function
Create PDF file
(ii). Code
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{
static float pagewidth = 594.0f;
static float pagedepth = 828.0f;
static float pagemargin = 30.0f;
static float fontsize = 10.0f;
The static float leadsize = 10.0f;
21st
22//Specify the path to generate PDF file
StreamWriter static ppdf = new StreamWriter ("E:\\net\\websites\\create_pdf_file\\test.pdf");//Specify the path of the build
24
static MemoryStream mpdf = new MemoryStream ();
26
//convert the Text Data to PDF format and write back to
//memory Stream
static void Converttobyteandaddtostream (String strMsg)
30 {
byte[] buffer = NULL;
Buffer = ASCIIEncoding.ASCII.GetBytes (STRMSG);
Mpdf.write (buffer, 0, buffer. Length);
buffer = NULL;
35}
36
Notoginseng//format The data length in XRef Format
static string xrefformatting (Long Xvalue)
39 {
String strMsg = Xvalue.tostring ();
(a) int ilen = Strmsg.length;
if (Ilen < 10)
43 {
StringBuilder s = new StringBuilder ();
the int i = 10-ilen;
S.append (' 0 ', i);
STRMSG = s.tostring () + strMsg;
48}
return STRMSG;
50}
Wuyi protected void Page_Load (object sender, EventArgs e)
52 {
53
54}
protected void Button1_Click (object sender, EventArgs e)
56 {
//create a ArrayList for xrefs of PDF Document
ArrayList xrefs = new ArrayList ();
YPos float = 0f;
Long Streamstart = 0;
Long streamend = 0;
Long Streamlen = 0;
A string strpdfmessage = null;
//pdf Header Message
Strpdfmessage = "%pdf-1.1\n";
Converttobyteandaddtostream (Strpdfmessage);
67
//id 1 for Containt
//id 2 for Length of the Stream
//write the Text
71
//1> Start a new Page
Xrefs.add (mpdf.length);
Strpdfmessage = "1 0 obj\n";
Converttobyteandaddtostream (Strpdfmessage);
Strpdfmessage = "<</length 2 0 R >>\n";
Converttobyteandaddtostream (Strpdfmessage);
Strpdfmessage = "stream\n";
Converttobyteandaddtostream (Strpdfmessage);
80
Bayi//get the start of the stream
Streamstart = Mpdf.length;
Strpdfmessage = "Bt\n/f0" + fontsize + "tf\n";
Converttobyteandaddtostream (Strpdfmessage);
YPos = Pagedepth-pagemargin;
Strpdfmessage = Pagemargin + "" + YPos + "td\n";
Converttobyteandaddtostream (Strpdfmessage);
Strpdfmessage = leadsize + "tl\n";
Converttobyteandaddtostream (Strpdfmessage);
90
//add the text data to the PDF memory stream
Strpdfmessage = "(Do a Test) tj\n";
Converttobyteandaddtostream (Strpdfmessage);
Strpdfmessage = "et\n";
Converttobyteandaddtostream (Strpdfmessage);
//get the end of the stream
Streamend = Mpdf.length;
//get the Length of the stream
Streamlen = Streamend-streamstart;
Strpdfmessage = "endstream\nendobj\n";
Converttobyteandaddtostream (Strpdfmessage);
102
//add 2 object to XRef
Xrefs.add (mpdf.length);
The Strpdfmessage = "2 0 obj\n" + streamlen + "\nendobj\n";
Converttobyteandaddtostream (Strpdfmessage);
107
108//add Page to Xrefs
109 Xrefs.add (mpdf.length);
Strpdfmessage = "3 0 obj\n<</type/page/parent 4 0 r/contents 1 0 r>>\nendobj\n";
Converttobyteandaddtostream (Strpdfmessage);
112
113//build The Pages
114 Xrefs.add (Mpdf.length);
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";
Converttobyteandaddtostream (Strpdfmessage);
121 Strpdfmessage = "/mediabox [0 0" + pagewidth + "" + pagedepth + "]\n>>\nendobj\n";
122 Converttobyteandaddtostream (Strpdfmessage);
123
124//add Font to Xrefs
Xrefs.add (mpdf.length);
126 Strpdfmessage = "5 0 obj\n<</type/font/subtype/type1/basefont/courier/encoding/winansiencoding>>\ne Ndobj\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";
140}
Converttobyteandaddtostream (Strpdfmessage);
MB//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);
//close the Stream
151 Mpdf.close ();
152 Ppdf.close ();
153}
154}
155
(iii). Sample code Download

Http://www.cnblogs.com/Files/ChengKing/Create_PDF_File.rar



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.