com.lowagie.text

Class Document

public class Document extends Object implements DocListener

A generic Document class.

All kinds of Text-elements can be added to a HTMLDocument. The Document signals all the listeners when an element has been added.

Remark:

  1. Once a document is created you can add some meta information.
  2. You can also set the headers/footers.
  3. You have to open the document before you can write content.
  4. You can only write content (no more meta-formation!) once a document is opened.
  5. When you change the header/footer on a certain page, this will be effective starting on the next page.
  6. Ater closing the document, every listener (as well as its OutputStream) is closed too.
Example:
// creation of the document with a certain size and certain margins
 Document document = new Document(PageSize.A4, 50, 50, 50, 50);
  try { 
   // creation of the different writers 
   HtmlWriter.getInstance(document , System.out);
   PdfWriter.getInstance(document , new FileOutputStream("text.pdf"));
   // we add some meta information to the document
   document.addAuthor("Bruno Lowagie"); 
   document.addSubject("This is the result of a Test."); 
   // we open the document for writing
   document.open(); 
   document.add(new Paragraph("Hello world"));
  } catch(DocumentException de) {
   System.err.println(de.getMessage());
  }
  document.close();
 
Field Summary
protected intchapternumber
This is a chapter number in case ChapterAutoNumber is used.
protected booleanclose
Has the document already been closed?
static booleancompress
Allows the pdf documents to be produced without compression for debugging purposes.
protected HeaderFooterfooter
This is the textual part of the footer
protected HeaderFooterheader
This is the textual part of a Page; it can contain a header
protected StringhtmlStyleClass
Style class in HTML body tag
static StringITEXT_VERSION
This constant may only be changed by Paulo Soares and/or Bruno Lowagie.
protected StringjavaScript_onLoad
Content of JavaScript onLoad function
protected StringjavaScript_onUnLoad
Content of JavaScript onUnLoad function
ArrayListlisteners
The DocListener.
protected floatmarginBottom
margin in y direction starting from the bottom
protected floatmarginLeft
margin in x direction starting from the left
protected booleanmarginMirroring
protected floatmarginRight
margin in x direction starting from the right
protected floatmarginTop
margin in y direction starting from the top
protected booleanopen
Is the document open or not?
protected intpageN
Current pagenumber
protected RectanglepageSize
The size of the page.
static booleanplainRandomAccess
When true the file access is not done through a memory mapped file.
Constructor Summary
Document()
Constructs a new Document -object.
Document(Rectangle pageSize)
Constructs a new Document -object.
Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)
Constructs a new Document -object.
Method Summary
booleanadd(Element element)
Adds an Element to the Document.
booleanaddAuthor(String author)
Adds the author to a Document.
booleanaddCreationDate()
Adds the current date and time to a Document.
booleanaddCreator(String creator)
Adds the creator to a Document.
voidaddDocListener(DocListener listener)
Adds a DocListener to the Document.
booleanaddHeader(String name, String content)
Adds a user defined header to the document.
booleanaddKeywords(String keywords)
Adds the keywords to a Document.
booleanaddProducer()
Adds the producer to a Document.
booleanaddSubject(String subject)
Adds the subject to a Document.
booleanaddTitle(String title)
Adds the title to a Document.
floatbottom()
Returns the lower left y-coordinate.
floatbottom(float margin)
Returns the lower left y-coordinate, considering a given margin.
floatbottomMargin()
Returns the bottom margin.
voidclose()
Closes the document.
StringgetHtmlStyleClass()
Gets the style class of the HTML body tag
StringgetJavaScript_onLoad()
Gets the JavaScript onLoad command.
StringgetJavaScript_onUnLoad()
Gets the JavaScript onUnLoad command.
intgetPageNumber()
Returns the current page number.
RectanglegetPageSize()
Gets the pagesize.
static StringgetVersion()
Gets the iText version.
booleanisMarginMirroring()
Gets the margin mirroring flag.
booleanisOpen()
Checks if the document is open.
floatleft()
Returns the lower left x-coordinate.
floatleft(float margin)
Returns the lower left x-coordinate considering a given margin.
floatleftMargin()
Returns the left margin.
booleannewPage()
Signals that an new page has to be started.
voidopen()
Opens the document.
voidremoveDocListener(DocListener listener)
Removes a DocListener from the Document.
voidresetFooter()
Resets the footer of this document.
voidresetHeader()
Resets the header of this document.
voidresetPageCount()
Sets the page number to 0.
floatright()
Returns the upper right x-coordinate.
floatright(float margin)
Returns the upper right x-coordinate, considering a given margin.
floatrightMargin()
Return the right margin.
voidsetFooter(HeaderFooter footer)
Changes the footer of this document.
voidsetHeader(HeaderFooter header)
Changes the header of this document.
voidsetHtmlStyleClass(String htmlStyleClass)
Adds a style class to the HTML body tag
voidsetJavaScript_onLoad(String code)
Adds a JavaScript onLoad function to the HTML body tag
voidsetJavaScript_onUnLoad(String code)
Adds a JavaScript onUnLoad function to the HTML body tag
booleansetMarginMirroring(boolean marginMirroring)
Set the margin mirroring.
booleansetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)
Sets the margins.
voidsetPageCount(int pageN)
Sets the page number.
booleansetPageSize(Rectangle pageSize)
Sets the pagesize.
floattop()
Returns the upper right y-coordinate.
floattop(float margin)
Returns the upper right y-coordinate, considering a given margin.
floattopMargin()
Returns the top margin.

Field Detail

chapternumber

protected int chapternumber
This is a chapter number in case ChapterAutoNumber is used.

close

protected boolean close
Has the document already been closed?

compress

public static boolean compress
Allows the pdf documents to be produced without compression for debugging purposes.

footer

protected HeaderFooter footer
This is the textual part of the footer

header

protected HeaderFooter header
This is the textual part of a Page; it can contain a header

htmlStyleClass

protected String htmlStyleClass
Style class in HTML body tag

ITEXT_VERSION

private static final String ITEXT_VERSION
This constant may only be changed by Paulo Soares and/or Bruno Lowagie.

javaScript_onLoad

protected String javaScript_onLoad
Content of JavaScript onLoad function

javaScript_onUnLoad

protected String javaScript_onUnLoad
Content of JavaScript onUnLoad function

listeners

private ArrayList listeners
The DocListener.

marginBottom

protected float marginBottom
margin in y direction starting from the bottom

marginLeft

protected float marginLeft
margin in x direction starting from the left

marginMirroring

protected boolean marginMirroring

marginRight

protected float marginRight
margin in x direction starting from the right

marginTop

protected float marginTop
margin in y direction starting from the top

open

protected boolean open
Is the document open or not?

pageN

protected int pageN
Current pagenumber

pageSize

protected Rectangle pageSize
The size of the page.

plainRandomAccess

public static boolean plainRandomAccess
When true the file access is not done through a memory mapped file. Use it if the file is too big to be mapped in your address space.

Constructor Detail

Document

public Document()
Constructs a new Document -object.

Document

public Document(Rectangle pageSize)
Constructs a new Document -object.

Parameters: pageSize the pageSize

Document

public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)
Constructs a new Document -object.

Parameters: pageSize the pageSize marginLeft the margin on the left marginRight the margin on the right marginTop the margin on the top marginBottom the margin on the bottom

Method Detail

add

public boolean add(Element element)
Adds an Element to the Document.

Parameters: element the Element to add

Returns: true if the element was added, false if not

Throws: DocumentException when a document isn't open yet, or has been closed

addAuthor

public boolean addAuthor(String author)
Adds the author to a Document.

Parameters: author the name of the author

Returns: true if successful, false otherwise

addCreationDate

public boolean addCreationDate()
Adds the current date and time to a Document.

Returns: true if successful, false otherwise

addCreator

public boolean addCreator(String creator)
Adds the creator to a Document.

Parameters: creator the name of the creator

Returns: true if successful, false otherwise

addDocListener

public void addDocListener(DocListener listener)
Adds a DocListener to the Document.

Parameters: listener the new DocListener.

addHeader

public boolean addHeader(String name, String content)
Adds a user defined header to the document.

Parameters: name the name of the header content the content of the header

Returns: true if successful, false otherwise

addKeywords

public boolean addKeywords(String keywords)
Adds the keywords to a Document.

Parameters: keywords adds the keywords to the document

Returns: true if successful, false otherwise

addProducer

public boolean addProducer()
Adds the producer to a Document.

Returns: true if successful, false otherwise

addSubject

public boolean addSubject(String subject)
Adds the subject to a Document.

Parameters: subject the subject

Returns: true if successful, false otherwise

addTitle

public boolean addTitle(String title)
Adds the title to a Document.

Parameters: title the title

Returns: true if successful, false otherwise

bottom

public float bottom()
Returns the lower left y-coordinate.

Returns: the lower left y-coordinate

bottom

public float bottom(float margin)
Returns the lower left y-coordinate, considering a given margin.

Parameters: margin a margin

Returns: the lower left y-coordinate

bottomMargin

public float bottomMargin()
Returns the bottom margin.

Returns: the bottom margin

close

public void close()
Closes the document.

Once all the content has been written in the body, you have to close the body. After that nothing can be written to the body anymore.

getHtmlStyleClass

public String getHtmlStyleClass()
Gets the style class of the HTML body tag

Returns: the style class of the HTML body tag

getJavaScript_onLoad

public String getJavaScript_onLoad()
Gets the JavaScript onLoad command.

Returns: the JavaScript onLoad command

getJavaScript_onUnLoad

public String getJavaScript_onUnLoad()
Gets the JavaScript onUnLoad command.

Returns: the JavaScript onUnLoad command

getPageNumber

public int getPageNumber()
Returns the current page number.

Returns: the current page number

getPageSize

public Rectangle getPageSize()
Gets the pagesize.

Returns: the page size

getVersion

public static final String getVersion()
Gets the iText version. This method may only be changed by Paulo Soares and/or Bruno Lowagie.

Returns: iText version

isMarginMirroring

public boolean isMarginMirroring()
Gets the margin mirroring flag.

Returns: the margin mirroring flag

isOpen

public boolean isOpen()
Checks if the document is open.

Returns: true if the document is open

left

public float left()
Returns the lower left x-coordinate.

Returns: the lower left x-coordinate

left

public float left(float margin)
Returns the lower left x-coordinate considering a given margin.

Parameters: margin a margin

Returns: the lower left x-coordinate

leftMargin

public float leftMargin()
Returns the left margin.

Returns: the left margin

newPage

public boolean newPage()
Signals that an new page has to be started.

Returns: true if the page was added, false if not.

Throws: DocumentException when a document isn't open yet, or has been closed

open

public void open()
Opens the document.

Once the document is opened, you can't write any Header- or Meta-information anymore. You have to open the document before you can begin to add content to the body of the document.

removeDocListener

public void removeDocListener(DocListener listener)
Removes a DocListener from the Document.

Parameters: listener the DocListener that has to be removed.

resetFooter

public void resetFooter()
Resets the footer of this document.

resetHeader

public void resetHeader()
Resets the header of this document.

resetPageCount

public void resetPageCount()
Sets the page number to 0.

right

public float right()
Returns the upper right x-coordinate.

Returns: the upper right x-coordinate

right

public float right(float margin)
Returns the upper right x-coordinate, considering a given margin.

Parameters: margin a margin

Returns: the upper right x-coordinate

rightMargin

public float rightMargin()
Return the right margin.

Returns: the right margin

setFooter

public void setFooter(HeaderFooter footer)
Changes the footer of this document.

Parameters: footer the new footer

setHeader

public void setHeader(HeaderFooter header)
Changes the header of this document.

Parameters: header the new header

setHtmlStyleClass

public void setHtmlStyleClass(String htmlStyleClass)
Adds a style class to the HTML body tag

Parameters: htmlStyleClass the style class for the HTML body tag

setJavaScript_onLoad

public void setJavaScript_onLoad(String code)
Adds a JavaScript onLoad function to the HTML body tag

Parameters: code the JavaScript code to be executed on load of the HTML page

setJavaScript_onUnLoad

public void setJavaScript_onUnLoad(String code)
Adds a JavaScript onUnLoad function to the HTML body tag

Parameters: code the JavaScript code to be executed on unload of the HTML page

setMarginMirroring

public boolean setMarginMirroring(boolean marginMirroring)
Set the margin mirroring. It will mirror margins for odd/even pages.

Note: it will not work with Table.

Parameters: marginMirroring true to mirror the margins

Returns: always true

setMargins

public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)
Sets the margins.

Parameters: marginLeft the margin on the left marginRight the margin on the right marginTop the margin on the top marginBottom the margin on the bottom

Returns: a boolean

setPageCount

public void setPageCount(int pageN)
Sets the page number.

Parameters: pageN the new page number

setPageSize

public boolean setPageSize(Rectangle pageSize)
Sets the pagesize.

Parameters: pageSize the new pagesize

Returns: a boolean

top

public float top()
Returns the upper right y-coordinate.

Returns: the upper right y-coordinate

top

public float top(float margin)
Returns the upper right y-coordinate, considering a given margin.

Parameters: margin a margin

Returns: the upper right y-coordinate

topMargin

public float topMargin()
Returns the top margin.

Returns: the top margin