Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

pdf::CDoc Class Reference

a PDF document. More...

#include <Doc.hh>

Collaboration diagram for pdf::CDoc:

Collaboration graph
[legend]
List of all members.

Public Types

Public Methods

Private Methods

Private Attributes


Detailed Description

a PDF document.

the PDF document class. it represents a PDF document. it contains a list of pages and document-wide resources such as fonts, images or cross references.

note that a PDF document is not the same as the PDF file. the file is just a place to store the content of the document. the document is the logical container of all logical objects in the document.

clients of this class will create an object, and add contents to it, e.g. :

    CDoc doc ;
    doc.AddPage( page ) ;
    
    // ... adding more contents
    
    ofstream fstr( "output.pdf" ) ;
    fstr << doc ;

Since version 0.0.2, you can add PDF document information to the document:

    CDocInfo info( "My Document", "Nestal Wan", "Testing", "libpdf++",
                   "libpdf++", "libpdf++" ) ;
    doc.Info( info ) ;

See the CDocInfo class for details.


Constructor & Destructor Documentation

pdf::CDoc::CDoc  
 

default constructor will create an empty document.

00050     : m_default_page_info( 594.0f, 828.0f )
00051 {
00052     assert( IsValid( ) ) ;
00053 }

pdf::CDoc::~CDoc  
 

nothing special. just free all resources.

00058 {
00059     assert( IsValid( ) ) ;
00060 }


Member Function Documentation

void pdf::CDoc::AddPage const page::CPage   page
 

insert a page to the document. the page is passed by value such that further modification of "page" will not affect the document.

Parameters:
page  the page object to be added

00102 {
00103     assert( IsValid( ) ) ;
00104     m_pages.push_back( page ) ;
00105 }

void pdf::CDoc::AddFont font::CFont   font
 

add a font to the document. a font must be added to document before it can be used.

Parameters:
font  the font to be added. it will be owned by the document after calling AddFont(). it is assumed to be created by new. the document will delete it in the destructor.
Note:
callers of AddFont() should NOT delete the "font" parameter after calling AddFont().
fonts cannot be shared between multiple documents.

00073 {
00074     assert( font != 0 ) ;
00075     assert( IsValid( ) ) ;
00076     
00077     m_res_dict.AddFont( font ) ;
00078 }

void pdf::CDoc::AddXObj graph::CXObject   xobj
 

add an xobject to the document. an xobject is an object that can appear in a page but is not stored inside the page object. an xobject must be added to the document before it can appear in the page.

Note:
xobjects cannot be shared between multiple documents.
See also:
page::CPage::AddXObjInst()

00089 {
00090     assert( xobj != 0 ) ;
00091     assert( IsValid( ) ) ;
00092 
00093     m_res_dict.AddXObj( xobj ) ;
00094 }

std::ostream & pdf::CDoc::Write std::ostream &    os const
 

write the document to an output. note that ihe ostream cannot be an unredirected standard output because CDoc relies on tellp() internally. tellg() will always return -1 for cout. use a ofstream instead.

Parameters:
os  a std::ostream other than cout/cerr/clog

00138 {
00139     assert( IsValid( ) ) ;
00140     assert( !m_pages.empty( ) ) ;   // the document should have pages to write
00141 
00142     using namespace std ;
00143     using namespace core ;
00144 
00145     // the resulting PDF file
00146     CFile pdf ;
00147     
00148     // first write the page tree object
00149     CObjRef page_tree = WritePageTree( pdf ) ;
00150 
00151     // create the catalog object
00152     CDictionary *catalog = new CDictionary ;
00153     catalog->AddPair( "Type", "Catalog" ) ;
00154     catalog->AddPair( CName( "Pages" ), page_tree.Dup( ) ) ;
00155     
00156     // add catalog to file
00157     CObjRef cata_ref = pdf.AddObj( catalog ) ;
00158 
00159     // write document info
00160     if ( !m_doc_info.Empty( ) )
00161     {
00162         CDictionary *info_dict = new CDictionary ;
00163         m_doc_info.Write( info_dict ) ;
00164 
00165         CObjRef ref = pdf.AddObj( info_dict ) ;
00166         
00167         return pdf.Write( os, cata_ref, &ref ) ;
00168     }
00169     else
00170         return pdf.Write( os, cata_ref, 0 ) ;
00171 }

page::CPage & pdf::CDoc::operator[] size_type    index
 

this function grants access to the added pages in the document.

Precondition:
index is within the range [0, page count), assertion will fail otherwise.

00112 {
00113     assert( index >= 0 && index < m_pages.size( ) ) ;
00114     assert( IsValid( ) ) ;
00115     return m_pages[index] ;
00116 }

const page::CPage & pdf::CDoc::operator[] size_type    index const
 

same as the non-constant version, except it returns a constant reference.

Precondition:
index is within the range [0, page count), assertion will fail otherwise.

00124 {
00125     assert( index >= 0 && index < m_pages.size( ) ) ;
00126     assert( IsValid( ) ) ;
00127     return m_pages[index] ;
00128 }

void pdf::CDoc::Info const CDocInfo   doc_info
 

change the document information. if the document info is "empty", the document info will not be written to the PDF file.

See also:
CDocInfo::Empty( )

00220 {
00221     assert( IsValid( ) ) ;
00222     m_doc_info = doc_info ;
00223 }

const CDocInfo & pdf::CDoc::Info   const
 

get the document information.

00228 {
00229     assert( IsValid( ) ) ;
00230     return m_doc_info ;
00231 }

const core::CObjRef pdf::CDoc::WritePageTree core::CFile   file const [private]
 

write the page tree. the page tree is a dictionary that contain the list of pages. it also contain all global resources like fonts and PDF xobjects.

00178 {
00179     assert( IsValid( ) ) ;
00180 
00181     using namespace core ;
00182     using namespace std ;
00183 
00184     CDictionary *page_tree = new CDictionary ;
00185     page_tree->AddPair( CName( "Type" ), new CName( "Pages" ) ) ;
00186     
00187     CObjRef page_tree_ref = file.AddObj( page_tree ) ;
00188     CArray *page_array = new CArray ;
00189     
00190     // write all page objects
00191     typedef vector<page::CPage>::const_iterator iterator ;
00192     for ( iterator i = m_pages.begin( ) ; i != m_pages.end( ) ; ++i )
00193     {
00194         // write the page to the PDF file
00195         CObjRef ref = i->Write( page_tree_ref, m_default_page_info, file ) ;
00196         
00197         // save the page's reference
00198         page_array->push_back( ref.Dup( ) ) ;
00199     }
00200 
00201     // order should not be important
00202     page_tree->AddPair( CName( "Count" ), new CNumber( m_pages.size( ) ) ) ;
00203     page_tree->AddPair( CName( "Kids" ), page_array ) ;
00204 
00205     // write resource
00206     page_tree->AddPair( CName( "Resources" ), m_res_dict.Write( file ) ) ;
00207 
00208     // use default page info as info of the page tree object
00209     m_default_page_info.Write( page_tree, page::CInfo( ) ) ;
00210 
00211     return page_tree_ref ;
00212 }


Member Data Documentation

std::vector<page::CPage> pdf::CDoc::m_pages [private]
 

the pages in this documents.

page::CResDict pdf::CDoc::m_res_dict [private]
 

resource dictionary of the page tree of the document. the resources stored inside this dictionary is available to all pages in the document.

page::CInfo pdf::CDoc::m_default_page_info [private]
 

default page settings. it will be applied when a new page is created

CDocInfo pdf::CDoc::m_doc_info [private]
 

document info. this includes misc info like author, subject, keyword etc. if the doc info is "empty", it will not be writen to the PDF file.


The documentation for this class was generated from the following files:
Generated on Sun Feb 2 09:17:09 2003 for libpdf++ by doxygen1.2.16