- java.lang.Object
-
- com.itextpdf.text.DocWriter
-
- com.itextpdf.text.html.HtmlWriter
-
- All Implemented Interfaces:
DocListener,ElementListener,java.util.EventListener
public class HtmlWriter extends DocWriter
ADocWriterclass for HTML.An
HtmlWritercan be added as aDocListenerto a certainDocumentby getting an instance. EveryElementadded to the originalDocumentwill be written to theOutputStreamof thisHtmlWriter.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
Fields Modifier and Type Field Description static byte[]BEGINCOMMENTThis is a possible HTML-tag.protected java.util.StackcurrentfontThis is the current font of the HTML.static byte[]ENDCOMMENTThis is a possible HTML-tag.protected HeaderFooterfooterThis is the textual part of the footerprotected HeaderFooterheaderThis is the textual part of a headerprotected java.lang.StringimagepathThis is a path for images.protected java.util.PropertiesmarkupStore the markup properties of a MarkedObject.static java.lang.StringNBSPThis is a possible HTML-tag.protected intpageNStores the page number.protected FontstandardfontThis is the standard font of the HTML.
-
Constructor Summary
Constructors Modifier Constructor Description protectedHtmlWriter(Document doc, java.io.OutputStream os)Constructs aHtmlWriter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(Element element)Signals that anElementwas added to theDocument.booleanadd(java.lang.String string)Signals that aStringwas added to theDocument.voidclose()Signals that theDocumentwas closed and that no otherElementswill be added.static HtmlWritergetInstance(Document document, java.io.OutputStream os)Gets an instance of theHtmlWriter.protected voidinitFooter()Adds the header to the top of the Documentprotected voidinitHeader()Adds the header to the top of the DocumentbooleanisOtherFont(Font font)Checks if a given font is the same as the font that was last used.booleannewPage()Signals that an new page has to be started.voidopen()Signals that theDocumenthas been opened and thatElementscan be added.voidresetImagepath()Resets the imagepath.voidsetFooter(HeaderFooter footer)Changes the footer of this document.voidsetHeader(HeaderFooter header)Changes the header of this document.voidsetImagepath(java.lang.String imagepath)Sets the basepath for images.voidsetStandardFont(Font standardfont)Changes the standardfont.protected voidwrite(Element element, int indent)Writes the HTML representation of an element.protected voidwrite(Font font, java.util.Properties styleAttributes)Writes the representation of aFont.protected voidwriteComment(java.lang.String comment)Writes some comment.protected voidwriteCssProperty(java.lang.String prop, java.lang.String value)Writes out a CSS property.protected voidwriteHeader(Meta meta)Writes a Metatag in the header.protected voidwriteJavaScript(Header header)Writes a JavaScript section or, if the markup attribute HtmlTags.URL is set, a JavaScript reference in the header.protected voidwriteLink(Header header)Writes a link in the header.protected voidwriteSection(Section section, int indent)Writes the HTML representation of a section.-
Methods inherited from class com.itextpdf.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 Detail
-
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 java.lang.String NBSP
This is a possible HTML-tag.- See Also:
- Constant Field Values
-
currentfont
protected java.util.Stack currentfont
This is the current font of the HTML.
-
standardfont
protected Font standardfont
This is the standard font of the HTML.
-
imagepath
protected java.lang.String imagepath
This is a path for images.
-
pageN
protected int pageN
Stores the page number.
-
header
protected HeaderFooter header
This is the textual part of a header
-
footer
protected HeaderFooter footer
This is the textual part of the footer
-
markup
protected java.util.Properties markup
Store the markup properties of a MarkedObject.
-
-
Constructor Detail
-
HtmlWriter
protected HtmlWriter(Document doc, java.io.OutputStream os)
Constructs aHtmlWriter.- Parameters:
doc- TheDocumentthat has to be written as HTMLos- TheOutputStreamthe writer has to write to.
-
-
Method Detail
-
getInstance
public static HtmlWriter getInstance(Document document, java.io.OutputStream os)
Gets an instance of theHtmlWriter.- Parameters:
document- TheDocumentthat has to be writtenos- TheOutputStreamthe writer has to write to.- Returns:
- a new
HtmlWriter
-
newPage
public boolean newPage()
Signals that an new page has to be started.- Specified by:
newPagein interfaceDocListener- Overrides:
newPagein classDocWriter- Returns:
trueif this action succeeded,falseif not.
-
add
public boolean add(Element element) throws DocumentException
Signals that anElementwas added to theDocument.- Specified by:
addin interfaceElementListener- Overrides:
addin classDocWriter- Parameters:
element- a high level object that has to be translated to HTML- 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()
Signals that theDocumenthas been opened and thatElementscan be added.The
HEAD-section of the HTML-document is written.- Specified by:
openin interfaceDocListener- Overrides:
openin classDocWriter
-
close
public void close()
Signals that theDocumentwas closed and that no otherElementswill be added.- Specified by:
closein interfaceDocListener- Overrides:
closein classDocWriter
-
initHeader
protected void initHeader()
Adds the header to the top of the Document
-
initFooter
protected void initFooter()
Adds the header to the top of the Document
-
writeHeader
protected void writeHeader(Meta meta) throws java.io.IOException
Writes a Metatag in the header.- Parameters:
meta- the element that has to be written- Throws:
java.io.IOException
-
writeLink
protected void writeLink(Header header) throws java.io.IOException
Writes a link in the header.- Parameters:
header- the element that has to be written- Throws:
java.io.IOException
-
writeJavaScript
protected void writeJavaScript(Header header) throws java.io.IOException
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:
java.io.IOException
-
writeComment
protected void writeComment(java.lang.String comment) throws java.io.IOExceptionWrites some comment.This method writes some comment.
- Parameters:
comment- the comment that has to be written- Throws:
java.io.IOException
-
setStandardFont
public void setStandardFont(Font standardfont)
Changes the standardfont.- Parameters:
standardfont- The font
-
isOtherFont
public boolean isOtherFont(Font font)
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
public void setImagepath(java.lang.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.
- Parameters:
imagepath- the new imagepath
-
resetImagepath
public void resetImagepath()
Resets the imagepath.
-
setHeader
public void setHeader(HeaderFooter header)
Changes the header of this document.- Specified by:
setHeaderin interfaceDocListener- Overrides:
setHeaderin classDocWriter- Parameters:
header- the new header
-
setFooter
public void setFooter(HeaderFooter footer)
Changes the footer of this document.- Specified by:
setFooterin interfaceDocListener- Overrides:
setFooterin classDocWriter- Parameters:
footer- the new footer
-
add
public boolean add(java.lang.String string)
Signals that aStringwas added to theDocument.- Parameters:
string- a String to add to the HTML- Returns:
trueif the string was added,falseif not.
-
write
protected void write(Element element, int indent) throws java.io.IOException
Writes the HTML representation of an element.- Parameters:
element- the elementindent- the indentation- Throws:
java.io.IOException
-
writeSection
protected void writeSection(Section section, int indent) throws java.io.IOException
Writes the HTML representation of a section.- Parameters:
section- the section to writeindent- the indentation- Throws:
java.io.IOException
-
write
protected void write(Font font, java.util.Properties styleAttributes) throws java.io.IOException
Writes the representation of aFont.- Parameters:
font- aFontstyleAttributes- the style of the font- Throws:
java.io.IOException
-
writeCssProperty
protected void writeCssProperty(java.lang.String prop, java.lang.String value) throws java.io.IOExceptionWrites out a CSS property.- Parameters:
prop- a CSS propertyvalue- the value of the CSS property- Throws:
java.io.IOException
-
-