winform 把視窗上的全部資訊生成PDF檔案顯示
using System;
using System.IO;
using System.Text;
using System.Collections;
namespace PDFGenerator
{
//生成pdf的類
public class PDFGenerator
{
//初始化,定義pdf檔案大小
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;
//在磁碟指定位置建立一個PDF檔案
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 iLen=strMsg.Length;
if (iLen<10) { StringBuilder s=new StringBuilder();
//StringBuilder是修改字串的類
int i=10-iLen;
s.Append('0',i);
strMsg=s.ToString() + strMsg;
}
return strMsg; }
//入口主函式
static void Main(string[] args)
{
//為pdf檔案的外部格式建立一個佇列列表
ArrayList xRefs=new ArrayList();
float yPos =0f;
long streamStart=0;
long streamEnd=0;
long streamLen =0;
string strPDFMessage=null;
//PDF檔案的頭資訊 strPDFMessage="%PDF-1.1\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//將頭資訊轉換成位元組並加入檔案緩衝區
//開始新的一頁
xRefs.Add(mPDF.Length); strPDFMessage="1 0 obj\n";
//ID 1 For Containt
ConvertToByteAndAddtoStream(strPDFMessage);
//write the Text
strPDFMessage="<< /Length 2 0 R >>\n";
//ID 2 For Length of the Stream
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage="stream\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//得到緩衝區的開始
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);
//輸入文字資訊到檔案緩衝區
strPDFMessage= "(It is a new test!)Tj\n" ;
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage= "ET\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//得到緩衝區的結尾
streamEnd=mPDF.Length;
//得到緩衝區的長度
streamLen=streamEnd-streamStart;
strPDFMessage= "endstream\nendobj\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//將第二個目標加入外部參照格式
xRefs.Add(mPDF.Length);
strPDFMessage="2 0 obj\n"+ streamLen + "\nendobj\n";
ConvertToByteAndAddtoStream(strPDFMessage);
ConvertToByteAndAddtoStream(strPDFMessage);
//將頁資訊加入到外部參照模式
xRefs.Add(mPDF.Length);
strPDFMessage="3 0 obj\n<</Type/Page/Parent 4 0 R/Contents 1 0 R>>\nendobj\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//建立這個頁
xRefs.Add(mPDF.Length);
strPDFMessage="4 0 obj\n<</Type /Pages /Count 1\n";
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage="/Kids[\n3 0 R\n]\n";
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage="/Resources<</ProcSet[/PDF/Text]/Font<</F0 5 0 R>> >>\n";
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage="/MediaBox [ 0 0 "+ pageWidth + " " + pageDepth + "]\n>>\nendobj\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//將字型資訊加入到外部參照模式
xRefs.Add(mPDF.Length);
strPDFMessage="5 0 obj\n<</Type/Font/Subtype/Type1/BaseFont/Courier/Encoding/WinAnsiEncoding>>\nendobj\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//將目錄資訊加入到外部參照模式
xRefs.Add(mPDF.Length);
strPDFMessage="6 0 obj\n<</Type/Catalog/Pages 4 0 R>>\nendobj\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//外部參照模式入口
streamStart=mPDF.Length;
strPDFMessage="xref\n0 7\n0000000000 65535 f \n";
for(int i=0;i<xRefs.Count;i++)
{
strPDFMessage+=xRefFormatting((long) xRefs[i])+" 00000 n \n";
}
ConvertToByteAndAddtoStream(strPDFMessage);
//PDF位置資訊
strPDFMessage="trailer\n<<\n/Size "+ (xRefs.Count+1)+"\n/Root 6 0 R\n>>\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//外部參照模式本地入口
strPDFMessage="startxref\n" + streamStart+"\n%%EOF\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//將檔案緩衝區的內容寫入到檔案
mPDF.WriteTo(pPDF.BaseStream);
//關閉緩衝區
mPDF.Close();
pPDF.Close();
}
}
}
這是直接生成1個PDF檔案的小程式希望對你有幫助