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();
BEGINCOMMENT
public static final byte[] BEGINCOMMENT
This is a possible HTML-tag.
ENDCOMMENT
public static final byte[] ENDCOMMENT
This is a possible HTML-tag.
NBSP
public static final String NBSP
This is a possible HTML-tag.
currentfont
protected Stack currentfont
This is the current font of the HTML.
footer
protected HeaderFooter footer
This is the textual part of the footer
header
protected HeaderFooter header
This is the textual part of a header
imagepath
protected String imagepath
This is a path for images.
pageN
protected int pageN
Stores the page number.
standardfont
protected Font standardfont
This is the standard font of the HTML.
add
public boolean add(String string)
throws DocumentException
Signals that a String
was added to the Document
.
string
- a String to add to the HTML
true
if the string was added, false
if not.
add
public boolean add(Element element)
throws DocumentException
Signals that an Element
was added to the Document
.
- add in interface ElementListener
- add in interface DocWriter
element
- a high level object that has to be translated to HTML
true
if the element was added, false
if not.
close
public void close()
Signals that the Document
was closed and that no other
Elements
will be added.
- close in interface DocListener
- close in interface DocWriter
getInstance
public static HtmlWriter getInstance(Document document,
OutputStream os)
Gets an instance of the HtmlWriter
.
document
- The Document
that has to be writtenos
- The OutputStream
the writer has to write to.
initFooter
protected void initFooter()
Adds the header to the top of the Document
initHeader
protected void initHeader()
Adds the header to the top of the Document
isOtherFont
public boolean isOtherFont(Font font)
Checks if a given font is the same as the font that was last used.
font
- the font of an object
open
public void open()
Signals that the
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 interface DocWriter
resetImagepath
public void resetImagepath()
Resets the imagepath.
setImagepath
public void setImagepath(String imagepath)
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.
imagepath
- the new imagepath
setStandardFont
public void setStandardFont(Font standardfont)
Changes the standardfont.
write
protected void write(Element element,
int indent)
throws IOException
Writes the HTML representation of an element.
element
- the elementindent
- the indentation
write
protected void write(Font font,
Properties styleAttributes)
throws IOException
Writes the representation of a Font
.
font
- a Font
styleAttributes
- the style of the font
writeComment
protected void writeComment(String comment)
throws IOException
Writes some comment.
This method writes some comment.
comment
- the comment that has to be written
writeCssProperty
protected void writeCssProperty(String prop,
String value)
throws IOException
Writes out a CSS property.
prop
- a CSS propertyvalue
- the value of the CSS property
writeHeader
protected void writeHeader(Meta meta)
throws IOException
Writes a Metatag in the header.
meta
- the element that has to be written
writeJavaScript
protected void writeJavaScript(Header header)
throws IOException
Writes a JavaScript section or, if the markup attribute HtmlTags.URL is set, a JavaScript reference in the header.
header
- the element that has to be written
writeLink
protected void writeLink(Header header)
throws IOException
Writes a link in the header.
header
- the element that has to be written
writeSection
protected void writeSection(Section section,
int indent)
throws IOException
Writes the HTML representation of a section.
section
- the section to writeindent
- the indentation