Search in sources :

Example 1 with MarkedObject

use of com.lowagie.text.MarkedObject 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);
    }
}
Also used : ExceptionConverter(com.lowagie.text.ExceptionConverter) Meta(com.lowagie.text.Meta) Header(com.lowagie.text.Header) MarkedSection(com.lowagie.text.MarkedSection) DocumentException(com.lowagie.text.DocumentException) MarkedObject(com.lowagie.text.MarkedObject) IOException(java.io.IOException)

Example 2 with MarkedObject

use of com.lowagie.text.MarkedObject in project OpenPDF by LibrePDF.

the class PdfDocument method add.

/**
 * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
 *
 * @param element the element to add
 * @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 (writer != null && writer.isPaused()) {
        return false;
    }
    try {
        switch(element.type()) {
            // Information (headers)
            case Element.HEADER:
                info.addkey(((Meta) element).getName(), ((Meta) element).getContent());
                break;
            case Element.TITLE:
                info.addTitle(((Meta) element).getContent());
                break;
            case Element.SUBJECT:
                info.addSubject(((Meta) element).getContent());
                break;
            case Element.KEYWORDS:
                info.addKeywords(((Meta) element).getContent());
                break;
            case Element.AUTHOR:
                info.addAuthor(((Meta) element).getContent());
                break;
            case Element.CREATOR:
                info.addCreator(((Meta) element).getContent());
                break;
            case Element.PRODUCER:
                info.addProducer(((Meta) element).getContent());
                break;
            case Element.CREATIONDATE:
                // you can not set the creation date, only reset it
                info.addCreationDate();
                break;
            // content (text)
            case Element.CHUNK:
                {
                    // if there isn't a current line available, we make one
                    if (line == null) {
                        carriageReturn();
                    }
                    // we cast the element to a chunk
                    PdfChunk chunk = new PdfChunk((Chunk) element, anchorAction);
                    // we try to add the chunk to the line, until we succeed
                    {
                        PdfChunk overflow;
                        while ((overflow = line.add(chunk)) != null) {
                            carriageReturn();
                            chunk = overflow;
                            chunk.trimFirstSpace();
                        }
                    }
                    pageEmpty = false;
                    if (chunk.isAttribute(Chunk.NEWPAGE)) {
                        newPage();
                    }
                    break;
                }
            case Element.ANCHOR:
                {
                    leadingCount++;
                    Anchor anchor = (Anchor) element;
                    String url = anchor.getReference();
                    leading = anchor.getLeading();
                    if (url != null) {
                        anchorAction = new PdfAction(url);
                    }
                    // we process the element
                    element.process(this);
                    anchorAction = null;
                    leadingCount--;
                    break;
                }
            case Element.ANNOTATION:
                {
                    if (line == null) {
                        carriageReturn();
                    }
                    Annotation annot = (Annotation) element;
                    Rectangle rect = new Rectangle(0, 0);
                    if (line != null)
                        rect = new Rectangle(annot.llx(indentRight() - line.widthLeft()), annot.ury(indentTop() - currentHeight - 20), annot.urx(indentRight() - line.widthLeft() + 20), annot.lly(indentTop() - currentHeight));
                    PdfAnnotation an = PdfAnnotationsImp.convertAnnotation(writer, annot, rect);
                    annotationsImp.addPlainAnnotation(an);
                    pageEmpty = false;
                    break;
                }
            case Element.PHRASE:
                {
                    leadingCount++;
                    // we cast the element to a phrase and set the leading of the document
                    leading = ((Phrase) element).getLeading();
                    // we process the element
                    element.process(this);
                    leadingCount--;
                    break;
                }
            case Element.PARAGRAPH:
                {
                    leadingCount++;
                    // we cast the element to a paragraph
                    Paragraph paragraph = (Paragraph) element;
                    addSpacing(paragraph.getSpacingBefore(), leading, paragraph.getFont());
                    // we adjust the parameters of the document
                    alignment = paragraph.getAlignment();
                    leading = paragraph.getTotalLeading();
                    carriageReturn();
                    // we don't want to make orphans/widows
                    if (currentHeight + line.height() + leading > indentTop() - indentBottom()) {
                        newPage();
                    }
                    indentation.indentLeft += paragraph.getIndentationLeft();
                    indentation.indentRight += paragraph.getIndentationRight();
                    carriageReturn();
                    PdfPageEvent pageEvent = writer.getPageEvent();
                    if (pageEvent != null && !isSectionTitle)
                        pageEvent.onParagraph(writer, this, indentTop() - currentHeight);
                    // if a paragraph has to be kept together, we wrap it in a table object
                    if (paragraph.getKeepTogether()) {
                        carriageReturn();
                        // fixes bug with nested tables not shown
                        // Paragraph#getChunks() doesn't contain the nested table element
                        PdfPTable table = createInOneCell(paragraph);
                        indentation.indentLeft -= paragraph.getIndentationLeft();
                        indentation.indentRight -= paragraph.getIndentationRight();
                        this.add(table);
                        indentation.indentLeft += paragraph.getIndentationLeft();
                        indentation.indentRight += paragraph.getIndentationRight();
                    } else {
                        line.setExtraIndent(paragraph.getFirstLineIndent());
                        element.process(this);
                        carriageReturn();
                        addSpacing(paragraph.getSpacingAfter(), paragraph.getTotalLeading(), paragraph.getFont());
                    }
                    if (pageEvent != null && !isSectionTitle)
                        pageEvent.onParagraphEnd(writer, this, indentTop() - currentHeight);
                    alignment = Element.ALIGN_LEFT;
                    indentation.indentLeft -= paragraph.getIndentationLeft();
                    indentation.indentRight -= paragraph.getIndentationRight();
                    carriageReturn();
                    leadingCount--;
                    break;
                }
            case Element.SECTION:
            case Element.CHAPTER:
                {
                    // Chapters and Sections only differ in their constructor
                    // so we cast both to a Section
                    Section section = (Section) element;
                    PdfPageEvent pageEvent = writer.getPageEvent();
                    boolean hasTitle = section.isNotAddedYet() && section.getTitle() != null;
                    // if the section is a chapter, we begin a new page
                    if (section.isTriggerNewPage()) {
                        newPage();
                    }
                    if (hasTitle) {
                        float fith = indentTop() - currentHeight;
                        int rotation = pageSize.getRotation();
                        if (rotation == 90 || rotation == 180)
                            fith = pageSize.getHeight() - fith;
                        PdfDestination destination = new PdfDestination(PdfDestination.FITH, fith);
                        while (currentOutline.level() >= section.getDepth()) {
                            currentOutline = currentOutline.parent();
                        }
                        PdfOutline outline = new PdfOutline(currentOutline, destination, section.getBookmarkTitle(), section.isBookmarkOpen());
                        currentOutline = outline;
                    }
                    // some values are set
                    carriageReturn();
                    indentation.sectionIndentLeft += section.getIndentationLeft();
                    indentation.sectionIndentRight += section.getIndentationRight();
                    if (section.isNotAddedYet() && pageEvent != null)
                        if (element.type() == Element.CHAPTER)
                            pageEvent.onChapter(writer, this, indentTop() - currentHeight, section.getTitle());
                        else
                            pageEvent.onSection(writer, this, indentTop() - currentHeight, section.getDepth(), section.getTitle());
                    // the title of the section (if any has to be printed)
                    if (hasTitle) {
                        isSectionTitle = true;
                        add(section.getTitle());
                        isSectionTitle = false;
                    }
                    indentation.sectionIndentLeft += section.getIndentation();
                    // we process the section
                    element.process(this);
                    flushLines();
                    // some parameters are set back to normal again
                    indentation.sectionIndentLeft -= (section.getIndentationLeft() + section.getIndentation());
                    indentation.sectionIndentRight -= section.getIndentationRight();
                    if (section.isComplete() && pageEvent != null)
                        if (element.type() == Element.CHAPTER)
                            pageEvent.onChapterEnd(writer, this, indentTop() - currentHeight);
                        else
                            pageEvent.onSectionEnd(writer, this, indentTop() - currentHeight);
                    break;
                }
            case Element.LIST:
                {
                    // we cast the element to a List
                    List list = (List) element;
                    if (list.isAlignindent()) {
                        list.normalizeIndentation();
                    }
                    // we adjust the document
                    indentation.listIndentLeft += list.getIndentationLeft();
                    indentation.indentRight += list.getIndentationRight();
                    // we process the items in the list
                    element.process(this);
                    // some parameters are set back to normal again
                    indentation.listIndentLeft -= list.getIndentationLeft();
                    indentation.indentRight -= list.getIndentationRight();
                    carriageReturn();
                    break;
                }
            case Element.LISTITEM:
                {
                    leadingCount++;
                    // we cast the element to a ListItem
                    ListItem listItem = (ListItem) element;
                    addSpacing(listItem.getSpacingBefore(), leading, listItem.getFont());
                    // we adjust the document
                    alignment = listItem.getAlignment();
                    indentation.listIndentLeft += listItem.getIndentationLeft();
                    indentation.indentRight += listItem.getIndentationRight();
                    leading = listItem.getTotalLeading();
                    carriageReturn();
                    // we prepare the current line to be able to show us the listsymbol
                    line.setListItem(listItem);
                    // we process the item
                    element.process(this);
                    addSpacing(listItem.getSpacingAfter(), listItem.getTotalLeading(), listItem.getFont());
                    // if the last line is justified, it should be aligned to the left
                    if (line.hasToBeJustified()) {
                        line.resetAlignment();
                    }
                    // some parameters are set back to normal again
                    carriageReturn();
                    indentation.listIndentLeft -= listItem.getIndentationLeft();
                    indentation.indentRight -= listItem.getIndentationRight();
                    leadingCount--;
                    break;
                }
            case Element.RECTANGLE:
                {
                    Rectangle rectangle = (Rectangle) element;
                    graphics.rectangle(rectangle);
                    pageEmpty = false;
                    break;
                }
            case Element.PTABLE:
                {
                    PdfPTable ptable = (PdfPTable) element;
                    if (ptable.size() <= ptable.getHeaderRows())
                        // nothing to do
                        break;
                    // before every table, we add a new line and flush all lines
                    ensureNewLine();
                    flushLines();
                    addPTable(ptable);
                    pageEmpty = false;
                    newLine();
                    break;
                }
            case Element.MULTI_COLUMN_TEXT:
                {
                    ensureNewLine();
                    flushLines();
                    MultiColumnText multiText = (MultiColumnText) element;
                    float height = multiText.write(writer.getDirectContent(), this, indentTop() - currentHeight);
                    currentHeight += height;
                    text.moveText(0, -1f * height);
                    pageEmpty = false;
                    break;
                }
            case Element.TABLE:
                {
                    if (element instanceof SimpleTable) {
                        PdfPTable ptable = ((SimpleTable) element).createPdfPTable();
                        if (ptable.size() <= ptable.getHeaderRows())
                            // nothing to do
                            break;
                        // before every table, we add a new line and flush all lines
                        ensureNewLine();
                        flushLines();
                        addPTable(ptable);
                        pageEmpty = false;
                        break;
                    } else if (element instanceof Table) {
                        try {
                            PdfPTable ptable = ((Table) element).createPdfPTable();
                            if (ptable.size() <= ptable.getHeaderRows())
                                // nothing to do
                                break;
                            // before every table, we add a new line and flush all lines
                            ensureNewLine();
                            flushLines();
                            addPTable(ptable);
                            pageEmpty = false;
                            break;
                        } catch (BadElementException bee) {
                            // constructing the PdfTable
                            // Before the table, add a blank line using offset or default leading
                            float offset = ((Table) element).getOffset();
                            if (Float.isNaN(offset))
                                offset = leading;
                            carriageReturn();
                            lines.add(new PdfLine(indentLeft(), indentRight(), alignment, offset));
                            currentHeight += offset;
                            addPdfTable((Table) element);
                        }
                    } else {
                        return false;
                    }
                    break;
                }
            case Element.JPEG:
            case Element.JPEG2000:
            case Element.JBIG2:
            case Element.IMGRAW:
            case Element.IMGTEMPLATE:
                {
                    // carriageReturn(); suggestion by Marc Campforts
                    add((Image) element);
                    break;
                }
            case Element.YMARK:
                {
                    DrawInterface zh = (DrawInterface) element;
                    zh.draw(graphics, indentLeft(), indentBottom(), indentRight(), indentTop(), indentTop() - currentHeight - (leadingCount > 0 ? leading : 0));
                    pageEmpty = false;
                    break;
                }
            case Element.MARKED:
                {
                    MarkedObject mo;
                    if (element instanceof MarkedSection) {
                        mo = ((MarkedSection) element).getTitle();
                        if (mo != null) {
                            mo.process(this);
                        }
                    }
                    mo = (MarkedObject) element;
                    mo.process(this);
                    break;
                }
            default:
                return false;
        }
        lastElementType = element.type();
        return true;
    } catch (Exception e) {
        throw new DocumentException(e);
    }
}
Also used : Rectangle(com.lowagie.text.Rectangle) Image(com.lowagie.text.Image) DocumentException(com.lowagie.text.DocumentException) ArrayList(java.util.ArrayList) List(com.lowagie.text.List) Table(com.lowagie.text.Table) SimpleTable(com.lowagie.text.SimpleTable) BadElementException(com.lowagie.text.BadElementException) MarkedSection(com.lowagie.text.MarkedSection) SimpleTable(com.lowagie.text.SimpleTable) DrawInterface(com.lowagie.text.pdf.draw.DrawInterface) Phrase(com.lowagie.text.Phrase) Chunk(com.lowagie.text.Chunk) MarkedSection(com.lowagie.text.MarkedSection) Section(com.lowagie.text.Section) Annotation(com.lowagie.text.Annotation) BadElementException(com.lowagie.text.BadElementException) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException) Paragraph(com.lowagie.text.Paragraph) Anchor(com.lowagie.text.Anchor) ListItem(com.lowagie.text.ListItem) MarkedObject(com.lowagie.text.MarkedObject)

Example 3 with MarkedObject

use of com.lowagie.text.MarkedObject 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);
    }
}
Also used : ExceptionConverter(com.lowagie.text.ExceptionConverter) Meta(com.lowagie.text.Meta) Header(com.lowagie.text.Header) MarkedSection(com.lowagie.text.MarkedSection) DocumentException(com.lowagie.text.DocumentException) MarkedObject(com.lowagie.text.MarkedObject) IOException(java.io.IOException)

Example 4 with MarkedObject

use of com.lowagie.text.MarkedObject in project itext2 by albfernandez.

the class PdfDocument method add.

/**
 * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
 *
 * @param element the element to add
 * @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 (writer != null && writer.isPaused()) {
        return false;
    }
    try {
        switch(element.type()) {
            // Information (headers)
            case Element.HEADER:
                info.addkey(((Meta) element).getName(), ((Meta) element).getContent());
                break;
            case Element.TITLE:
                info.addTitle(((Meta) element).getContent());
                break;
            case Element.SUBJECT:
                info.addSubject(((Meta) element).getContent());
                break;
            case Element.KEYWORDS:
                info.addKeywords(((Meta) element).getContent());
                break;
            case Element.AUTHOR:
                info.addAuthor(((Meta) element).getContent());
                break;
            case Element.CREATOR:
                info.addCreator(((Meta) element).getContent());
                break;
            case Element.PRODUCER:
                // you can not change the name of the producer
                info.addProducer();
                break;
            case Element.CREATIONDATE:
                // you can not set the creation date, only reset it
                info.addCreationDate();
                break;
            // content (text)
            case Element.CHUNK:
                {
                    // if there isn't a current line available, we make one
                    if (line == null) {
                        carriageReturn();
                    }
                    // we cast the element to a chunk
                    PdfChunk chunk = new PdfChunk((Chunk) element, anchorAction);
                    // we try to add the chunk to the line, until we succeed
                    {
                        PdfChunk overflow;
                        while ((overflow = line.add(chunk)) != null) {
                            carriageReturn();
                            chunk = overflow;
                            chunk.trimFirstSpace();
                        }
                    }
                    pageEmpty = false;
                    if (chunk.isAttribute(Chunk.NEWPAGE)) {
                        newPage();
                    }
                    break;
                }
            case Element.ANCHOR:
                {
                    leadingCount++;
                    Anchor anchor = (Anchor) element;
                    String url = anchor.getReference();
                    leading = anchor.getLeading();
                    if (url != null) {
                        anchorAction = new PdfAction(url);
                    }
                    // we process the element
                    element.process(this);
                    anchorAction = null;
                    leadingCount--;
                    break;
                }
            case Element.ANNOTATION:
                {
                    if (line == null) {
                        carriageReturn();
                    }
                    Annotation annot = (Annotation) element;
                    Rectangle rect = new Rectangle(0, 0);
                    if (line != null)
                        rect = new Rectangle(annot.llx(indentRight() - line.widthLeft()), annot.lly(indentTop() - currentHeight), annot.urx(indentRight() - line.widthLeft() + 20), annot.ury(indentTop() - currentHeight - 20));
                    PdfAnnotation an = PdfAnnotationsImp.convertAnnotation(writer, annot, rect);
                    annotationsImp.addPlainAnnotation(an);
                    pageEmpty = false;
                    break;
                }
            case Element.PHRASE:
                {
                    leadingCount++;
                    // we cast the element to a phrase and set the leading of the document
                    leading = ((Phrase) element).getLeading();
                    // we process the element
                    element.process(this);
                    leadingCount--;
                    break;
                }
            case Element.PARAGRAPH:
                {
                    leadingCount++;
                    // we cast the element to a paragraph
                    Paragraph paragraph = (Paragraph) element;
                    addSpacing(paragraph.getSpacingBefore(), leading, paragraph.getFont());
                    // we adjust the parameters of the document
                    alignment = paragraph.getAlignment();
                    leading = paragraph.getTotalLeading();
                    carriageReturn();
                    // we don't want to make orphans/widows
                    if (currentHeight + line.height() + leading > indentTop() - indentBottom()) {
                        newPage();
                    }
                    indentation.indentLeft += paragraph.getIndentationLeft();
                    indentation.indentRight += paragraph.getIndentationRight();
                    carriageReturn();
                    PdfPageEvent pageEvent = writer.getPageEvent();
                    if (pageEvent != null && !isSectionTitle)
                        pageEvent.onParagraph(writer, this, indentTop() - currentHeight);
                    // if a paragraph has to be kept together, we wrap it in a table object
                    if (paragraph.getKeepTogether()) {
                        carriageReturn();
                        PdfPTable table = new PdfPTable(1);
                        table.setWidthPercentage(100f);
                        PdfPCell cell = new PdfPCell();
                        cell.setBorder(Table.NO_BORDER);
                        cell.setPadding(0);
                        Iterator iterator = paragraph.iterator();
                        while (iterator.hasNext()) {
                            Object o = iterator.next();
                            if (o instanceof Element) {
                                cell.addElement((Element) o);
                            }
                        }
                        table.addCell(cell);
                        indentation.indentLeft -= paragraph.getIndentationLeft();
                        indentation.indentRight -= paragraph.getIndentationRight();
                        this.add(table);
                        indentation.indentLeft += paragraph.getIndentationLeft();
                        indentation.indentRight += paragraph.getIndentationRight();
                    } else {
                        line.setExtraIndent(paragraph.getFirstLineIndent());
                        element.process(this);
                        carriageReturn();
                        addSpacing(paragraph.getSpacingAfter(), paragraph.getTotalLeading(), paragraph.getFont());
                    }
                    if (pageEvent != null && !isSectionTitle)
                        pageEvent.onParagraphEnd(writer, this, indentTop() - currentHeight);
                    alignment = Element.ALIGN_LEFT;
                    indentation.indentLeft -= paragraph.getIndentationLeft();
                    indentation.indentRight -= paragraph.getIndentationRight();
                    carriageReturn();
                    leadingCount--;
                    break;
                }
            case Element.SECTION:
            case Element.CHAPTER:
                {
                    // Chapters and Sections only differ in their constructor
                    // so we cast both to a Section
                    Section section = (Section) element;
                    PdfPageEvent pageEvent = writer.getPageEvent();
                    boolean hasTitle = section.isNotAddedYet() && section.getTitle() != null;
                    // if the section is a chapter, we begin a new page
                    if (section.isTriggerNewPage()) {
                        newPage();
                    }
                    if (hasTitle) {
                        float fith = indentTop() - currentHeight;
                        int rotation = pageSize.getRotation();
                        if (rotation == 90 || rotation == 180)
                            fith = pageSize.getHeight() - fith;
                        PdfDestination destination = new PdfDestination(PdfDestination.FITH, fith);
                        while (currentOutline.level() >= section.getDepth()) {
                            currentOutline = currentOutline.parent();
                        }
                        PdfOutline outline = new PdfOutline(currentOutline, destination, section.getBookmarkTitle(), section.isBookmarkOpen());
                        currentOutline = outline;
                    }
                    // some values are set
                    carriageReturn();
                    indentation.sectionIndentLeft += section.getIndentationLeft();
                    indentation.sectionIndentRight += section.getIndentationRight();
                    if (section.isNotAddedYet() && pageEvent != null)
                        if (element.type() == Element.CHAPTER)
                            pageEvent.onChapter(writer, this, indentTop() - currentHeight, section.getTitle());
                        else
                            pageEvent.onSection(writer, this, indentTop() - currentHeight, section.getDepth(), section.getTitle());
                    // the title of the section (if any has to be printed)
                    if (hasTitle) {
                        isSectionTitle = true;
                        add(section.getTitle());
                        isSectionTitle = false;
                    }
                    indentation.sectionIndentLeft += section.getIndentation();
                    // we process the section
                    element.process(this);
                    flushLines();
                    // some parameters are set back to normal again
                    indentation.sectionIndentLeft -= (section.getIndentationLeft() + section.getIndentation());
                    indentation.sectionIndentRight -= section.getIndentationRight();
                    if (section.isComplete() && pageEvent != null)
                        if (element.type() == Element.CHAPTER)
                            pageEvent.onChapterEnd(writer, this, indentTop() - currentHeight);
                        else
                            pageEvent.onSectionEnd(writer, this, indentTop() - currentHeight);
                    break;
                }
            case Element.LIST:
                {
                    // we cast the element to a List
                    List list = (List) element;
                    if (list.isAlignindent()) {
                        list.normalizeIndentation();
                    }
                    // we adjust the document
                    indentation.listIndentLeft += list.getIndentationLeft();
                    indentation.indentRight += list.getIndentationRight();
                    // we process the items in the list
                    element.process(this);
                    // some parameters are set back to normal again
                    indentation.listIndentLeft -= list.getIndentationLeft();
                    indentation.indentRight -= list.getIndentationRight();
                    carriageReturn();
                    break;
                }
            case Element.LISTITEM:
                {
                    leadingCount++;
                    // we cast the element to a ListItem
                    ListItem listItem = (ListItem) element;
                    addSpacing(listItem.getSpacingBefore(), leading, listItem.getFont());
                    // we adjust the document
                    alignment = listItem.getAlignment();
                    indentation.listIndentLeft += listItem.getIndentationLeft();
                    indentation.indentRight += listItem.getIndentationRight();
                    leading = listItem.getTotalLeading();
                    carriageReturn();
                    // we prepare the current line to be able to show us the listsymbol
                    line.setListItem(listItem);
                    // we process the item
                    element.process(this);
                    addSpacing(listItem.getSpacingAfter(), listItem.getTotalLeading(), listItem.getFont());
                    // if the last line is justified, it should be aligned to the left
                    if (line.hasToBeJustified()) {
                        line.resetAlignment();
                    }
                    // some parameters are set back to normal again
                    carriageReturn();
                    indentation.listIndentLeft -= listItem.getIndentationLeft();
                    indentation.indentRight -= listItem.getIndentationRight();
                    leadingCount--;
                    break;
                }
            case Element.RECTANGLE:
                {
                    Rectangle rectangle = (Rectangle) element;
                    graphics.rectangle(rectangle);
                    pageEmpty = false;
                    break;
                }
            case Element.PTABLE:
                {
                    PdfPTable ptable = (PdfPTable) element;
                    if (ptable.size() <= ptable.getHeaderRows())
                        // nothing to do
                        break;
                    // before every table, we add a new line and flush all lines
                    ensureNewLine();
                    flushLines();
                    addPTable(ptable);
                    pageEmpty = false;
                    newLine();
                    break;
                }
            case Element.MULTI_COLUMN_TEXT:
                {
                    ensureNewLine();
                    flushLines();
                    MultiColumnText multiText = (MultiColumnText) element;
                    float height = multiText.write(writer.getDirectContent(), this, indentTop() - currentHeight);
                    currentHeight += height;
                    text.moveText(0, -1f * height);
                    pageEmpty = false;
                    break;
                }
            case Element.TABLE:
                {
                    if (element instanceof SimpleTable) {
                        PdfPTable ptable = ((SimpleTable) element).createPdfPTable();
                        if (ptable.size() <= ptable.getHeaderRows())
                            // nothing to do
                            break;
                        // before every table, we add a new line and flush all lines
                        ensureNewLine();
                        flushLines();
                        addPTable(ptable);
                        pageEmpty = false;
                        break;
                    } else if (element instanceof Table) {
                        try {
                            PdfPTable ptable = ((Table) element).createPdfPTable();
                            if (ptable.size() <= ptable.getHeaderRows())
                                // nothing to do
                                break;
                            // before every table, we add a new line and flush all lines
                            ensureNewLine();
                            flushLines();
                            addPTable(ptable);
                            pageEmpty = false;
                            break;
                        } catch (BadElementException bee) {
                            // constructing the PdfTable
                            // Before the table, add a blank line using offset or default leading
                            float offset = ((Table) element).getOffset();
                            if (Float.isNaN(offset))
                                offset = leading;
                            carriageReturn();
                            lines.add(new PdfLine(indentLeft(), indentRight(), alignment, offset));
                            currentHeight += offset;
                            addPdfTable((Table) element);
                        }
                    } else {
                        return false;
                    }
                    break;
                }
            case Element.JPEG:
            case Element.JPEG2000:
            case Element.JBIG2:
            case Element.IMGRAW:
            case Element.IMGTEMPLATE:
                {
                    // carriageReturn(); suggestion by Marc Campforts
                    add((Image) element);
                    break;
                }
            case Element.YMARK:
                {
                    DrawInterface zh = (DrawInterface) element;
                    zh.draw(graphics, indentLeft(), indentBottom(), indentRight(), indentTop(), indentTop() - currentHeight - (leadingCount > 0 ? leading : 0));
                    pageEmpty = false;
                    break;
                }
            case Element.MARKED:
                {
                    MarkedObject mo;
                    if (element instanceof MarkedSection) {
                        mo = ((MarkedSection) element).getTitle();
                        if (mo != null) {
                            mo.process(this);
                        }
                    }
                    mo = (MarkedObject) element;
                    mo.process(this);
                    break;
                }
            default:
                return false;
        }
        lastElementType = element.type();
        return true;
    } catch (Exception e) {
        throw new DocumentException(e);
    }
}
Also used : Element(com.lowagie.text.Element) Rectangle(com.lowagie.text.Rectangle) Image(com.lowagie.text.Image) DocumentException(com.lowagie.text.DocumentException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(com.lowagie.text.List) Table(com.lowagie.text.Table) SimpleTable(com.lowagie.text.SimpleTable) BadElementException(com.lowagie.text.BadElementException) MarkedSection(com.lowagie.text.MarkedSection) SimpleTable(com.lowagie.text.SimpleTable) DrawInterface(com.lowagie.text.pdf.draw.DrawInterface) Phrase(com.lowagie.text.Phrase) Chunk(com.lowagie.text.Chunk) MarkedSection(com.lowagie.text.MarkedSection) Section(com.lowagie.text.Section) Annotation(com.lowagie.text.Annotation) BadElementException(com.lowagie.text.BadElementException) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException) Paragraph(com.lowagie.text.Paragraph) Anchor(com.lowagie.text.Anchor) MarkedObject(com.lowagie.text.MarkedObject) ListItem(com.lowagie.text.ListItem) MarkedObject(com.lowagie.text.MarkedObject)

Aggregations

DocumentException (com.lowagie.text.DocumentException)4 MarkedObject (com.lowagie.text.MarkedObject)4 MarkedSection (com.lowagie.text.MarkedSection)4 IOException (java.io.IOException)4 Anchor (com.lowagie.text.Anchor)2 Annotation (com.lowagie.text.Annotation)2 BadElementException (com.lowagie.text.BadElementException)2 Chunk (com.lowagie.text.Chunk)2 ExceptionConverter (com.lowagie.text.ExceptionConverter)2 Header (com.lowagie.text.Header)2 Image (com.lowagie.text.Image)2 List (com.lowagie.text.List)2 ListItem (com.lowagie.text.ListItem)2 Meta (com.lowagie.text.Meta)2 Paragraph (com.lowagie.text.Paragraph)2 Phrase (com.lowagie.text.Phrase)2 Rectangle (com.lowagie.text.Rectangle)2 Section (com.lowagie.text.Section)2 SimpleTable (com.lowagie.text.SimpleTable)2 Table (com.lowagie.text.Table)2