Package com.lowagie.text.html
Class HtmlWriter
java.lang.Object
com.lowagie.text.DocWriter
com.lowagie.text.html.HtmlWriter
- All Implemented Interfaces:
DocListener
,ElementListener
,AutoCloseable
,EventListener
A
DocWriter
class for HTML.
An HtmlWriter
can be added as a DocListener
to a certain Document
by getting
an instance. Every Element
added to the original Document
will be written to the
OutputStream
of this HtmlWriter
.
Example:
// creation of the document with a certain size and certain margins Document document = new Document(PageSize.A4, 50, 50, 50, 50); try { // this will write HTML to the Standard OutputStream HtmlWriter.getInstance(document, System.out); // this will write HTML to a file called text.html HtmlWriter.getInstance(document, new FileOutputStream("text.html")); // this will write HTML to for instance the OutputStream of a HttpServletResponse-object HtmlWriter.getInstance(document, response.getOutputStream()); } catch(DocumentException de) { System.err.println(de.getMessage()); } // this will close the document and all the OutputStreams listening to it document.close();
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byte[]
This is a possible HTML-tag.This is the current font of the HTML.static final byte[]
This is a possible HTML-tag.protected HeaderFooter
This is the textual part of the footerprotected HeaderFooter
This is the textual part of a headerprotected String
This is a path for images.protected Properties
Store the markup properties of a MarkedObject.static final String
This is a possible HTML-tag.protected int
Stores the page number.protected Font
This is the standard font of the HTML. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
HtmlWriter
(Document doc, OutputStream os) Constructs aHtmlWriter
. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Signals that anElement
was added to theDocument
.boolean
Signals that aString
was added to theDocument
.void
close()
Signals that theDocument
was closed and that no otherElements
will be added.static HtmlWriter
getInstance
(Document document, OutputStream os) Gets an instance of theHtmlWriter
.protected void
Adds the header to the top of theDocument
protected void
Adds the header to the top of theDocument
boolean
isOtherFont
(Font font) Checks if a given font is the same as the font that was last used.boolean
newPage()
Signals that an new page has to be started.void
open()
Signals that theDocument
has been opened and thatElements
can be added.void
Resets the imagepath.void
setFooter
(HeaderFooter footer) Changes the footer of this document.void
setHeader
(HeaderFooter header) Changes the header of this document.void
setImagepath
(String imagepath) Sets the basepath for images.void
setStandardFont
(Font standardfont) Changes the standardfont.protected void
Writes the HTML representation of an element.protected void
write
(Font font, Properties styleAttributes) Writes the representation of aFont
.protected void
writeComment
(String comment) Writes some comment.protected void
writeCssProperty
(String prop, String value) Writes out a CSS property.protected void
writeHeader
(Meta meta) Writes a Metatag in the header.protected void
writeJavaScript
(Header header) Writes a JavaScript section or, if the markup attribute HtmlTags.URL is set, a JavaScript reference in the header.protected void
Writes a link in the header.protected void
writeSection
(Section section, int indent) Writes the HTML representation of a section.Methods inherited from class com.lowagie.text.DocWriter
addTabs, flush, getISOBytes, isCloseStream, isPaused, pause, resetFooter, resetHeader, resetPageCount, resume, setCloseStream, setMarginMirroring, setMarginMirroringTopBottom, setMargins, setPageCount, setPageSize, write, write, writeEnd, writeEnd, writeMarkupAttributes, writeStart
-
Field Details
-
BEGINCOMMENT
public static final byte[] BEGINCOMMENTThis is a possible HTML-tag. -
ENDCOMMENT
public static final byte[] ENDCOMMENTThis is a possible HTML-tag. -
NBSP
This is a possible HTML-tag.- See Also:
-
currentfont
This is the current font of the HTML. -
standardfont
This is the standard font of the HTML. -
imagepath
This is a path for images. -
pageN
protected int pageNStores the page number. -
header
This is the textual part of a header -
markup
Store the markup properties of a MarkedObject.
-
-
Constructor Details
-
HtmlWriter
Constructs aHtmlWriter
.- Parameters:
doc
- TheDocument
that has to be written as HTMLos
- TheOutputStream
the writer has to write to.
-
-
Method Details
-
getInstance
Gets an instance of theHtmlWriter
.- Parameters:
document
- TheDocument
that has to be writtenos
- TheOutputStream
the writer has to write to.- Returns:
- a new
HtmlWriter
-
newPage
public boolean newPage()Signals that an new page has to be started.- Specified by:
newPage
in interfaceDocListener
- Overrides:
newPage
in classDocWriter
- Returns:
true
if this action succeeded,false
if not.
-
add
Signals that anElement
was added to theDocument
.- Specified by:
add
in interfaceElementListener
- Overrides:
add
in classDocWriter
- Parameters:
element
- a high level object that has to be translated to HTML- 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()Signals that theDocument
has been opened and thatElements
can be added.The
HEAD
-section of the HTML-document is written.- Specified by:
open
in interfaceDocListener
- Overrides:
open
in classDocWriter
-
close
public void close()Signals that theDocument
was closed and that no otherElements
will be added.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceDocListener
- Overrides:
close
in classDocWriter
-
initHeader
protected void initHeader()Adds the header to the top of theDocument
-
writeHeader
Writes a Metatag in the header.- Parameters:
meta
- the element that has to be written- Throws:
IOException
- on error
-
writeLink
Writes a link in the header.- Parameters:
header
- the element that has to be written- Throws:
IOException
- on error
-
writeJavaScript
Writes a JavaScript section or, if the markup attribute HtmlTags.URL is set, a JavaScript reference in the header.- Parameters:
header
- the element that has to be written- Throws:
IOException
- on error
-
writeComment
Writes some comment.This method writes some comment.
- Parameters:
comment
- the comment that has to be written- Throws:
IOException
- on error
-
setStandardFont
Changes the standardfont.- Parameters:
standardfont
- The font
-
isOtherFont
Checks if a given font is the same as the font that was last used.- Parameters:
font
- the font of an object- Returns:
- true if the font differs
-
setImagepath
Sets the basepath for images.This is especially useful if you add images using a file, rather than an URL. In PDF there is no problem, since the images are added inline, but in HTML it is sometimes necessary to use a relative path or a special path to some images directory.
- Parameters:
imagepath
- the new imagepath
-
resetImagepath
public void resetImagepath()Resets the imagepath. -
add
Signals that aString
was added to theDocument
.- Parameters:
string
- a String to add to the HTML- Returns:
true
if the string was added,false
if not.
-
write
Writes the HTML representation of an element.- Parameters:
element
- the elementindent
- the indentation- Throws:
IOException
- thrown when an I/O operation fails
-
writeSection
Writes the HTML representation of a section.- Parameters:
section
- the section to writeindent
- the indentation- Throws:
IOException
- thrown when an I/O operation fails
-
write
Writes the representation of aFont
.- Parameters:
font
- aFont
styleAttributes
- the style of the font- Throws:
IOException
- thrown when an I/O operation fails
-
writeCssProperty
Writes out a CSS property.- Parameters:
prop
- a CSS propertyvalue
- the value of the CSS property- Throws:
IOException
- thrown when an I/O operation fails
-