Class Document
- All Implemented Interfaces:
AutoCloseable,EventListener,DocListener,ElementListener
- Direct Known Subclasses:
PdfDocument
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(new Rectangle(595, 842), 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 booleanHas the document already been closed?static booleanAllows the pdf documents to be produced without compression for debugging purposes.protected floatmargin in y direction starting from the bottomprotected floatmargin in x direction starting from the leftprotected 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. -
Constructor Summary
ConstructorsConstructorDescriptionDocument()Constructs a newDocument-object with the A4 page size.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
Modifier and TypeMethodDescriptionbooleanAdds anElementto theDocument.booleanAdds the author to a Document.booleanAdds the current date and time to a Document.booleanaddCreationDate(PdfDate date) Adds the current date and time to a Document.voidaddDocListener(DocListener listener) Adds aDocListenerto theDocument.booleanaddModificationDate(PdfDate date) Adds the current date and time to a Document.booleanAdds the producer to a Document.booleanaddProducer(String producer) Adds the provided value as the producer to a Document.booleanaddSubject(String subject) Adds the subject to a Document.booleanAdds the title to a Document.voidclose()Closes the document.Gets the text rendering options.static StringGets the iText version.booleanisOpen()Checks if the document is open.booleannewPage()Signals to all listeners, that a new page has to be started.voidopen()Opens the document.booleansetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) Sets the margins.booleansetPageSize(Rectangle pageSize) Sets the pagesize.voidsetTextRenderingOptions(TextRenderingOptions textRenderingOptions) Sets the text rendering options.
-
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. -
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 -
pageN
protected int pageNCurrent pagenumber
-
-
Constructor Details
-
Document
public Document()Constructs a newDocument-object with the A4 page size. -
Document
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 Details
-
getVersion
Gets the iText version.- Returns:
- iText version
-
addDocListener
Adds aDocListenerto theDocument.- Parameters:
listener- the new DocListener.
-
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 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:
openin interfaceDocListener
-
setPageSize
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:
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.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.
A new page is created when you call:
- Document.open()
- Document.newPage(), only if the current page has some content, otherwise no new page will be created
- 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 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:
newPagein interfaceDocListener- Returns:
trueif the page was added,falseif not.
-
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 interfaceAutoCloseable- Specified by:
closein interfaceDocListener
-
addTitle
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 thetitlemetatag in theHEADpart of the file. Shortcut method to call:add(new Meta(Element.TITLE, title))- Parameters:
title- the title- Returns:
trueif successful,falseotherwise
-
addSubject
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 thesubjectmetatag in theHEADpart of the file.- Parameters:
subject- the subject- Returns:
trueif successful,falseotherwise
-
addAuthor
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 theauthormetatag in theHEADpart of the file.- Parameters:
author- the name of the author- Returns:
trueif successful,falseotherwise
-
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 theHEADpart of the file.- Returns:
trueif successful,falseotherwise
-
addProducer
Adds the provided value as the producer to a Document. The default producer isOpenPDF XX.YY.ZZwhereXX.YY.ZZis 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 theHEADpart of the file.- Parameters:
producer- new producer line value- Returns:
trueif successful,falseotherwise
-
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 theHEADpart of the file.- Returns:
trueif successful,falseotherwise
-
addCreationDate
Adds the current date and time to a Document.- Returns:
trueif successful,falseotherwise
-
addModificationDate
Adds the current date and time to a Document.- Returns:
trueif successful,falseotherwise
-
isOpen
public boolean isOpen()Checks if the document is open.- Returns:
trueif the document is open
-
getTextRenderingOptions
Gets the text rendering options.- Returns:
- the text rendering options
-
setTextRenderingOptions
Sets the text rendering options.- Parameters:
textRenderingOptions- the text rendering options
-