use of com.lowagie.text.Meta in project itext2 by albfernandez.
the class RtfDestinationInfo method handleCloseGroup.
/* (non-Javadoc)
* @see com.lowagie.text.rtf.direct.RtfDestination#handleGroupEnd()
*/
public boolean handleCloseGroup() {
if (this.text.length() > 0) {
Document doc = this.rtfParser.getDocument();
if (doc != null) {
if (this.elementName.equals("author")) {
doc.addAuthor(this.text);
}
if (this.elementName.equals("title")) {
doc.addTitle(this.text);
}
if (this.elementName.equals("subject")) {
doc.addSubject(this.text);
}
} else {
RtfDocument rtfDoc = this.rtfParser.getRtfDocument();
if (rtfDoc != null) {
if (this.elementName.equals("author")) {
Meta meta = new Meta(this.elementName, this.text);
RtfInfoElement elem = new RtfInfoElement(rtfDoc, meta);
rtfDoc.getDocumentHeader().addInfoElement(elem);
}
if (this.elementName.equals("title")) {
Meta meta = new Meta(this.elementName, this.text);
RtfInfoElement elem = new RtfInfoElement(rtfDoc, meta);
rtfDoc.getDocumentHeader().addInfoElement(elem);
}
if (this.elementName.equals("subject")) {
Meta meta = new Meta(this.elementName, this.text);
RtfInfoElement elem = new RtfInfoElement(rtfDoc, meta);
rtfDoc.getDocumentHeader().addInfoElement(elem);
}
}
}
this.setToDefaults();
}
return true;
}
use of com.lowagie.text.Meta in project itext2 by albfernandez.
the class HtmlWriter method add.
/**
* Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
*
* @param element a high level object that has to be translated to HTML
* @return <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not.
* @throws DocumentException when a document isn't open yet, or has been closed
*/
public boolean add(Element element) throws DocumentException {
if (pause) {
return false;
}
if (open && !element.isContent()) {
throw new DocumentException("The document is open; you can only add Elements with content.");
}
try {
switch(element.type()) {
case Element.HEADER:
try {
Header h = (Header) element;
if (HtmlTags.STYLESHEET.equals(h.getName())) {
writeLink(h);
} else if (HtmlTags.JAVASCRIPT.equals(h.getName())) {
writeJavaScript(h);
} else {
writeHeader(h);
}
} catch (ClassCastException cce) {
}
return true;
case Element.SUBJECT:
case Element.KEYWORDS:
case Element.AUTHOR:
Meta meta = (Meta) element;
writeHeader(meta);
return true;
case Element.TITLE:
addTabs(2);
writeStart(HtmlTags.TITLE);
os.write(GT);
addTabs(3);
write(HtmlEncoder.encode(((Meta) element).getContent()));
addTabs(2);
writeEnd(HtmlTags.TITLE);
return true;
case Element.CREATOR:
writeComment("Creator: " + HtmlEncoder.encode(((Meta) element).getContent()));
return true;
case Element.PRODUCER:
writeComment("Producer: " + HtmlEncoder.encode(((Meta) element).getContent()));
return true;
case Element.CREATIONDATE:
writeComment("Creationdate: " + HtmlEncoder.encode(((Meta) element).getContent()));
return true;
case Element.MARKED:
if (element instanceof MarkedSection) {
MarkedSection ms = (MarkedSection) element;
addTabs(1);
writeStart(HtmlTags.DIV);
writeMarkupAttributes(ms.getMarkupAttributes());
os.write(GT);
MarkedObject mo = ((MarkedSection) element).getTitle();
if (mo != null) {
markup = mo.getMarkupAttributes();
mo.process(this);
}
ms.process(this);
writeEnd(HtmlTags.DIV);
return true;
} else {
MarkedObject mo = (MarkedObject) element;
markup = mo.getMarkupAttributes();
return mo.process(this);
}
default:
write(element, 2);
return true;
}
} catch (IOException ioe) {
throw new ExceptionConverter(ioe);
}
}
use of com.lowagie.text.Meta in project OpenPDF by LibrePDF.
the class HtmlWriter method add.
/**
* Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
*
* @param element a high level object that has to be translated to HTML
* @return <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not.
* @throws DocumentException when a document isn't open yet, or has been closed
*/
@Override
public boolean add(Element element) throws DocumentException {
if (pause) {
return false;
}
if (open && !element.isContent()) {
throw new DocumentException(getComposedMessage("the.document.is.open.you.can.only.add.elements.with.content"));
}
try {
switch(element.type()) {
case Element.HEADER:
try {
Header header = (Header) element;
if (HtmlTags.STYLESHEET.equals(header.getName())) {
writeLink(header);
} else if (HtmlTags.JAVASCRIPT.equals(header.getName())) {
writeJavaScript(header);
} else {
writeHeader(header);
}
} catch (ClassCastException ignored) {
}
return true;
case Element.SUBJECT:
case Element.KEYWORDS:
case Element.AUTHOR:
Meta meta = (Meta) element;
writeHeader(meta);
return true;
case Element.TITLE:
addTabs(2);
writeStart(HtmlTags.TITLE);
os.write(GT);
addTabs(3);
write(HtmlEncoder.encode(((Meta) element).getContent()));
addTabs(2);
writeEnd(HtmlTags.TITLE);
return true;
case Element.CREATOR:
writeComment("Creator: " + HtmlEncoder.encode(((Meta) element).getContent()));
return true;
case Element.PRODUCER:
writeComment("Producer: " + HtmlEncoder.encode(((Meta) element).getContent()));
return true;
case Element.CREATIONDATE:
writeComment("Creationdate: " + HtmlEncoder.encode(((Meta) element).getContent()));
return true;
case Element.MARKED:
if (element instanceof MarkedSection) {
MarkedSection ms = (MarkedSection) element;
addTabs(1);
writeStart(HtmlTags.DIV);
writeMarkupAttributes(ms.getMarkupAttributes());
os.write(GT);
MarkedObject mo = ((MarkedSection) element).getTitle();
if (mo != null) {
markup = mo.getMarkupAttributes();
mo.process(this);
}
ms.process(this);
writeEnd(HtmlTags.DIV);
return true;
} else {
MarkedObject mo = (MarkedObject) element;
markup = mo.getMarkupAttributes();
return mo.process(this);
}
default:
write(element, 2);
return true;
}
} catch (IOException ioe) {
throw new ExceptionConverter(ioe);
}
}
use of com.lowagie.text.Meta in project OpenPDF by LibrePDF.
the class SAXiTextHandler method handleStartingTags.
/**
* This method deals with the starting tags.
*
* @param name the name of the tag
* @param attributes the list of attributes
*/
public void handleStartingTags(String name, Properties attributes) {
if (ignore || ElementTags.IGNORE.equals(name)) {
ignore = true;
return;
}
// maybe there is some meaningful data that wasn't between tags
if (currentChunk != null && isNotBlank(currentChunk.getContent())) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
} catch (EmptyStackException ese) {
if (bf == null) {
current = new Paragraph("", new Font());
} else {
current = new Paragraph("", new Font(this.bf));
}
}
current.add(currentChunk);
stack.push(current);
currentChunk = null;
}
// chunks
if (ElementTags.CHUNK.equals(name)) {
currentChunk = ElementFactory.getChunk(attributes);
if (bf != null) {
currentChunk.setFont(new Font(this.bf));
}
return;
}
// symbols
if (ElementTags.ENTITY.equals(name)) {
Font f = new Font();
if (currentChunk != null) {
handleEndingTags(ElementTags.CHUNK);
f = currentChunk.getFont();
}
currentChunk = EntitiesToSymbol.get(attributes.getProperty(ElementTags.ID), f);
return;
}
// phrases
if (ElementTags.PHRASE.equals(name)) {
stack.push(ElementFactory.getPhrase(attributes));
return;
}
// anchors
if (ElementTags.ANCHOR.equals(name)) {
stack.push(ElementFactory.getAnchor(attributes));
return;
}
// paragraphs and titles
if (ElementTags.PARAGRAPH.equals(name) || ElementTags.TITLE.equals(name)) {
stack.push(ElementFactory.getParagraph(attributes));
return;
}
// lists
if (ElementTags.LIST.equals(name)) {
stack.push(ElementFactory.getList(attributes));
return;
}
// listitems
if (ElementTags.LISTITEM.equals(name)) {
stack.push(ElementFactory.getListItem(attributes));
return;
}
// cells
if (ElementTags.CELL.equals(name)) {
stack.push(ElementFactory.getCell(attributes));
return;
}
// tables
if (ElementTags.TABLE.equals(name)) {
Table table = ElementFactory.getTable(attributes);
float[] widths = table.getProportionalWidths();
for (int i = 0; i < widths.length; i++) {
if (widths[i] == 0) {
widths[i] = 100.0f / widths.length;
}
}
try {
table.setWidths(widths);
} catch (BadElementException bee) {
// this shouldn't happen
throw new ExceptionConverter(bee);
}
stack.push(table);
return;
}
// sections
if (ElementTags.SECTION.equals(name)) {
Element previous = stack.pop();
Section section;
try {
section = ElementFactory.getSection((Section) previous, attributes);
} catch (ClassCastException cce) {
throw new ExceptionConverter(cce);
}
stack.push(previous);
stack.push(section);
return;
}
// chapters
if (ElementTags.CHAPTER.equals(name)) {
stack.push(ElementFactory.getChapter(attributes));
return;
}
// images
if (ElementTags.IMAGE.equals(name)) {
try {
Image img = ElementFactory.getImage(attributes);
try {
addImage(img);
return;
} catch (EmptyStackException ese) {
// to the document
try {
document.add(img);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
return;
}
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
// annotations
if (ElementTags.ANNOTATION.equals(name)) {
Annotation annotation = ElementFactory.getAnnotation(attributes);
TextElementArray current;
try {
try {
current = (TextElementArray) stack.pop();
try {
current.add(annotation);
} catch (Exception e) {
document.add(annotation);
}
stack.push(current);
} catch (EmptyStackException ese) {
document.add(annotation);
}
return;
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
// newlines
if (isNewline(name)) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
current.add(Chunk.NEWLINE);
stack.push(current);
} catch (EmptyStackException ese) {
if (currentChunk == null) {
try {
document.add(Chunk.NEWLINE);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
} else {
currentChunk.append("\n");
}
}
return;
}
// newpage
if (isNewpage(name)) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
Chunk newPage = new Chunk("");
newPage.setNewPage();
if (bf != null) {
newPage.setFont(new Font(this.bf));
}
current.add(newPage);
stack.push(current);
} catch (EmptyStackException ese) {
document.newPage();
}
return;
}
if (ElementTags.HORIZONTALRULE.equals(name)) {
TextElementArray current;
LineSeparator hr = new LineSeparator(1.0f, 100.0f, null, Element.ALIGN_CENTER, 0);
try {
current = (TextElementArray) stack.pop();
current.add(hr);
stack.push(current);
} catch (EmptyStackException ese) {
try {
document.add(hr);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
return;
}
// documentroot
if (isDocumentRoot(name)) {
String key;
String value;
// pagesize and orientation specific code suggested by Samuel Gabriel
// Updated by Ricardo Coutinho. Only use if set in html!
Rectangle pageSize = null;
String orientation = null;
for (Object o : attributes.keySet()) {
key = (String) o;
value = attributes.getProperty(key);
try {
// margin specific code suggested by Reza Nasiri
if (ElementTags.LEFT.equalsIgnoreCase(key))
leftMargin = Float.parseFloat(value + "f");
if (ElementTags.RIGHT.equalsIgnoreCase(key))
rightMargin = Float.parseFloat(value + "f");
if (ElementTags.TOP.equalsIgnoreCase(key))
topMargin = Float.parseFloat(value + "f");
if (ElementTags.BOTTOM.equalsIgnoreCase(key))
bottomMargin = Float.parseFloat(value + "f");
} catch (Exception ex) {
throw new ExceptionConverter(ex);
}
if (ElementTags.PAGE_SIZE.equals(key)) {
try {
Field pageSizeField = PageSize.class.getField(value);
pageSize = (Rectangle) pageSizeField.get(null);
} catch (Exception ex) {
throw new ExceptionConverter(ex);
}
} else if (ElementTags.ORIENTATION.equals(key)) {
try {
if ("landscape".equals(value)) {
orientation = "landscape";
}
} catch (Exception ex) {
throw new ExceptionConverter(ex);
}
} else {
try {
document.add(new Meta(key, value));
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
if (pageSize != null) {
if ("landscape".equals(orientation)) {
pageSize = pageSize.rotate();
}
document.setPageSize(pageSize);
}
document.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);
if (controlOpenClose)
document.open();
}
}
use of com.lowagie.text.Meta in project itext2 by albfernandez.
the class SAXiTextHandler method handleStartingTags.
/**
* This method deals with the starting tags.
*
* @param name
* the name of the tag
* @param attributes
* the list of attributes
*/
public void handleStartingTags(String name, Properties attributes) {
// System.err.println("Start: " + name);
if (ignore || ElementTags.IGNORE.equals(name)) {
ignore = true;
return;
}
// maybe there is some meaningful data that wasn't between tags
if (currentChunk != null) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
} catch (EmptyStackException ese) {
if (bf == null) {
current = new Paragraph("", new Font());
} else {
current = new Paragraph("", new Font(this.bf));
}
}
current.add(currentChunk);
stack.push(current);
currentChunk = null;
}
// chunks
if (ElementTags.CHUNK.equals(name)) {
currentChunk = ElementFactory.getChunk(attributes);
if (bf != null) {
currentChunk.setFont(new Font(this.bf));
}
return;
}
// symbols
if (ElementTags.ENTITY.equals(name)) {
Font f = new Font();
if (currentChunk != null) {
handleEndingTags(ElementTags.CHUNK);
f = currentChunk.getFont();
}
currentChunk = EntitiesToSymbol.get(attributes.getProperty(ElementTags.ID), f);
return;
}
// phrases
if (ElementTags.PHRASE.equals(name)) {
stack.push(ElementFactory.getPhrase(attributes));
return;
}
// anchors
if (ElementTags.ANCHOR.equals(name)) {
stack.push(ElementFactory.getAnchor(attributes));
return;
}
// paragraphs and titles
if (ElementTags.PARAGRAPH.equals(name) || ElementTags.TITLE.equals(name)) {
stack.push(ElementFactory.getParagraph(attributes));
return;
}
// lists
if (ElementTags.LIST.equals(name)) {
stack.push(ElementFactory.getList(attributes));
return;
}
// listitems
if (ElementTags.LISTITEM.equals(name)) {
stack.push(ElementFactory.getListItem(attributes));
return;
}
// cells
if (ElementTags.CELL.equals(name)) {
stack.push(ElementFactory.getCell(attributes));
return;
}
// tables
if (ElementTags.TABLE.equals(name)) {
Table table = ElementFactory.getTable(attributes);
float[] widths = table.getProportionalWidths();
for (int i = 0; i < widths.length; i++) {
if (widths[i] == 0) {
widths[i] = 100.0f / widths.length;
}
}
try {
table.setWidths(widths);
} catch (BadElementException bee) {
// this shouldn't happen
throw new ExceptionConverter(bee);
}
stack.push(table);
return;
}
// sections
if (ElementTags.SECTION.equals(name)) {
Element previous = (Element) stack.pop();
Section section;
try {
section = ElementFactory.getSection((Section) previous, attributes);
} catch (ClassCastException cce) {
throw new ExceptionConverter(cce);
}
stack.push(previous);
stack.push(section);
return;
}
// chapters
if (ElementTags.CHAPTER.equals(name)) {
stack.push(ElementFactory.getChapter(attributes));
return;
}
// images
if (ElementTags.IMAGE.equals(name)) {
try {
Image img = ElementFactory.getImage(attributes);
try {
addImage(img);
return;
} catch (EmptyStackException ese) {
// to the document
try {
document.add(img);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
return;
}
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
// annotations
if (ElementTags.ANNOTATION.equals(name)) {
Annotation annotation = ElementFactory.getAnnotation(attributes);
TextElementArray current;
try {
try {
current = (TextElementArray) stack.pop();
try {
current.add(annotation);
} catch (Exception e) {
document.add(annotation);
}
stack.push(current);
} catch (EmptyStackException ese) {
document.add(annotation);
}
return;
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
// newlines
if (isNewline(name)) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
current.add(Chunk.NEWLINE);
stack.push(current);
} catch (EmptyStackException ese) {
if (currentChunk == null) {
try {
document.add(Chunk.NEWLINE);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
} else {
currentChunk.append("\n");
}
}
return;
}
// newpage
if (isNewpage(name)) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
Chunk newPage = new Chunk("");
newPage.setNewPage();
if (bf != null) {
newPage.setFont(new Font(this.bf));
}
current.add(newPage);
stack.push(current);
} catch (EmptyStackException ese) {
document.newPage();
}
return;
}
if (ElementTags.HORIZONTALRULE.equals(name)) {
TextElementArray current;
LineSeparator hr = new LineSeparator(1.0f, 100.0f, null, Element.ALIGN_CENTER, 0);
try {
current = (TextElementArray) stack.pop();
current.add(hr);
stack.push(current);
} catch (EmptyStackException ese) {
try {
document.add(hr);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
return;
}
// documentroot
if (isDocumentRoot(name)) {
String key;
String value;
// pagesize and orientation specific code suggested by Samuel Gabriel
// Updated by Ricardo Coutinho. Only use if set in html!
Rectangle pageSize = null;
String orientation = null;
for (Iterator i = attributes.keySet().iterator(); i.hasNext(); ) {
key = (String) i.next();
value = attributes.getProperty(key);
try {
// margin specific code suggested by Reza Nasiri
if (ElementTags.LEFT.equalsIgnoreCase(key))
leftMargin = Float.parseFloat(value + "f");
if (ElementTags.RIGHT.equalsIgnoreCase(key))
rightMargin = Float.parseFloat(value + "f");
if (ElementTags.TOP.equalsIgnoreCase(key))
topMargin = Float.parseFloat(value + "f");
if (ElementTags.BOTTOM.equalsIgnoreCase(key))
bottomMargin = Float.parseFloat(value + "f");
} catch (Exception ex) {
throw new ExceptionConverter(ex);
}
if (ElementTags.PAGE_SIZE.equals(key)) {
try {
String pageSizeName = value;
Field pageSizeField = PageSize.class.getField(pageSizeName);
pageSize = (Rectangle) pageSizeField.get(null);
} catch (Exception ex) {
throw new ExceptionConverter(ex);
}
} else if (ElementTags.ORIENTATION.equals(key)) {
try {
if ("landscape".equals(value)) {
orientation = "landscape";
}
} catch (Exception ex) {
throw new ExceptionConverter(ex);
}
} else {
try {
document.add(new Meta(key, value));
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
if (pageSize != null) {
if ("landscape".equals(orientation)) {
pageSize = pageSize.rotate();
}
document.setPageSize(pageSize);
}
document.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);
if (controlOpenClose)
document.open();
}
}
Aggregations