public class Document extends Object implements DocListener
All kinds of Text-elements can be added to a HTMLDocument. The Document signals all the
listeners when an element has been added.
Remark:
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();
| Modifier and Type | Field and 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 footer
|
protected HeaderFooter |
header
This is the textual part of a Page; it can contain a header
|
protected String |
htmlStyleClass
Style class in HTML body tag
|
protected String |
javaScript_onLoad
Content of JavaScript onLoad function
|
protected String |
javaScript_onUnLoad
Content of JavaScript onUnLoad function
|
protected float |
marginBottom
margin in y direction starting from the bottom
|
protected float |
marginLeft
margin in x direction starting from the left
|
protected boolean |
marginMirroring
mirroring of the left/right margins
|
protected boolean |
marginMirroringTopBottom
mirroring of the top/bottom margins
|
protected float |
marginRight
margin in x direction starting from the right
|
protected float |
marginTop
margin in y direction starting from the top
|
protected boolean |
open
Is the document open or not?
|
protected int |
pageN
Current pagenumber
|
protected Rectangle |
pageSize
The size of the page.
|
static boolean |
plainRandomAccess
When true the file access is not done through a memory mapped file.
|
static float |
wmfFontCorrection
Scales the WMF font size.
|
| Constructor and Description |
|---|
Document()
Constructs a new
Document -object with the default page size as PageSize.A4 . |
Document(Rectangle pageSize)
Constructs a new
Document -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 new
Document -object. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(Element element)
Adds an
Element to the Document. |
boolean |
addAuthor(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(String creator)
Adds the creator to a Document.
|
void |
addDocListener(DocListener listener)
Adds a
DocListener to the Document. |
boolean |
addHeader(String name,
String content)
Adds a user defined header to the document.
|
boolean |
addKeywords(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(String producer)
Adds the provided value as the producer to a Document.
|
boolean |
addSubject(String subject)
Adds the subject to a Document.
|
boolean |
addTitle(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.
|
String |
getDocumentLanguage()
The default language of the document.
|
String |
getHtmlStyleClass()
Gets the style class of the HTML body tag
|
String |
getJavaScript_onLoad()
Gets the JavaScript onLoad command.
|
String |
getJavaScript_onUnLoad()
Gets the JavaScript onUnLoad command.
|
int |
getPageNumber()
Returns the current page number.
|
Rectangle |
getPageSize()
Gets the pagesize.
|
static String |
getProduct()
Gets the product name.
|
static String |
getRelease()
Gets the release number.
|
TextRenderingOptions |
getTextRenderingOptions()
Gets the text rendering options.
|
static String |
getVersion()
Gets the iText version.
|
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 a
DocListener from the Document. |
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(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(String htmlStyleClass)
Adds a style class to the HTML body tag
|
void |
setJavaScript_onLoad(String code)
Adds a JavaScript onLoad function to the HTML body tag
|
void |
setJavaScript_onUnLoad(String code)
Adds a JavaScript onUnLoad function to the HTML body tag
|
boolean |
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.
|
public static boolean compress
public static boolean plainRandomAccess
public static float wmfFontCorrection
protected boolean open
protected boolean close
protected Rectangle pageSize
protected float marginLeft
protected float marginRight
protected float marginTop
protected float marginBottom
protected boolean marginMirroring
protected boolean marginMirroringTopBottom
protected String javaScript_onLoad
protected String javaScript_onUnLoad
protected String htmlStyleClass
protected int pageN
protected HeaderFooter header
protected HeaderFooter footer
protected int chapternumber
public Document()
Document -object with the default page size as PageSize.A4 .public Document(Rectangle pageSize)
Document -object using the given rectangle as page size and default margin of 36.pageSize - the pageSizepublic Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)
Document -object.pageSize - the pageSizemarginLeft - the margin on the leftmarginRight - the margin on the rightmarginTop - the margin on the topmarginBottom - the margin on the bottompublic static String getProduct()
public static String getRelease()
public static String getVersion()
public void addDocListener(DocListener listener)
DocListener to the Document.listener - the new DocListener.public void removeDocListener(DocListener listener)
DocListener from the Document.listener - the DocListener that has to be removed.public boolean add(Element element) throws DocumentException
Element to the Document.add in interface ElementListenerelement - the Element to addtrue if the element was added, false
if notDocumentException - when a document isn't open yet, or has been closedpublic void open()
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.
open in interface DocListenerpublic boolean setPageSize(Rectangle 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.
setPageSize in interface DocListenerpageSize - the new pagesizebooleanpublic boolean setMargins(float marginLeft,
float marginRight,
float marginTop,
float marginBottom)
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.
setMargins in interface DocListenermarginLeft - the margin on the leftmarginRight - the margin on the rightmarginTop - the margin on the topmarginBottom - the margin on the bottombooleanpublic boolean newPage()
newPage in interface DocListenertrue if the page was added, false
if not.public void setHeader(HeaderFooter header)
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.
setHeader in interface DocListenerheader - the new headerpublic void resetHeader()
This change will be effective starting from the next page.
resetHeader in interface DocListenerpublic void setFooter(HeaderFooter footer)
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.
setFooter in interface DocListenerfooter - the new footerpublic void resetFooter()
This change will be effective starting from the next page.
resetFooter in interface DocListenerpublic void resetPageCount()
This change will be effective starting from the next page.
resetPageCount in interface DocListenerpublic void setPageCount(int pageN)
This change will be effective starting from the next page.
The page number of the next new page will be: pageN + 1
setPageCount in interface DocListenerpageN - the new page numberpublic int getPageNumber()
public void close()
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.
close in interface DocListenerclose in interface AutoCloseablepublic boolean addHeader(String name, String content)
add(new Header(name, content))name - the name of the headercontent - the content of the headertrue if successful, false otherwisepublic boolean addTitle(String title)
title meta tag in the
HEAD part of the file.
Shortcut method to call: add(new Meta(Element.TITLE, title))title - the titletrue if successful, false otherwisepublic boolean addSubject(String subject)
subject meta tag in the
HEAD part of the file.subject - the subjecttrue if successful, false otherwisepublic boolean addKeywords(String keywords)
keywords meta tag in the
HEAD part of the file.keywords - adds the keywords to the documenttrue if successful, false otherwisepublic boolean addAuthor(String author)
author meta tag in the
HEAD part of the file.author - the name of the authortrue if successful, false otherwisepublic boolean addCreator(String creator)
HEAD part of the file.creator - the name of the creatortrue if successful, false otherwisepublic boolean addProducer()
HEAD part of the file.true if successful, false otherwisepublic boolean addProducer(String producer)
OpenPDF XX.YY.ZZ where XX.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 the HEAD part of the file.producer - new producer line valuetrue if successful, false otherwisepublic boolean addCreationDate()
HEAD part of the file.true if successful, false otherwisepublic boolean addCreationDate(PdfDate date)
true if successful, false otherwisepublic boolean addModificationDate()
true if successful, false otherwisepublic boolean addModificationDate(PdfDate date)
true if successful, false otherwisepublic float leftMargin()
public float rightMargin()
public float topMargin()
public float bottomMargin()
public float left()
public float right()
public float top()
public float bottom()
public float left(float margin)
margin - a marginpublic float right(float margin)
margin - a marginpublic float top(float margin)
margin - a marginpublic float bottom(float margin)
margin - a marginpublic Rectangle getPageSize()
public boolean isOpen()
true if the document is openpublic String getJavaScript_onLoad()
public void setJavaScript_onLoad(String code)
code - the JavaScript code to be executed on load of the HTML pagepublic String getJavaScript_onUnLoad()
public void setJavaScript_onUnLoad(String code)
code - the JavaScript code to be executed on unload of the HTML pagepublic String getHtmlStyleClass()
public void setHtmlStyleClass(String htmlStyleClass)
htmlStyleClass - the style class for the HTML body tagpublic boolean setMarginMirroring(boolean marginMirroring)
Note: it will not work with Table.
setMarginMirroring in interface DocListenermarginMirroring - true to mirror the marginstruepublic boolean setMarginMirroringTopBottom(boolean marginMirroringTopBottom)
Note: it will not work with Table.
setMarginMirroringTopBottom in interface DocListenermarginMirroringTopBottom - true to mirror the marginstruepublic boolean isMarginMirroring()
public String getDocumentLanguage()
public void setDocumentLanguage(String documentLanguage)
documentLanguage - the wanted languagepublic boolean isGlyphSubstitutionEnabled()
setGlyphSubstitutionEnabled(boolean)public void setGlyphSubstitutionEnabled(boolean glyphSubstitutionEnabled)
glyphSubstitutionEnabled - the glyph substitution enabled flagFopGlyphProcessor,
setDocumentLanguage(String)public TextRenderingOptions getTextRenderingOptions()
getDocumentLanguage(),
isGlyphSubstitutionEnabled()public void setTextRenderingOptions(TextRenderingOptions textRenderingOptions)
textRenderingOptions - the text rendering optionssetDocumentLanguage(String),
setGlyphSubstitutionEnabled(boolean)Copyright © 2024. All rights reserved.