Class Document
- java.lang.Object
-
- com.lowagie.text.Document
-
- All Implemented Interfaces:
DocListener
,ElementListener
,java.lang.AutoCloseable
,java.util.EventListener
- Direct Known Subclasses:
PdfDocument
public class Document extends java.lang.Object implements DocListener
A generic Document class.All kinds of Text-elements can be added to a
HTMLDocument
. TheDocument
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
Fields Modifier and Type Field Description protected int
chapternumber
This is a chapter number in case ChapterAutoNumber is used.protected boolean
close
Has the document already been closed?static boolean
compress
Allows the pdf documents to be produced without compression for debugging purposes.protected HeaderFooter
footer
This is the textual part of the footerprotected HeaderFooter
header
This is the textual part of a Page; it can contain a headerprotected java.lang.String
htmlStyleClass
Style class in HTML body tagprotected java.lang.String
javaScript_onLoad
Content of JavaScript onLoad functionprotected java.lang.String
javaScript_onUnLoad
Content of JavaScript onUnLoad functionprivate java.util.List<DocListener>
listeners
The DocListener.protected float
marginBottom
margin in y direction starting from the bottomprotected float
marginLeft
margin in x direction starting from the leftprotected boolean
marginMirroring
mirroring of the left/right marginsprotected boolean
marginMirroringTopBottom
mirroring of the top/bottom marginsprotected float
marginRight
margin in x direction starting from the rightprotected float
marginTop
margin in y direction starting from the topprotected boolean
open
Is the document open or not?private static java.lang.String
OPENPDF
private static java.lang.String
OPENPDF_VERSION
protected int
pageN
Current pagenumberprotected Rectangle
pageSize
The size of the page.static boolean
plainRandomAccess
When true the file access is not done through a memory mapped file.private static java.lang.String
RELEASE
(package private) TextRenderingOptions
textRenderingOptions
Text rendering options, including the default language of the document and a flag to enable font glyph substitution (if FOP is available)private static java.lang.String
VERSION_PROPERTIES
static float
wmfFontCorrection
Scales the WMF font size.
-
Constructor Summary
Constructors Constructor Description Document()
Constructs a newDocument
-object with the default page size asPageSize.A4
.Document(Rectangle pageSize)
Constructs a newDocument
-object using the given rectangle as page size and default margin of 36.Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)
Constructs a newDocument
-object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(Element element)
Adds anElement
to theDocument
.boolean
addAuthor(java.lang.String author)
Adds the author to a Document.boolean
addCreationDate()
Adds the current date and time to a Document.boolean
addCreationDate(PdfDate date)
Adds the current date and time to a Document.boolean
addCreator(java.lang.String creator)
Adds the creator to a Document.void
addDocListener(DocListener listener)
Adds aDocListener
to theDocument
.boolean
addHeader(java.lang.String name, java.lang.String content)
Adds a user defined header to the document.boolean
addKeywords(java.lang.String keywords)
Adds the keywords to a Document.boolean
addModificationDate()
Adds the current date and time to a Document.boolean
addModificationDate(PdfDate date)
Adds the current date and time to a Document.boolean
addProducer()
Adds the producer to a Document.boolean
addProducer(java.lang.String producer)
Adds the provided value as the producer to a Document.boolean
addSubject(java.lang.String subject)
Adds the subject to a Document.boolean
addTitle(java.lang.String title)
Adds the title to a Document.float
bottom()
Returns the lower left y-coordinate.float
bottom(float margin)
Returns the lower left y-coordinate, considering a given margin.float
bottomMargin()
Returns the bottom margin.void
close()
Closes the document.java.lang.String
getDocumentLanguage()
The default language of the document.java.lang.String
getHtmlStyleClass()
Gets the style class of the HTML body tagjava.lang.String
getJavaScript_onLoad()
Gets the JavaScript onLoad command.java.lang.String
getJavaScript_onUnLoad()
Gets the JavaScript onUnLoad command.int
getPageNumber()
Returns the current page number.Rectangle
getPageSize()
Gets the pagesize.static java.lang.String
getProduct()
Gets the product name.static java.lang.String
getRelease()
Gets the release number.TextRenderingOptions
getTextRenderingOptions()
Gets the text rendering options.static java.lang.String
getVersion()
Gets the iText version.private static java.lang.String
getVersionNumber()
boolean
isGlyphSubstitutionEnabled()
Returns the glyph substitution enabled flag.boolean
isMarginMirroring()
Gets the margin mirroring flag.boolean
isOpen()
Checks if the document is open.float
left()
Returns the lower left x-coordinate.float
left(float margin)
Returns the lower left x-coordinate considering a given margin.float
leftMargin()
Returns the left margin.boolean
newPage()
Signals to all listeners, that a new page has to be started.void
open()
Opens the document.void
removeDocListener(DocListener listener)
Removes aDocListener
from theDocument
.void
resetFooter()
Resets the footer of this document.void
resetHeader()
Resets the header of this document.void
resetPageCount()
Sets the page number to 0.float
right()
Returns the upper right x-coordinate.float
right(float margin)
Returns the upper right x-coordinate, considering a given margin.float
rightMargin()
Return the right margin.void
setDocumentLanguage(java.lang.String documentLanguage)
The new document language.void
setFooter(HeaderFooter footer)
Changes the footer of this document.void
setGlyphSubstitutionEnabled(boolean glyphSubstitutionEnabled)
Set a flag that determine whether glyph substion is enabled when FOP is available.void
setHeader(HeaderFooter header)
Changes the header of this document.void
setHtmlStyleClass(java.lang.String htmlStyleClass)
Adds a style class to the HTML body tagvoid
setJavaScript_onLoad(java.lang.String code)
Adds a JavaScript onLoad function to the HTML body tagvoid
setJavaScript_onUnLoad(java.lang.String code)
Adds a JavaScript onUnLoad function to the HTML body tagboolean
setMarginMirroring(boolean marginMirroring)
Set the margin mirroring.boolean
setMarginMirroringTopBottom(boolean marginMirroringTopBottom)
Set the margin mirroring.boolean
setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)
Sets the margins.void
setPageCount(int pageN)
Sets the page number.boolean
setPageSize(Rectangle pageSize)
Sets the pagesize.void
setTextRenderingOptions(TextRenderingOptions textRenderingOptions)
Sets the text rendering options.float
top()
Returns the upper right y-coordinate.float
top(float margin)
Returns the upper right y-coordinate, considering a given margin.float
topMargin()
Returns the top margin.
-
-
-
Field Detail
-
VERSION_PROPERTIES
private static final java.lang.String VERSION_PROPERTIES
- See Also:
- Constant Field Values
-
OPENPDF
private static final java.lang.String OPENPDF
- See Also:
- Constant Field Values
-
RELEASE
private static final java.lang.String RELEASE
-
OPENPDF_VERSION
private static final java.lang.String OPENPDF_VERSION
-
compress
public static boolean compress
Allows the pdf documents to be produced without compression for debugging purposes.
-
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.
-
wmfFontCorrection
public static float wmfFontCorrection
Scales the WMF font size. The default value is 0.86.
-
open
protected boolean open
Is the document open or not?
-
close
protected boolean close
Has the document already been closed?
-
pageSize
protected Rectangle pageSize
The size of the page.
-
marginLeft
protected float marginLeft
margin in x direction starting from the left
-
marginRight
protected float marginRight
margin in x direction starting from the right
-
marginTop
protected float marginTop
margin in y direction starting from the top
-
marginBottom
protected float marginBottom
margin in y direction starting from the bottom
-
marginMirroring
protected boolean marginMirroring
mirroring of the left/right margins
-
marginMirroringTopBottom
protected boolean marginMirroringTopBottom
mirroring of the top/bottom margins- Since:
- 2.1.6
-
javaScript_onLoad
protected java.lang.String javaScript_onLoad
Content of JavaScript onLoad function
-
javaScript_onUnLoad
protected java.lang.String javaScript_onUnLoad
Content of JavaScript onUnLoad function
-
htmlStyleClass
protected java.lang.String htmlStyleClass
Style class in HTML body tag
-
pageN
protected int pageN
Current pagenumber
-
header
protected HeaderFooter header
This is the textual part of a Page; it can contain a header
-
footer
protected HeaderFooter footer
This is the textual part of the footer
-
chapternumber
protected int chapternumber
This is a chapter number in case ChapterAutoNumber is used.
-
textRenderingOptions
TextRenderingOptions textRenderingOptions
Text rendering options, including the default language of the document and a flag to enable font glyph substitution (if FOP is available)- Since:
- 3.1.15
-
listeners
private java.util.List<DocListener> listeners
The DocListener.
-
-
Constructor Detail
-
Document
public Document()
Constructs a newDocument
-object with the default page size asPageSize.A4
.
-
Document
public Document(Rectangle pageSize)
Constructs a newDocument
-object using the given rectangle as page size and default margin of 36.- 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 Detail
-
getVersionNumber
private static java.lang.String getVersionNumber()
-
getProduct
public static java.lang.String getProduct()
Gets the product name.- Returns:
- the product name
- Since:
- 2.1.6
-
getRelease
public static java.lang.String getRelease()
Gets the release number.- Returns:
- the product name
- Since:
- 2.1.6
-
getVersion
public static java.lang.String getVersion()
Gets the iText version.- Returns:
- iText version
-
addDocListener
public void addDocListener(DocListener listener)
Adds aDocListener
to theDocument
.- Parameters:
listener
- the new DocListener.
-
removeDocListener
public void removeDocListener(DocListener listener)
Removes aDocListener
from theDocument
.- Parameters:
listener
- the DocListener that has to be removed.
-
add
public boolean add(Element element) throws DocumentException
Adds anElement
to theDocument
.- Specified by:
add
in interfaceElementListener
- Parameters:
element
- theElement
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
-
open
public void open()
Opens the document.Once the document is opened, you can't write any meta-information anymore. If you change the header/footer after opening the document, the change will be effective starting from the second page. You have to open the document before you can begin to add content to the body of the document.
- Specified by:
open
in interfaceDocListener
-
setPageSize
public boolean setPageSize(Rectangle pageSize)
Sets the pagesize.This change will be effective starting from the next page. If you want to change the page size of the first page, you need to set it before opening the document.
- Specified by:
setPageSize
in interfaceDocListener
- Parameters:
pageSize
- the new pagesize- Returns:
- a
boolean
-
setMargins
public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)
Sets the margins.This change will be effective starting from the next page. If you want to change margins on the first page, you need to set it before opening the document.
- Specified by:
setMargins
in 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 to all listeners, that a new page has to be started. New pages can only be added on already opened and not yet closed documents.- Specified by:
newPage
in interfaceDocListener
- Returns:
true
if the page was added,false
if not.
-
setHeader
public void setHeader(HeaderFooter header)
Changes the header of this document.This change will be effective starting from the next page. If you want to have a header on the first page, you need to set it before opening the document.
- Specified by:
setHeader
in interfaceDocListener
- Parameters:
header
- the new header
-
resetHeader
public void resetHeader()
Resets the header of this document.This change will be effective starting from the next page.
- Specified by:
resetHeader
in interfaceDocListener
-
setFooter
public void setFooter(HeaderFooter footer)
Changes the footer of this document.This change will be effective starting from the next page. If you want to have a footer on the first page, you need to set it before opening the document.
- Specified by:
setFooter
in interfaceDocListener
- Parameters:
footer
- the new footer
-
resetFooter
public void resetFooter()
Resets the footer of this document.This change will be effective starting from the next page.
- Specified by:
resetFooter
in interfaceDocListener
-
resetPageCount
public void resetPageCount()
Sets the page number to 0.This change will be effective starting from the next page.
- Specified by:
resetPageCount
in interfaceDocListener
-
setPageCount
public void setPageCount(int pageN)
Sets the page number.This change will be effective starting from the next page.
The page number of the next new page will be:
pageN + 1
- Specified by:
setPageCount
in 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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfaceDocListener
-
addHeader
public boolean addHeader(java.lang.String name, java.lang.String content)
Adds a user defined header to the document. Shortcut method to call:add(new Header(name, content))
- Parameters:
name
- the name of the headercontent
- the content of the header- Returns:
true
if successful,false
otherwise
-
addTitle
public boolean addTitle(java.lang.String title)
Adds the title to a Document. In case of a PDF this will be visible in the document properties panel. In case of a HTML file this will be visible as thetitle
meta
tag in theHEAD
part of the file. Shortcut method to call:add(new Meta(Element.TITLE, title))
- Parameters:
title
- the title- Returns:
true
if successful,false
otherwise
-
addSubject
public boolean addSubject(java.lang.String subject)
Adds the subject to a Document. In case of a PDF this will be visible in the document properties panel. In case of a HTML file this will be visible as thesubject
meta
tag in theHEAD
part of the file.- Parameters:
subject
- the subject- Returns:
true
if successful,false
otherwise
-
addKeywords
public boolean addKeywords(java.lang.String keywords)
Adds the keywords to a Document. In case of a PDF this will be visible in the document properties panel. In case of a HTML file this will be visible as thekeywords
meta
tag in theHEAD
part of the file.- Parameters:
keywords
- adds the keywords to the document- Returns:
true
if successful,false
otherwise
-
addAuthor
public boolean addAuthor(java.lang.String author)
Adds the author to a Document. In case of a PDF this will be visible in the document properties panel. In case of a HTML file this will be visible as theauthor
meta
tag in theHEAD
part of the file.- Parameters:
author
- the name of the author- Returns:
true
if successful,false
otherwise
-
addCreator
public boolean addCreator(java.lang.String creator)
Adds the creator to a Document. In case of a PDF this will be visible in the document properties panel. In case of a HTML file this will be visible as comment in theHEAD
part of the file.- Parameters:
creator
- the name of the creator- Returns:
true
if successful,false
otherwise
-
addProducer
public boolean addProducer()
Adds the producer to a Document. In case of a PDF this will be visible in the document properties panel. In case of a HTML file this will be visible as comment in theHEAD
part of the file.- Returns:
true
if successful,false
otherwise
-
addProducer
public boolean addProducer(java.lang.String producer)
Adds the provided value as the producer to a Document. The default producer isOpenPDF XX.YY.ZZ
whereXX.YY.ZZ
is the version of the OpenPDF library used to produce the document In case of a PDF this will be visible in the document properties panel. In case of a HTML file this will be visible as comment in theHEAD
part of the file.- Parameters:
producer
- new producer line value- Returns:
true
if successful,false
otherwise
-
addCreationDate
public boolean addCreationDate()
Adds the current date and time to a Document. In case of a PDF this will be visible in the document properties panel. In case of a HTML file this will be visible as comment in theHEAD
part of the file.- Returns:
true
if successful,false
otherwise
-
addCreationDate
public boolean addCreationDate(PdfDate date)
Adds the current date and time to a Document.- Returns:
true
if successful,false
otherwise
-
addModificationDate
public boolean addModificationDate()
Adds the current date and time to a Document.- Returns:
true
if successful,false
otherwise
-
addModificationDate
public boolean addModificationDate(PdfDate date)
Adds the current date and time to a Document.- Returns:
true
if successful,false
otherwise
-
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
public Rectangle getPageSize()
Gets the pagesize.- Returns:
- the page size
-
isOpen
public boolean isOpen()
Checks if the document is open.- Returns:
true
if the document is open
-
getJavaScript_onLoad
public java.lang.String getJavaScript_onLoad()
Gets the JavaScript onLoad command.- Returns:
- the JavaScript onLoad command
-
setJavaScript_onLoad
public void setJavaScript_onLoad(java.lang.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
-
getJavaScript_onUnLoad
public java.lang.String getJavaScript_onUnLoad()
Gets the JavaScript onUnLoad command.- Returns:
- the JavaScript onUnLoad command
-
setJavaScript_onUnLoad
public void setJavaScript_onUnLoad(java.lang.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
-
getHtmlStyleClass
public java.lang.String getHtmlStyleClass()
Gets the style class of the HTML body tag- Returns:
- the style class of the HTML body tag
-
setHtmlStyleClass
public void setHtmlStyleClass(java.lang.String htmlStyleClass)
Adds a style class to the HTML body tag- Parameters:
htmlStyleClass
- the style class for 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:
setMarginMirroring
in interfaceDocListener
- Parameters:
marginMirroring
-true
to 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:
setMarginMirroringTopBottom
in interfaceDocListener
- Parameters:
marginMirroringTopBottom
-true
to mirror the margins- Returns:
- always
true
- Since:
- 2.1.6
-
isMarginMirroring
public boolean isMarginMirroring()
Gets the margin mirroring flag.- Returns:
- the margin mirroring flag
-
getDocumentLanguage
public java.lang.String getDocumentLanguage()
The default language of the document. Can be set to values like "en_US". This language is used in FopGlyphProcessor to determine which glyphs are to be substituted. The default "dflt" means that all glyphs which can be replaced will be substituted.- Returns:
- the current document language
-
setDocumentLanguage
public void setDocumentLanguage(java.lang.String documentLanguage)
The new document language. This language is used in FopGlyphProcessor to determine which glyphs are to be substituted. The default "dflt" means that all glyphs which can be replaced will be substituted.- Parameters:
documentLanguage
- the wanted language
-
isGlyphSubstitutionEnabled
public boolean isGlyphSubstitutionEnabled()
Returns the glyph substitution enabled flag.- Returns:
- the glyph substitution enabled flag
- See Also:
setGlyphSubstitutionEnabled(boolean)
-
setGlyphSubstitutionEnabled
public void setGlyphSubstitutionEnabled(boolean glyphSubstitutionEnabled)
Set a flag that determine whether glyph substion is enabled when FOP is available.- Parameters:
glyphSubstitutionEnabled
- the glyph substitution enabled flag- See Also:
FopGlyphProcessor
,setDocumentLanguage(String)
-
getTextRenderingOptions
public TextRenderingOptions getTextRenderingOptions()
Gets the text rendering options.- Returns:
- the text rendering options
- See Also:
getDocumentLanguage()
,isGlyphSubstitutionEnabled()
-
setTextRenderingOptions
public void setTextRenderingOptions(TextRenderingOptions textRenderingOptions)
Sets the text rendering options.- Parameters:
textRenderingOptions
- the text rendering options- See Also:
setDocumentLanguage(String)
,setGlyphSubstitutionEnabled(boolean)
-
-