Use C # code to generate a simple PDF File

Source: Internet
Author: User
Tags generator generator
Using System;
Using System. IO;
Using System. Text;
Using System. Collections;

Namespace generator Generator
{
/// <Summary>
/// Application: Generation of PDF file from text
/// Author: Pramod Kumar Singh
/// Date: 25th July 2001
/// </Summary>

Public class extends Generator
{
Static float pageWidth = 594.0f;
Static float pageDepth = 828366f;
Static float pageMargin = 30366f;
Static float fontSize = 10.0f;
Static float leadSize = 10.0f;

// Create a PDF file.
// PDF on Disk
Static StreamWriter pPDF = new StreamWriter ("F: \ Temp \ mydomaindetail ");
// PDF in Memory
Static MemoryStream mPDF = new MemoryStream ();

// Convert the Text Data to PDF format and write back
// Memory Stream
Static void ConvertToByteAndAddtoStream (string strMsg)
{
Byte [] buffer = null;
Buffer = ASCIIEncoding. ASCII. GetBytes (strMsg );
MPDF. Write (buffer, 0, buffer. Length );
Buffer = null;
}

// Format the data length in xRef Format
Static string xRefFormatting (long xValue)
{
String strMsg = xValue. ToString ();
Int iLen = strMsg. Length;
If (iLen <10)
{
StringBuilder s = new StringBuilder ();
// String s = null;
Int I = 10-iLen;
S. Append ('0', I );
StrMsg = s. ToString () + strMsg;
}
Return strMsg;
}

// Entry Point
Static void Main (string [] args)
{
// Create a ArrayList for xRefs of PDF Document
ArrayList xRefs = new ArrayList ();
Byte [] buffer = null;
Float yPos = 0f;
Long streamStart = 0;
Long streamEnd = 0;
Long streamLen = 0;
String strPDFMessage = null;
// PDF Header Message
StrPDFMessage = "% PDF-1.1 \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );

// ID 1 For Containt
// ID 2 For Length of the Stream
// Write the Text

// 1> Start a new Page
XRefs. Add (mPDF. Length );
Str1_message = "1 0 obj \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );
Str1_message = "</Length 2 0 R> \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );
Str1_message = "stream \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );

// Get the start of the stream
StreamStart = mPDF. Length;
Str1_message = "BT \ n/F0" + fontSize + "Tf \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );
YPos = pageDepth-pageMargin;
Strreceivmessage = pageMargin + "" + yPos + "Td \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );
Strshortmessage = leadSize + "TL \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );

// Add the text data to the PDF memory stream
Strequalmessage = "(Pramod Kumar Singh) Tj \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );
Str1_message = "ET \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );
// Get the End of the stream
StreamEnd = mPDF. Length;
// Get the Length of the stream
StreamLen = streamEnd-streamStart;
Str1_message = "endstream \ nendobj \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );

// Add 2 object to xRef
XRefs. Add (mPDF. Length );
Str1_message = "2 0 obj \ n" + streamLen + "\ nendobj \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );

// Add Page to xRefs
XRefs. Add (mPDF. Length );
Str1_message = "3 0 obj \ n </Type/Page/Parent 4 0 R/Contents 1 0 R> \ nendobj \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );

// Build the Pages
XRefs. Add (mPDF. Length );
Str1_message = "4 0 obj \ n </Type/Pages/Count 1 \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );
Str1_message = "/Kids [\ n3 0 R \ n] \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );
StrPDFMessage = "/Resources </ProcSet [/PDF/Text]/Font </F0 5 0 R> \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );
Str1_message = "/MediaBox [0 0" + pageWidth + "" + pageDepth + "] \ n> \ nendobj \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );

// Add font to xRefs
XRefs. Add (mPDF. Length );
Str1_message = "5 0 obj \ n </Type/Font/Subtype/Type1/BaseFont/Courier/Encoding/WinAnsiEncoding> \ nendobj \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );

// Add the catalog to xRefs
XRefs. Add (mPDF. Length );
Str1_message = "6 0 obj \ n </Type/Catalog/Pages 4 0 R> \ nendobj \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );

// XRefs Entry
StreamStart = mPDF. Length;
Str1_message = "xref \ n0 7 \ n1_00 65535 f \ n ";
For (int I = 0; I <xRefs. Count; I ++)
{
Str1_message + = xRefFormatting (long) xRefs [I]) + "00000 n \ n ";
}
ConvertToByteAndAddtoStream (strPDFMessage );
// Trailer for the PDF
Strequalmessage = "trailer \ n <\ n/Size" + (xRefs. Count + 1) + "\ n/Root 6 0 R \ n> \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );
// XRef location entry
StrPDFMessage = "startxref \ n" + streamStart + "\ n % EOF \ n ";
ConvertToByteAndAddtoStream (strPDFMessage );
// Write the PDF from Memory Stream to File Stream
MPDF. WriteTo (pPDF. BaseStream );
// Close the Stream
MPDF. Close ();
PPDF. Close ();
}
}
}

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.