Generate PDF documents (original code) with C #

zhaozj2021-02-12  136

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Using system.io;

Using system.text;

Namespace PdfGenerator

{

///

/// Form1 summary description.

///

public

Class Form1: System.Windows.Forms.form

{

///

/// The required designer variable.

///

Private system.componentmodel.container Components

=

NULL;

Public Form1 ()

{

//

// Windows Form Designer Support

// InitializationComponent ();

//

// Todo: Add any constructor code after INITIALIZECOMPONENT call

//}

///

/// Clean all the resources being used.

///

protected override

Void Dispose

Bool disposing)

{

IF (Disposing)

{

IF (Components!

NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private

void initializecomponent ()

{

this.components

=

New system.componentmodel.container ();

this.size

=

New system.drawing.size (300, 300);

this.Text

= "Form1";

}

#ndregion

Static

Float PageWidth

= 594.0F;

Static

Float PageDepth

= 828.0f;

Static

Float Page Margin

= 30.0F;

Static

Float Fontsize

= 20.0F;

Static

Float Leadsize

= 10.0F;

Static streamWriter PPDF =

New streamwriter ("e: //mypdf.pdf");

Static memoryStream MPDF =

New memorystream ();

Static

Void ConvertTobyteandAddtostream

String strmsg)

{

Byte [] buffer =

NULL;

Buffer = asciiencoding.ascii.getbytes (strmsg);

Mpdf.write (buffer, 0, buffer.length);

Buffer =

NULL;

}

Static

String Xrefformatting (long xvalue)

{

String strmsg = xvalue.toString (); int = strmsg.length;

ILEN <10)

{

Stringbuilder s =

NEW STRINGBUILDER ();

INT i = 10-Ilen;

S.Append ('0', i);

strmsg = S.toString ()

STRMSG;

}

Return strmsg;

}

Static

Void main

String [] ARGS)

{

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";

ConvertTobyTeandAddTREAM (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 high YPOS

= PageDepth

- Page Margin

StrpdfMessage = Page Margin

""

YPOS "TD / N";

ConvertTobyTeandAddTREAM (STRPDFMESSAGE);

StrpdfMessage = LeadSize "TL / N";

ConvertTobyTeandAddTREAM (STRPDFMESSAGE);

// Entity content strpdfMessage = "(http:

//www.wenhui.org )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 << / type / font / subtype / type1 / basefont / courting >> / 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 ();

}

}

// form}

转载请注明原文地址:https://www.9cbs.com/read-6987.html

New Post(0)