//write by wenhui.org
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;
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();
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文档头信息
strpdfmessage="%pdf-1.1\n";
converttobyteandaddtostream(strpdfmessage);
xrefs.add(mpdf.length);
strpdfmessage="1 0 obj\n";
converttobyteandaddtostream(strpdfmessage);
strpdfmessage="<< /length 2 0 r >>\n";
converttobyteandaddtostream(strpdfmessage);
strpdfmessage="stream\n";
converttobyteandaddtostream(strpdfmessage);
////////pdf文档描述
streamstart=mpdf.length;
//字体
strpdfmessage="bt\n/f0 " + fontsize +" tf\n";
converttobyteandaddtostream(strpdfmessage);
//pdf文档实体高度
ypos = pagedepth – pagemargin;
strpdfmessage=pagemargin + " " + ypos +" td\n" ;
converttobyteandaddtostream(strpdfmessage);
strpdfmessage= leadsize+" tl\n" ;
converttobyteandaddtostream(strpdfmessage);
//实体内容
strpdfmessage= "(http://www.wenhui.org)tj\n" ;
converttobyteandaddtostream(strpdfmessage);
strpdfmessage= "et\n";
converttobyteandaddtostream(strpdfmessage);
streamend=mpdf.length;
streamlen=streamend-streamstart;
strpdfmessage= "endstream\nendobj\n";
converttobyteandaddtostream(strpdfmessage);
//pdf文档的版本信息
xrefs.add(mpdf.length);
strpdfmessage="2 0 obj\n"+ streamlen + "\nendobj\n";
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);
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();
}
}
}