use of com.lowagie.text.Element in project OpenPDF by LibrePDF.
the class HTMLWorker method endDocument.
public void endDocument() {
try {
stack.forEach(o -> document.add((Element) o));
if (currentParagraph != null) {
document.add(currentParagraph);
}
currentParagraph = null;
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
use of com.lowagie.text.Element in project OpenPDF by LibrePDF.
the class HTMLWorker method endElement.
public void endElement(String tag) {
if (!tagsSupported.containsKey(tag))
return;
try {
String follow = FactoryProperties.followTags.get(tag);
if (follow != null) {
cprops.removeChain(follow);
return;
}
if (tag.equals("font") || tag.equals("span")) {
cprops.removeChain(tag);
return;
}
if (tag.equals("a")) {
if (currentParagraph == null) {
currentParagraph = new Paragraph();
}
boolean skip = false;
if (interfaceProps != null) {
ALink i = (ALink) interfaceProps.get("alink_interface");
if (i != null)
skip = i.process(currentParagraph, cprops);
}
if (!skip) {
cprops.findProperty("href").ifPresent(href -> {
List<Element> chunks = currentParagraph.getChunks();
for (Element chunk : chunks) {
Chunk ck = (Chunk) chunk;
ck.setAnchor(href);
}
});
}
Paragraph tmp = (Paragraph) stack.pop();
Phrase tmp2 = new Phrase();
tmp2.add(currentParagraph);
tmp.add(tmp2);
currentParagraph = tmp;
cprops.removeChain("a");
return;
}
if (tag.equals("br")) {
return;
}
if (currentParagraph != null) {
if (stack.empty())
document.add(currentParagraph);
else {
Object obj = stack.pop();
if (obj instanceof TextElementArray) {
TextElementArray current = (TextElementArray) obj;
current.add(currentParagraph);
}
stack.push(obj);
}
}
currentParagraph = null;
if (tag.equals(HtmlTags.UNORDEREDLIST) || tag.equals(HtmlTags.ORDEREDLIST)) {
if (pendingLI)
endElement(HtmlTags.LISTITEM);
skipText = false;
cprops.removeChain(tag);
if (stack.empty())
return;
Object obj = stack.pop();
if (!(obj instanceof com.lowagie.text.List)) {
stack.push(obj);
return;
}
if (stack.empty()) {
document.add((Element) obj);
} else {
Object peek = stack.peek();
if (peek instanceof com.lowagie.text.List) {
((com.lowagie.text.List) peek).add((com.lowagie.text.List) obj);
} else {
((TextElementArray) peek).add((Element) obj);
}
}
return;
}
if (tag.equals(HtmlTags.LISTITEM)) {
pendingLI = false;
skipText = true;
cprops.removeChain(tag);
if (stack.empty())
return;
Object obj = stack.pop();
if (!(obj instanceof ListItem)) {
stack.push(obj);
return;
}
if (stack.empty()) {
document.add((Element) obj);
return;
}
Object list = stack.pop();
if (!(list instanceof com.lowagie.text.List)) {
stack.push(list);
return;
}
ListItem item = (ListItem) obj;
((com.lowagie.text.List) list).add(item);
List<Element> cks = item.getChunks();
if (!cks.isEmpty())
item.getListSymbol().setFont(((Chunk) cks.get(0)).getFont());
stack.push(list);
return;
}
if (tag.equals("div") || tag.equals("body")) {
cprops.removeChain(tag);
return;
}
if (tag.equals(HtmlTags.PRE)) {
cprops.removeChain(tag);
isPRE = false;
return;
}
if (tag.equals("p")) {
cprops.removeChain(tag);
return;
}
if (tag.equals("h1") || tag.equals("h2") || tag.equals("h3") || tag.equals("h4") || tag.equals("h5") || tag.equals("h6")) {
cprops.removeChain(tag);
return;
}
if (tag.equals("table")) {
if (pendingTR)
endElement("tr");
cprops.removeChain("table");
IncTable table = (IncTable) stack.pop();
PdfPTable tb = table.buildTable();
tb.setSplitRows(true);
if (stack.empty())
document.add(tb);
else
((TextElementArray) stack.peek()).add(tb);
boolean[] state = (boolean[]) tableState.pop();
pendingTR = state[0];
pendingTD = state[1];
skipText = false;
return;
}
if (tag.equals("tr")) {
if (pendingTD)
endElement("td");
pendingTR = false;
cprops.removeChain("tr");
List<PdfPCell> cells = new ArrayList<>();
IncTable table;
while (true) {
Object obj = stack.pop();
if (obj instanceof IncCell) {
cells.add(((IncCell) obj).getCell());
}
if (obj instanceof IncTable) {
table = (IncTable) obj;
break;
}
}
table.addCols(cells);
table.endRow();
stack.push(table);
skipText = true;
return;
}
if (tag.equals("td") || tag.equals("th")) {
pendingTD = false;
cprops.removeChain("td");
skipText = true;
return;
}
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
use of com.lowagie.text.Element in project OpenPDF by LibrePDF.
the class PdfCell method addList.
private void addList(List list, float left, float right, int alignment) {
PdfChunk chunk;
PdfChunk overflow;
java.util.List<PdfAction> allActions = new ArrayList<>();
processActions(list, null, allActions);
int aCounter = 0;
for (Object o1 : list.getItems()) {
Element ele = (Element) o1;
switch(ele.type()) {
case Element.LISTITEM:
ListItem item = (ListItem) ele;
line = new PdfLine(left + item.getIndentationLeft(), right, alignment, item.getLeading());
line.setListItem(item);
for (Object o : item.getChunks()) {
chunk = new PdfChunk((Chunk) o, allActions.get(aCounter++));
while ((overflow = line.add(chunk)) != null) {
addLine(line);
line = new PdfLine(left + item.getIndentationLeft(), right, alignment, item.getLeading());
chunk = overflow;
}
line.resetAlignment();
addLine(line);
line = new PdfLine(left + item.getIndentationLeft(), right, alignment, leading);
}
break;
case Element.LIST:
List sublist = (List) ele;
addList(sublist, left + sublist.getIndentationLeft(), right, alignment);
break;
}
}
}
use of com.lowagie.text.Element in project OpenPDF by LibrePDF.
the class SAXiTextHandler method addImage.
protected void addImage(Image img) throws EmptyStackException {
// if there is an element on the stack...
Element current = stack.pop();
// added directly
if (current instanceof Section || current instanceof Cell) {
((TextElementArray) current).add(img);
stack.push(current);
} else // ... if it is a Phrase, we have to wrap the Image in a new Chunk
if (current instanceof Phrase) {
((TextElementArray) current).add(new Chunk(img, 0, 0));
stack.push(current);
} else // ...if not, we need to to a lot of stuff
{
Stack<Element> newStack = new Stack<>();
while (!(current instanceof Section || current instanceof Cell)) {
newStack.push(current);
if (current instanceof Anchor) {
img.setAnnotation(new Annotation(0, 0, 0, 0, ((Anchor) current).getReference()));
}
current = stack.pop();
}
((TextElementArray) current).add(img);
stack.push(current);
while (!newStack.empty()) {
stack.push(newStack.pop());
}
}
}
use of com.lowagie.text.Element 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();
}
}
Aggregations