Package com.lowagie.text
Class Document
java.lang.Object
com.lowagie.text.Document
- All Implemented Interfaces:
DocListener,ElementListener,EventListener
- Direct Known Subclasses:
PdfDocument
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:
- Once a document is created you can add some meta information.
- You can also set the headers/footers.
- You have to open the document before you can write content.
- You can only write content (no more meta-formation!) once a document is opened.
- When you change the header/footer on a certain page, this will be effective starting on the next page.
- After closing the document, every listener (as well as its
OutputStream) is closed too.
// 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
FieldsModifier and TypeFieldDescriptionprotected intThis is a chapter number in case ChapterAutoNumber is used.protected booleanHas the document already been closed?static booleanAllows the pdf documents to be produced without compression for debugging purposes.protected HeaderFooterThis is the textual part of the footerprotected HeaderFooterThis is the textual part of a Page; it can contain a headerprotected StringStyle class in HTML body tagprotected StringContent of JavaScript onLoad functionprotected StringContent of JavaScript onUnLoad functionprotected floatmargin in y direction starting from the bottomprotected floatmargin in x direction starting from the leftprotected booleanmirroring of the left/right marginsprotected booleanmirroring of the top/bottom marginsprotected floatmargin in x direction starting from the rightprotected floatmargin in y direction starting from the topprotected booleanIs the document open or not?protected intCurrent pagenumberprotected RectangleThe size of the page.static booleanWhen true the file access is not done through a memory mapped file.static floatScales the WMF font size. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds anElementto theDocument.booleanAdds the author to a Document.booleanAdds the current date and time to a Document.booleanaddCreator(String creator) Adds the creator to a Document.voidaddDocListener(DocListener listener) Adds aDocListenerto theDocument.booleanAdds a user defined header to the document.booleanaddKeywords(String keywords) Adds the keywords to a Document.booleanAdds the producer to a Document.booleanaddSubject(String subject) Adds the subject to a Document.booleanAdds 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.floatReturns the bottom margin.voidclose()Closes the document.Gets the style class of the HTML body tagGets the JavaScript onLoad command.Gets the JavaScript onUnLoad command.intReturns the current page number.Gets the pagesize.static final StringGets the product name.static final StringGets the release number.static final StringGets the iText version.booleanGets 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.floatReturns the left margin.booleannewPage()Signals that an new page has to be started.voidopen()Opens the document.voidremoveDocListener(DocListener listener) Removes aDocListenerfrom theDocument.voidResets the footer of this document.voidResets the header of this document.voidSets 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.floatReturn 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 tagvoidsetJavaScript_onLoad(String code) Adds a JavaScript onLoad function to the HTML body tagvoidsetJavaScript_onUnLoad(String code) Adds a JavaScript onUnLoad function to the HTML body tagbooleansetMarginMirroring(boolean marginMirroring) Set the margin mirroring.booleansetMarginMirroringTopBottom(boolean marginMirroringTopBottom) 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.floatReturns the top margin.
-
Field Details
-
compress
public static boolean compressAllows the pdf documents to be produced without compression for debugging purposes. -
plainRandomAccess
public static boolean plainRandomAccessWhen 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. -
wmfFontCorrection
public static float wmfFontCorrectionScales the WMF font size. The default value is 0.86. -
open
protected boolean openIs the document open or not? -
close
protected boolean closeHas the document already been closed? -
pageSize
The size of the page. -
marginLeft
protected float marginLeftmargin in x direction starting from the left -
marginRight
protected float marginRightmargin in x direction starting from the right -
marginTop
protected float marginTopmargin in y direction starting from the top -
marginBottom
protected float marginBottommargin in y direction starting from the bottom -
marginMirroring
protected boolean marginMirroringmirroring of the left/right margins -
marginMirroringTopBottom
protected boolean marginMirroringTopBottommirroring of the top/bottom margins- Since:
- 2.1.6
-
javaScript_onLoad
Content of JavaScript onLoad function -
javaScript_onUnLoad
Content of JavaScript onUnLoad function -
htmlStyleClass
Style class in HTML body tag -
pageN
protected int pageNCurrent pagenumber -
header
This is the textual part of a Page; it can contain a header -
chapternumber
protected int chapternumberThis is a chapter number in case ChapterAutoNumber is used.
-
-
Constructor Details
-
Document
public Document()Constructs a newDocument-object. -
Document
Constructs a newDocument-object.- Parameters:
pageSize- the pageSize
-
Document
public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom) Constructs a newDocument-object.- Parameters:
pageSize- the pageSizemarginLeft- the margin on the leftmarginRight- the margin on the rightmarginTop- the margin on the topmarginBottom- the margin on the bottom
-
-
Method Details
-
addDocListener
Adds aDocListenerto theDocument.- Parameters:
listener- the new DocListener.
-
removeDocListener
Removes aDocListenerfrom theDocument.- Parameters:
listener- the DocListener that has to be removed.
-
add
Adds anElementto theDocument.- Specified by:
addin interfaceElementListener- Parameters:
element- theElementto add- Returns:
trueif the element was added,falseif 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.
- Specified by:
openin interfaceDocListener
-
setPageSize
Sets the pagesize.- Specified by:
setPageSizein interfaceDocListener- Parameters:
pageSize- the new pagesize- Returns:
- a
boolean
-
setMargins
public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) Sets the margins.- Specified by:
setMarginsin interfaceDocListener- Parameters:
marginLeft- the margin on the leftmarginRight- the margin on the rightmarginTop- the margin on the topmarginBottom- the margin on the bottom- Returns:
- a
boolean
-
newPage
public boolean newPage()Signals that an new page has to be started.- Specified by:
newPagein interfaceDocListener- Returns:
trueif the page was added,falseif not.
-
resetHeader
public void resetHeader()Resets the header of this document.- Specified by:
resetHeaderin interfaceDocListener
-
resetPageCount
public void resetPageCount()Sets the page number to 0.- Specified by:
resetPageCountin interfaceDocListener
-
setPageCount
public void setPageCount(int pageN) Sets the page number.- Specified by:
setPageCountin interfaceDocListener- Parameters:
pageN- the new page number
-
getPageNumber
public int getPageNumber()Returns the current page number.- Returns:
- the current page number
-
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.
- Specified by:
closein interfaceDocListener
-
addHeader
Adds a user defined header to the document.- Parameters:
name- the name of the headercontent- the content of the header- Returns:
trueif successful,falseotherwise
-
addTitle
Adds the title to a Document.- Parameters:
title- the title- Returns:
trueif successful,falseotherwise
-
addSubject
Adds the subject to a Document.- Parameters:
subject- the subject- Returns:
trueif successful,falseotherwise
-
addKeywords
Adds the keywords to a Document.- Parameters:
keywords- adds the keywords to the document- Returns:
trueif successful,falseotherwise
-
addAuthor
Adds the author to a Document.- Parameters:
author- the name of the author- Returns:
trueif successful,falseotherwise
-
addCreator
Adds the creator to a Document.- Parameters:
creator- the name of the creator- Returns:
trueif successful,falseotherwise
-
addProducer
public boolean addProducer()Adds the producer to a Document.- Returns:
trueif successful,falseotherwise
-
addCreationDate
public boolean addCreationDate()Adds the current date and time to a Document.- Returns:
trueif successful,falseotherwise
-
leftMargin
public float leftMargin()Returns the left margin.- Returns:
- the left margin
-
rightMargin
public float rightMargin()Return the right margin.- Returns:
- the right margin
-
topMargin
public float topMargin()Returns the top margin.- Returns:
- the top margin
-
bottomMargin
public float bottomMargin()Returns the bottom margin.- Returns:
- the bottom margin
-
left
public float left()Returns the lower left x-coordinate.- Returns:
- the lower left x-coordinate
-
right
public float right()Returns the upper right x-coordinate.- Returns:
- the upper right x-coordinate
-
top
public float top()Returns the upper right y-coordinate.- Returns:
- the upper right y-coordinate
-
bottom
public float bottom()Returns the lower left y-coordinate.- Returns:
- the lower left y-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
-
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
-
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
-
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
-
getPageSize
Gets the pagesize.- Returns:
- the page size
-
isOpen
public boolean isOpen()Checks if the document is open.- Returns:
trueif the document is open
-
getProduct
Gets the product name.- Returns:
- the product name
- Since:
- 2.1.6
-
getRelease
Gets the release number.- Returns:
- the product name
- Since:
- 2.1.6
-
getVersion
Gets the iText version.- Returns:
- iText version
-
setJavaScript_onLoad
Adds a JavaScript onLoad function to the HTML body tag- Parameters:
code- the JavaScript code to be executed on load of the HTML page
-
getJavaScript_onLoad
Gets the JavaScript onLoad command.- Returns:
- the JavaScript onLoad command
-
setJavaScript_onUnLoad
Adds a JavaScript onUnLoad function to the HTML body tag- Parameters:
code- the JavaScript code to be executed on unload of the HTML page
-
getJavaScript_onUnLoad
Gets the JavaScript onUnLoad command.- Returns:
- the JavaScript onUnLoad command
-
setHtmlStyleClass
Adds a style class to the HTML body tag- Parameters:
htmlStyleClass- the style class for the HTML body tag
-
getHtmlStyleClass
Gets the style class of the HTML body tag- Returns:
- the style class of the HTML body tag
-
setMarginMirroring
public boolean setMarginMirroring(boolean marginMirroring) Set the margin mirroring. It will mirror right/left margins for odd/even pages.Note: it will not work with
Table.- Specified by:
setMarginMirroringin interfaceDocListener- Parameters:
marginMirroring-trueto mirror the margins- Returns:
- always
true
-
setMarginMirroringTopBottom
public boolean setMarginMirroringTopBottom(boolean marginMirroringTopBottom) Set the margin mirroring. It will mirror top/bottom margins for odd/even pages.Note: it will not work with
Table.- Specified by:
setMarginMirroringTopBottomin interfaceDocListener- Parameters:
marginMirroringTopBottom-trueto mirror the margins- Returns:
- always
true - Since:
- 2.1.6
-
isMarginMirroring
public boolean isMarginMirroring()Gets the margin mirroring flag.- Returns:
- the margin mirroring flag
-