Click here to Skip to main content
13,901,646 members
Click here to Skip to main content
Add your own
alternative version


95 bookmarked
Posted 25 May 2004

The Great PDF - Revealed

, 20 Aug 2004
Rate this:
Please Sign up or sign in to vote.
A project to create PDF files using text editor and digitally sign them.


I have always wondered about the Adobe Reader and the PDF files. Have you ever tried to open a PDF file in a text editor? It’s amazing! In this project I am trying to bring the hidden things behind the PDF files to light. This simple application lets you create PDF files, just as you create txt files from a Notepad (hence the name pdfpad). Type your text in the editor and save it as a PDF file. Of course, you need Acrobat Reader to view the created PDF file. You cannot open an existing PDF file in this editor. You can only create, and once created its done. The greatest feature of this project is the digital signature. It teaches you the very basics of adding an invisible digital signature to the files created using pdfpad. It  automatically adds an invisible digital signature when you create PDF files in pdfpad.exe.

Due to lack of time, many of the details couldn't be included. Please bear with us.


Firstly, one should know the basics of the PDF format. I recommend you to download a copy of the PDF reference manual from PDF Reference and go through it (oops it's 1000 pages!).

Download the application demo, enter some text, and save the file as PDF. Now open the file in notepad and read on…

If one says C++ is object oriented, I would say PDF is more object oriented. In a PDF everything is treated as an object and every object has its own property and will refer other objects. This makes large PDF files (A 1000 page book just downloaded) to be navigated randomly and quickly.

A PDF file is read from the last. There is a token called the startxref, this is were everything begins. A viewer application reads this entry to get the offset of a table called xref. The table lists the objects used in the file and also their byte offsets within the PDF file. The format of the entries greatly matter here. Each entry should be 20 bytes long including the carriage return and the line feed.

Every object is numbered sequentially starting from 0 to n. ( though not necessary). If you notice the xref entry you will find a ‘0’ and a number n. This means that the table contains n objects starting from 0. Just take a look at them… 0000000074 this is the byte offset, 00000 is the generation number, n ..means it's in use. Only the first entry has the generation number that is not zero and it's marked f. Read the reference manual for more details.

A PDF document can be regarded as a hierarchy of objects contained in the body section of a PDF file. At the root of the hierarchy are the document's catalog dictionary. Most of the objects in the hierarchy are dictionaries. Each page of the document is represented by a page object, which is a dictionary that includes references to the page contents and other attributes such as its thumbnail image and any annotations associated with it. The individual page objects are tied together in a structure called the page tree, which in turn is located via an indirect reference in the document catalog.

The root of a document object hierarchy is the catalog dictionary, located via the Root entry in the trailer of the PDF file. The catalog contains references to other objects that define the document's contents, outline article threads, named destinations and other attributes.

Now to start with, the reader reads the value of the root entry in the trailer. This is the root. This is the root of all the references that are to be made. Now the reader reads the byte offset of the root object and moves to the root. This is a catalog dictionary. This again contains many other  references. In our application only minimum entries are made so that it is easy to understand.

Now let’s see what happens to the text that we enter in the edit box. Firstly, all the occurrences of the end of line are replaced with the PDF operators for line feed. Then all the operators for showing the text on the page is added in the contents dictionary. This content is added as a stream, which is called a content stream. For compressing the text I have used zlib, courtesy zlib, this is a freely downloadable library. Flat compression algorithm is used to compress the text. This algorithm is supported by the Adobe viewer.

The most amazing thing is about the digital signature. I haven't employed a real life digital signature using cryptographic libraries. All I intend to show is, how to add a digital signature to the PDF document. The entries here are all dummy entries. This signature can be made a real digital signature if you can change the contents entry in the Signature dictionary with the real signed hash of the document.

I won’t be covering the details of the digital signature here. I will stick to the details of the PDF. PDF has two types of digital signatures, invisible and visible. Our application uses invisible signatures. The signature can be viewed in the signature panel. The entries in the signature dictionary can be changed to put your name, time of signing, location etc., programmatically using the user's inputs. This is left to you.

When a digital signature is added to a document, the Adobe acrobats signature handler calculates a checksum that is based on the content of the document at that time and it embeds the checksum in the signature. When the signature is validated, the handler recalculates the checksum for that signed version of the document and compares it with the value in the signature. If the signed version has changed in any way the signature handler detects the change and marks the signature as invalid.

You can also use Crypto API to create the hash, Sign using Digital Certificates etc., which I hope to cover in my next article. While creating the hash, the byte range must be specified correctly. Byte range is an array of two integers, Starting offset and number of bytes. Byte range array is used to exclude the contents entry in the signature dictionary. This entry will be filled with a temp entry initially to get the total file size for calculating the hash. After creating the hash the contents entries are be made. This explains why byte range is specified so as to exclude the contents entry from creating the hash. Otherwise while verifying the signature it may get invalidated.

Once you get a grip of the reference manual you can modify the code below, to add more pages, add drawing to the below etc.

Using the code

The main function that creates the PDF files is added to the ***doc class, it's called CreatedPdf ( CString text). I enjoy manipulating the CString object rather than using the char buffers. You can modify accordingly to make it more efficient. The code is well commented to explain the details.

This is a part of the Doc class, that should be modified to write the files in PDF format:

/*You should derive the view class from CEditView and modify the 
serialize function as below.*/
void CPdfPadDoc::Serialize(CArchive& ar)
           CString strFull;
           CEdit &edit =((CEditView*)m_viewList.GetHead())->GetEditCtrl();

The main function written in doc class is the CreatePdf(). This actually takes the text and returns the formatted PDF to be written to the file:

CString CPdfPadDoc::CreatePdf(CString text)
  /*To replace all the the endof line with appropriate 
  pdf graphics commands.*/
  text.Replace("\r\n",")Tj T*(");
  /*accordingly to the inputs to the editor*/
  /*it will hold the byte offsets of the various 
  objects within the pdf file*/
  int objArray[10];
  int fontSize=1;
  /*start of the first line. This is the left margin*/
  int hPos=50;
  /*The top margin*/
  int vPos=750;
  CString fileBuff;
  /*This is version 1.5 of the pdf reference manual*/
  /*important for correct build*/
  CString header="%PDF-1.5\r%\xC3\xBE\r\n";
  /*It is called catalog dictionary*/
          CString catalog="1 0 obj<</Pages "
            "2 0 R/Type /Catalog/AcroForm 6 0 R>>\nendobj\r";
  /*This example only contains one page. So kids contains 
  only one reference*/
          CString pageTree="2 0 obj<</Count 1/"
                "Kids [3 0 R]/Type /Pages>>\nendobj\r";
  /*used the page. */
          CString page="3 0 obj<</Annots[7 0 R]/"
               "Contents [5 0 R]/Type /Page/Parent 2 0 R/Rotate 0/"
               "MediaBox[0 0 612 792]/CropBox[0 0 612 792]/"
               "Resources<</Font<</T1_0 4 0 R>>/"
          CString font="4 0 obj<</Type/Font/BaseFont/"
  /*Every text is associated with a text matrix. 
  Details of this operators can be 
  found in pdf reference manual*/
        CString stream;
        stream.Format("%s%d%s%d%s%d%s%s%s","0 g\r1 i \rBT\r/T1_0 ",
          fontSize," Tf\r0 Tc 0 Tw 0  Ts 100  Tz 0 Tr 1.2 TL 12 0 0 12 ",
          hPos," ",vPos," Tm \rT* (",text,")Tj \rET");
  /*Compress the stream using flate compression*/
          CString compressedStream=FlateCompress(stream);/*1*/
          int len=compressedStream.GetLength();
  /*if you dont what the stream to be compressed. Replace 
  the line number 1 and remove the entry from
  string contents /Filter /FlateDecode
  CString compressedStream=stream*/
          CString contents;
               "5 0 obj<</Filter /FlateDecode/Length ",len,
          CString acroForm;
          acroForm="6 0 obj<</Fields[7 0 R]/SigFlags 3/"
                       "DA(/Helv 0 Tf 0 g )>>\nendobj\n";
          CString annotation;
          annotation="7 0 obj<</Type /Annots/Subtype /"
                    "Widget/FT /Sig/Rect[0 0 0 0]/P 3 0 R/"
                    "T(signature)/V 8 0 R/MK<<>>>>\"
          CString sign;
          sign="8 0 obj<</Type /Sig/Filter/ICM.SignDoc/Contents";
          int byteRange[2];
  //This is just a dummy signature. Actually it should be 
  //taken after using cryptographic library.
  //In the next version...
          CString signature="<AE423B23FE56>";
          //We dont know the actual byte range. ie the end of file. 
          //Therefore it is a dummy entry now.
          //We will replace it after we get the length of file.
          sign.Format("%s%d%s%d%s","/ByteRange [0 ",byteRange[0],
             " ",byteRange[1],"XXX]/Name(Shiraz)/"
             "Location(Cordiant)/Reason(ICM Library)"
             "/Date(Nov  3 200314:27:40)>>\nendobj\n");
  /*This table will contain the objects used in the 
  file and there byte offsets*/
          CString xref;
          /*Please look in article*/
          xref.Format("%s%d%s","xref\r\n0 ",9,
                         "\r\n0000000000 65535 f\r\n");
  int numObj=8;
          CString offsets;
          for(int i=0;i<numObj;i++)
                 //This field should be 20 bytes long.
                 xref+=offsets+" 00000 n\r\n";
          CString trailer;
          trailer.Format("%s%d%s","trailer\n<</Size 9/"
              "Root 1 0 R/ID[<5181383ede94727bcb32ac27ded71c68"
  /*We have finished with the pdf file.One thing 
  left is the actual byte range.*/
  CString byteRangeEnd;
  /*retrun the final string*/
  return fileBuff;

This method is used to compress the content stream. The usage of the DLL can be found in zlib.dll.

CString CPdfPadDoc::FlateCompress(CString inputStream)
  CMemFile *pInput=new CMemFile();
  CMemFile *pOutput=new CMemFile();
  z_stream zstream;
  DWORD inputLength=inputStream.GetLength();
  char *inBuffer=new char[inputLength];
  BYTE zBufIn[20000];
  BYTE zBufOut[4000];
  deflateInit(&zstream, Z_DEFAULT_COMPRESSION);
      int error = Z_OK;
      while ( TRUE )
        UINT cbRead = 0;
         cbRead = pInput->Read(zBufIn, sizeof(zBufIn));
         if ( cbRead == 0 )
         zstream.next_in = (Bytef*)zBufIn;
         zstream.avail_in = (uInt)cbRead;
         while ( TRUE )
             zstream.next_out = (Bytef*)zBufOut;
             zstream.avail_out = sizeof(zBufOut);
             err = deflate(&zstream, Z_SYNC_FLUSH);
             if (err != Z_OK)
             UINT cbWrite = sizeof(zBufOut) - zstream.avail_out;
             if ( cbWrite == 0 )
            pOutput->Write(zBufOut, cbWrite);
             if ( zstream.avail_out != 0 )
  error = deflateEnd(&zstream);//Check the error if required
  DWORD szOutBuff=pOutput->GetLength();
  char *outBuffer=(char*)malloc(szOutBuff);
  CString outStream(outBuffer);


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Engineer Hydenso Steel & Engineering Pvt Ltd
India India
Settled in the beautiful city of Kochi in India.
Started working in VC++, dotnet in the hometown from 2003 after searching hard for some position in research in robotics and Aritificial Interlligence.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Manoj Kumar Choubey9-Feb-12 22:05
professionalManoj Kumar Choubey9-Feb-12 22:05 
Questionwhy does it not suport chinese? Pin
zzss_wjl31-Dec-09 18:36
memberzzss_wjl31-Dec-09 18:36 
GeneralGood one Pin
lxjhonson7-Jan-09 19:13
memberlxjhonson7-Jan-09 19:13 
QuestionHow to convert it to a console app. Pin
Subrat 47082669-Nov-08 16:52
memberSubrat 47082669-Nov-08 16:52 
QuestionOpen signature tab [modified] Pin
Deepali Dhingra16-Aug-07 22:55
professionalDeepali Dhingra16-Aug-07 22:55 
QuestionHow can i rescale the Pages Pin
powerkru31-May-07 19:28
memberpowerkru31-May-07 19:28 
Generalsignature Pin
tonirodado2-May-07 5:43
membertonirodado2-May-07 5:43 
QuestionAny body can explain this code ? Pin
Arunnath25-Dec-05 22:27
memberArunnath25-Dec-05 22:27 
GeneralGreat Information Pin
David Roh27-Nov-05 3:54
memberDavid Roh27-Nov-05 3:54 
QuestionMerge 2 pdf files ? Pin
dragomir27-Oct-05 4:36
memberdragomir27-Oct-05 4:36 
AnswerRe: Merge 2 pdf files ? Pin
shiraztk27-Oct-05 7:23
membershiraztk27-Oct-05 7:23 
GeneralRe: Merge 2 pdf files ? Pin
dragomir27-Oct-05 20:35
memberdragomir27-Oct-05 20:35 
QuestionSignature does not check Pin
Pandele Florin28-Sep-05 2:21
memberPandele Florin28-Sep-05 2:21 
GeneralHOWTO: Drawing Code Pin
justsuraj29-Jun-05 18:53
memberjustsuraj29-Jun-05 18:53 
GeneralHOWTO: Drawing Code Pin
justsuraj29-Jun-05 18:53
memberjustsuraj29-Jun-05 18:53 
Questionline moveto ? Pin
donj8226-May-05 0:19
memberdonj8226-May-05 0:19 
GeneralGood but memory leak Pin
Xiong Shijie27-Apr-05 2:29
memberXiong Shijie27-Apr-05 2:29 
GeneralAdded Unicode support Pin
Sekar_Mokachandra22-Mar-05 8:28
memberSekar_Mokachandra22-Mar-05 8:28 
GeneralFlateCompress removed Pin
Sekar_Mokachandra22-Mar-05 7:58
memberSekar_Mokachandra22-Mar-05 7:58 
GeneralRe: FlateCompress removed Pin
shiraztk22-Mar-05 18:19
membershiraztk22-Mar-05 18:19 
Generalunicode Pin
Lin Lin6-Jan-05 15:39
memberLin Lin6-Jan-05 15:39 
QuestionWhy is SigFlags = 3? Pin
Anonymous4-Jan-05 4:45
memberAnonymous4-Jan-05 4:45 
AnswerRe: Why is SigFlags = 3? Pin
shiraztk4-Jan-05 18:07
membershiraztk4-Jan-05 18:07 
GeneralRe: Why is SigFlags = 3? Pin
Anonymous5-Jan-05 4:59
memberAnonymous5-Jan-05 4:59 
QuestionIs it possible to generate this Pdf WITHOUT the digital signature? Pin
rogerio_duarte10-Dec-04 2:37
memberrogerio_duarte10-Dec-04 2:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190306.1 | Last Updated 21 Aug 2004
Article Copyright 2004 by shiraztk
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid