public class HtmlWriter extends DocWriter implements DocListener
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();
Modifier and Type | Field and Description |
---|---|
static byte[] |
BEGINCOMMENT
This is a possible HTML-tag.
|
protected java.util.Stack |
currentfont
This is the current font of the HTML.
|
static byte[] |
ENDCOMMENT
This is a possible HTML-tag.
|
protected HeaderFooter |
footer
This is the textual part of the footer
|
protected HeaderFooter |
header
This is the textual part of a header
|
protected java.lang.String |
imagepath
This is a path for images.
|
static java.lang.String |
NBSP
This is a possible HTML-tag.
|
protected int |
pageN
Stores the page number.
|
protected Font |
standardfont
This is the standard font of the HTML.
|
Modifier | Constructor and Description |
---|---|
protected |
HtmlWriter(Document doc,
java.io.OutputStream os)
Constructs a
HtmlWriter . |
Modifier and Type | Method and Description |
---|---|
boolean |
add(Element element)
Signals that an
Element was added to the Document . |
boolean |
add(java.lang.String string)
Signals that a
String was added to the Document . |
void |
close()
Signals that the
Document was closed and that no other
Elements will be added. |
static HtmlWriter |
getInstance(Document document,
java.io.OutputStream os)
Gets an instance of the
HtmlWriter . |
protected void |
initFooter()
Adds the header to the top of the Document
|
protected void |
initHeader()
Adds the header to the top of the Document
|
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 the
Document has been opened and that
Elements can be added. |
void |
resetImagepath()
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(java.lang.String imagepath)
Sets the basepath for images.
|
void |
setStandardFont(Font standardfont)
Changes the standardfont.
|
protected void |
write(Element element,
int indent)
Writes the HTML representation of an element.
|
protected void |
write(Font font,
java.util.Properties styleAttributes)
Writes the representation of a
Font . |
protected void |
writeComment(java.lang.String comment)
Writes some comment.
|
protected void |
writeCssProperty(java.lang.String prop,
java.lang.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 |
writeLink(Header header)
Writes a link in the header.
|
protected void |
writeSection(Section section,
int indent)
Writes the HTML representation of a section.
|
add, addTabs, clearTextWrap, flush, getISOBytes, hasMarkupAttributes, isCloseStream, pause, removeWatermark, resetFooter, resetHeader, resetPageCount, resume, setCloseStream, setMarginMirroring, setMargins, setPageCount, setPageSize, write, write, writeEnd, writeEnd, writeMarkupAttributes, writeStart
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
add, clearTextWrap, removeWatermark, resetFooter, resetHeader, resetPageCount, setMarginMirroring, setMargins, setPageCount, setPageSize
public static final byte[] BEGINCOMMENT
public static final byte[] ENDCOMMENT
public static final java.lang.String NBSP
protected java.util.Stack currentfont
protected Font standardfont
protected java.lang.String imagepath
protected int pageN
protected HeaderFooter header
protected HeaderFooter footer
protected HtmlWriter(Document doc, java.io.OutputStream os)
HtmlWriter
.doc
- The Document
that has to be written as HTMLos
- The OutputStream
the writer has to write to.public static HtmlWriter getInstance(Document document, java.io.OutputStream os)
HtmlWriter
.document
- The Document
that has to be writtenos
- The OutputStream
the writer has to write to.HtmlWriter
public boolean newPage() throws DocumentException
newPage
in interface DocListener
newPage
in class DocWriter
true
if this action succeeded, false
if not.DocumentException
- when a document isn't open yet, or has been closedpublic boolean add(Element element) throws DocumentException
Element
was added to the Document
.add
in interface ElementListener
add
in class DocWriter
element
- a high level object that has to be translated to HTMLtrue
if the element was added, false
if not.DocumentException
- when a document isn't open yet, or has been closedpublic void open()
Document
has been opened and that
Elements
can be added.
The HEAD
-section of the HTML-document is written.
open
in interface DocListener
open
in class DocWriter
public void close()
Document
was closed and that no other
Elements
will be added.close
in interface DocListener
close
in class DocWriter
protected void initHeader()
protected void initFooter()
protected void writeHeader(Meta meta) throws java.io.IOException
meta
- the element that has to be writtenjava.io.IOException
protected void writeLink(Header header) throws java.io.IOException
header
- the element that has to be writtenjava.io.IOException
protected void writeJavaScript(Header header) throws java.io.IOException
header
- the element that has to be writtenjava.io.IOException
protected void writeComment(java.lang.String comment) throws java.io.IOException
This method writes some comment.
comment
- the comment that has to be writtenjava.io.IOException
public void setStandardFont(Font standardfont)
standardfont
- The fontpublic boolean isOtherFont(Font font)
font
- the font of an objectpublic void setImagepath(java.lang.String imagepath)
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.
imagepath
- the new imagepathpublic void resetImagepath()
public void setHeader(HeaderFooter header)
setHeader
in interface DocListener
setHeader
in class DocWriter
header
- the new headerpublic void setFooter(HeaderFooter footer)
setFooter
in interface DocListener
setFooter
in class DocWriter
footer
- the new footerpublic boolean add(java.lang.String string) throws DocumentException
String
was added to the Document
.string
- a String to add to the HTMLtrue
if the string was added, false
if not.DocumentException
- when a document isn't open yet, or has been closedprotected void write(Element element, int indent) throws java.io.IOException
element
- the elementindent
- the indentationjava.io.IOException
protected void writeSection(Section section, int indent) throws java.io.IOException
section
- the section to writeindent
- the indentationjava.io.IOException
protected void write(Font font, java.util.Properties styleAttributes) throws java.io.IOException
Font
.font
- a Font
styleAttributes
- the style of the fontjava.io.IOException
protected void writeCssProperty(java.lang.String prop, java.lang.String value) throws java.io.IOException
prop
- a CSS propertyvalue
- the value of the CSS propertyjava.io.IOException