Time to see how to generate PDF documents with C # online. It feels not bad, I have done it, and the function has been made to make a small transformation, making it a function of truly used in our usual development. It is also very convenient to drop. The specific steps are as follows: 1: Generate a PDF class. This class will provide a main function interface that requires the generation path and related content of the generation document. This class is mainly written through streamwriter! ! However, the format written by StreamWriter is done by PDF. The code of the class is as follows: USING SYSTEM;
Using system.io;
Using system.text;
Using system.collections;
Namespace PdfGenerator
{
Public Class PdfGenerator
{
Static float pagewidth = 594.0f;
Static float pageDepth = 828.0f;
Static float pagemargin = 30.0f;
Static float fontsize = 20.0f;
Static float leadsize = 10.0f;
Private static string str_path = "c: //wt.pdf";
// static streamwriter ppdf = new streamwriter (STR_PATH);
// static streamwriter PPDF;
Static MemoryStream MPDF = New MemoryStream ();
Static Void ConvertTobyteandAddTostream (String Strmsg)
{
BYTE [] Buffer = NULL;
Buffer = asciiencoding.ascii.getbytes (strmsg);
// buffer = asciiencoding.
Mpdf.write (buffer, 0, buffer.length);
// buffer = null;
}
Static string xrefformatting (long xvalue)
{
String strmsg = xvalue.tostring ();
INT Ilen = strmsg.length;
ILEN <10)
{
Stringbuilder s = new stringbuilder ();
INT i = 10-Ilen;
S.Append ('0', i);
STRMSG = S.toString () strmsg;
}
Return strmsg;
}
Public int main (string str_path_p, string str_content)
{Try {str_path = STR_PATH_P ". PDF"; streamwriter PPDF = New StreamWriter (STR_PATH); ArrayList Xrefs = New ArrayList ();
// Byte [] buffer = null;
Float ypos = 0f;
Long streamstart = 0;
Long streamend = 0;
Long streamlen = 0;
String strpdfMessage = NULL;
// PDF document header information
StrpdfMessage = "% PDF-1.1 / N";
CONVERTTOBYTEANDDTOSTREAM (STRPDFMESSAGE); XREFS.ADD (MPDF.Length);
StrpdfMessage = "1 0 obj / n";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
StrpdfMessage = "<< / length 2 0 r >> / n";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
StrpdfMessage = "stream / n";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
PDF document description
streamstart = mpdf.length;
// font
StrpdfMessage = "BT / N / F0" Fontsize "TF / N";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
// PDF document entity height
YPOS = PageDepth - Page Margin;
StrpdfMessage = Page Margin " YPOS " TD / N ";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
StrpdfMessage = LeadSize "TL / N";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
// Entity content
StrpdfMessage = "(" str_content ") tj / n";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
StrpdfMessage = "ET / N";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
streamend = mpdf.length;
Streamlen = streamend-streamstart;
StrpdfMessage = "endstream / nendobj / n";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
/ / PDF document version information
Xrefs.Add (MPDF.Length);
StrpdfMessage = "2 0 obj / n" streamlen "/ NENDOBJ / N";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
Xrefs.Add (MPDF.Length);
StrpdfMessage = "3 0 obj / n << / Type / Page / Parent 4 0 r / contents 1 0 r >> / NENDOBJ / N";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
Xrefs.Add (MPDF.Length);
StrpdfMessage = "4 0 obj / n << / type / pages / count 1 / n";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
StrpdfMessage = "/ kids [/ n3 0 r / n] / n";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
StrpdfMessage = "/ resources << / procset [/ pdf / text] / font << / f0 5 0 r >> >> / n"; ConvertTobyTeandAddTREAM (StrpdfMessage);
StrpdfMessage = "/ MediaBox [0 0" PageWidth " PageDepth "] / N >> / NENDOBJ / N ";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
Xrefs.Add (MPDF.Length);
// strpdfMessage = "5 0 obj / n << / type1 / font / subtype / type1 / basefont / courting >> / NENDOBJ / N";
StrpdfMessage = "5 0 obj / n << / type1 / font / subtype / type1 / basefont / course / encoding / gb2312 >> / NENDOBJ / N"
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
Xrefs.Add (MPDF.Length);
StrpdfMessage = "6 0 obj / n << / type / catalog / pages 4 0 r >> / NENDOBJ / N";
ConvertTobyTeandAddTREAM (STRPDFMESSAGE);
streamstart = mpdf.length;
StrpdfMessage = "XREF / N0 7 / N000000000000 65535 f / N";
For (int i = 0; i { StrpdfMessage = Xrefformatting ((long) XREFS [i]) "00000 n / n"; } ConvertTobyTeandAddTREAM (STRPDFMESSAGE); StrpdfMessage = "trailer / n << / n / size" (xrefs.count 1) "/ n / root 6 0 r / n >> / n"; ConvertTobyTeandAddTREAM (STRPDFMESSAGE); StrpdfMessage = "startxref / n" streamstart "/ n %% EOF / N"; ConvertTobyTeandAddTREAM (STRPDFMESSAGE); MPDF.Writeto (PPDF.BaseStream); MPDF.CLOSE (); PPDF.Close (); Return 1;} catCH {return 0;} } }