Reading PDF,PDG with C #
Last Update:2017-02-28
Source: Internet
Author: User
Create PDF file
Using System;
Using System.IO;
Using System.Text;
Using System.Collections;
Namespace Paladin.utility
{
<summary>
Summary description of the topdf.
</summary>
public class Topdf
{
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 MemoryStream mpdf= new MemoryStream ();
Public Topdf ()
{
//
TODO: Add constructor logic here
//
}
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 public void CreatePDF (string filePath)
{
StreamWriter Ppdf=new StreamWriter (FilePath);
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";
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 document Description
Streamstart=mpdf.length;
Font
Strpdfmessage= "bt\n/f0" + fontsize + "tf\n";
Converttobyteandaddtostream (Strpdfmessage);
PDF document Entity height
YPos = Pagedepth-pagemargin;
Strpdfmessage=pagemargin + "" + YPos + "td\n";
Converttobyteandaddtostream (Strpdfmessage);
strpdfmessage= leadsize+ "tl\n";
Converttobyteandaddtostream (Strpdfmessage);
Entity content
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);
Version information for PDF documents
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";
}
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 ();
}
}
}