|
技术资料 > .Net专区 > C#语言 : 用C#代码生成一个简单的PDF文件 |
用C#代码生成一个简单的PDF文件 March 25,2004 |
using System;
using System.IO;
using System.Text;
using System.Collections;
namespace PDFGenerator
{
/// <summary>
/// Application : Generation of PDF file from text
/// Author : Pramod Kumar Singh
/// Date : 25th July 2001
///</summary>
public class PDFGenerator
{
static float pageWidth = 594.0f;
static float pageDepth = 828.0f;
static float pageMargin = 30.0f;
static float fontSize = 10.0f;
static float leadSize = 10.0f;
//Create a PDF file.
//PDF on Disk
static StreamWriter pPDF=new StreamWriter("F:\Temp\myPDF.pdf");
//PDF in Memory
static MemoryStream mPDF= new MemoryStream();
//Convert the Text Data to PDF format and write back to
//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.1n";
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);
strPDFMessage="1 0 objn";
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage="<< /Length 2 0 R >>n";
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage="streamn";
ConvertToByteAndAddtoStream(strPDFMessage);
//Get the start of the stream
streamStart=mPDF.Length;
strPDFMessage="BTn/F0 " + fontSize +" Tfn";
ConvertToByteAndAddtoStream(strPDFMessage);
yPos = pageDepth - pageMargin;
strPDFMessage=pageMargin + " " + yPos +" Tdn" ;
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage= leadSize+" TLn" ;
ConvertToByteAndAddtoStream(strPDFMessage);
//Add the text data to the PDF memory stream
strPDFMessage= "(Pramod Kumar Singh)Tjn" ;
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage= "ETn";
ConvertToByteAndAddtoStream(strPDFMessage);
//Get the End of the stream
streamEnd=mPDF.Length;
//Get the Length of the stream
streamLen=streamEnd-streamStart;
strPDFMessage= "endstreamnendobjn";
ConvertToByteAndAddtoStream(strPDFMessage);
//Add 2 object to xRef
xRefs.Add(mPDF.Length);
strPDFMessage="2 0 objn"+ streamLen + "nendobjn";
ConvertToByteAndAddtoStream(strPDFMessage);
//Add Page to xRefs
xRefs.Add(mPDF.Length);
strPDFMessage="3 0 objn<</Type/Page/Parent 4 0 R/Contents 1 0 R>>nendobjn";
ConvertToByteAndAddtoStream(strPDFMessage);
//Build the Pages
xRefs.Add(mPDF.Length);
strPDFMessage="4 0 objn<</Type /Pages /Count 1n";
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage="/Kids[n3 0 Rn]n";
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage="/Resources<</ProcSet[/PDF/Text]/Font<</F0 5 0 R>> >>n";
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage="/MediaBox [ 0 0 "+ pageWidth + " " + pageDepth + " ]n>>nendobjn";
ConvertToByteAndAddtoStream(strPDFMessage);
//Add font to xRefs
xRefs.Add(mPDF.Length);
strPDFMessage="5 0 objn<</Type/Font/Subtype/Type1/BaseFont/Courier/Encoding/WinAnsiEncoding>>nendobjn";
ConvertToByteAndAddtoStream(strPDFMessage);
//Add the catalog to xRefs
xRefs.Add(mPDF.Length);
strPDFMessage="6 0 objn<</Type/Catalog/Pages 4 0 R>>nendobjn";
ConvertToByteAndAddtoStream(strPDFMessage);
//xRefs Entry
streamStart=mPDF.Length;
strPDFMessage="xrefn0 7n0000000000 65535 f n";
for(int i=0;i<xRefs.Count;i++)
{
strPDFMessage+=xRefFormatting((long) xRefs)+" 00000 n n";
}
ConvertToByteAndAddtoStream(strPDFMessage);
//Trailer for the PDF
strPDFMessage="trailern<<n/Size "+ (xRefs.Count+1)+"n/Root 6 0 Rn>>n";
ConvertToByteAndAddtoStream(strPDFMessage);
//xRef location entry
strPDFMessage="startxrefn" + streamStart+"n%%EOFn";
ConvertToByteAndAddtoStream(strPDFMessage);
//Write the PDF from Memory Stream to File Stream
mPDF.WriteTo(pPDF.BaseStream);
//Close the Stream
mPDF.Close();
pPDF.Close();
}
}
}
|
|
Copyright © 2001-2008 Shenzhen Hiblue Software Team All rights reserved