Search in sources :

Example 1 with RtfTab

use of com.lowagie.text.rtf.text.RtfTab in project itext2 by albfernandez.

the class TabGroupsTest method main.

/**
 * Using the RtfTabGroup to simplify adding a set of tab stops.
 */
@Test
public void main() throws Exception {
    Document document = new Document();
    RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("TabGroups.rtf"));
    document.open();
    // Construct the RtfTabGroup object
    RtfTabGroup tabGroup = new RtfTabGroup();
    // Add RtfTab tab stops at the desired positions
    tabGroup.add(new RtfTab(400, RtfTab.TAB_RIGHT_ALIGN));
    tabGroup.add(new RtfTab(500, RtfTab.TAB_DECIMAL_ALIGN));
    // Create a Paragraph object
    Paragraph par = new Paragraph();
    // Add the tab group to the paragraph
    par.add(tabGroup);
    // Specify the tab positions using "\t"
    par.add("Description\tDate\tAmount");
    document.add(par);
    DecimalFormat nf = new DecimalFormat("#.00");
    double sum = 0;
    for (int i = 0; i < 10; i++) {
        double value = Math.random() * 100;
        sum = sum + value;
        par = new Paragraph();
        // The RtfTabGroup can be reused for further paragraphs
        par.add(tabGroup);
        par.add("Item " + (i + 1) + "\t" + (12 + i) + ".03.2007\t" + nf.format(value));
        document.add(par);
    }
    par = new Paragraph("", new Font(Font.TIMES_ROMAN, 12, Font.BOLD));
    par.add(tabGroup);
    // If a tab in the RtfTabGroup is not needed, just add an empty tab stop in the text
    par.add("Total\t\t" + nf.format(sum));
    document.add(par);
    document.close();
}
Also used : DecimalFormat(java.text.DecimalFormat) RtfTabGroup(com.lowagie.text.rtf.text.RtfTabGroup) Document(com.lowagie.text.Document) RtfTab(com.lowagie.text.rtf.text.RtfTab) Font(com.lowagie.text.Font) Paragraph(com.lowagie.text.Paragraph) Test(org.junit.Test)

Example 2 with RtfTab

use of com.lowagie.text.rtf.text.RtfTab in project itext2 by albfernandez.

the class BasicTabsTest method main.

/**
 * Tab stops in paragraphs.
 */
@Test
public void main() throws Exception {
    Document document = new Document();
    RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("BasicTabs.rtf"));
    document.open();
    // Define the Paragraph to add tab stops to
    Paragraph par = new Paragraph();
    // Add the tab stops to the paragraph
    par.add(new RtfTab(70, RtfTab.TAB_LEFT_ALIGN));
    par.add(new RtfTab(400, RtfTab.TAB_RIGHT_ALIGN));
    // Add the text to the paragraph, placing the tab stops with \t
    par.add("\tFirst the text on the left-hand side\tThis text is right aligned.");
    document.add(par);
    document.close();
}
Also used : Document(com.lowagie.text.Document) RtfTab(com.lowagie.text.rtf.text.RtfTab) Paragraph(com.lowagie.text.Paragraph) Test(org.junit.Test)

Example 3 with RtfTab

use of com.lowagie.text.rtf.text.RtfTab in project itext2 by albfernandez.

the class RtfMapper method mapElement.

/**
 * Takes an Element subclass and returns an array of RtfBasicElement
 * subclasses, that contained the mapped RTF equivalent to the Element
 * passed in.
 *
 * @param element The Element to wrap
 * @return An array of RtfBasicElement wrapping the Element
 * @throws DocumentException
 */
public RtfBasicElement[] mapElement(Element element) throws DocumentException {
    ArrayList rtfElements = new ArrayList();
    if (element instanceof RtfBasicElement) {
        RtfBasicElement rtfElement = (RtfBasicElement) element;
        rtfElement.setRtfDocument(this.rtfDoc);
        return new RtfBasicElement[] { rtfElement };
    }
    switch(element.type()) {
        case Element.CHUNK:
            Chunk chunk = (Chunk) element;
            if (chunk.hasAttributes()) {
                if (chunk.getAttributes().containsKey(Chunk.IMAGE)) {
                    rtfElements.add(new RtfImage(rtfDoc, chunk.getImage()));
                } else if (chunk.getAttributes().containsKey(Chunk.NEWPAGE)) {
                    rtfElements.add(new RtfNewPage(rtfDoc));
                } else if (chunk.getAttributes().containsKey(Chunk.TAB)) {
                    Float tabPos = (Float) ((Object[]) chunk.getAttributes().get(Chunk.TAB))[1];
                    RtfTab tab = new RtfTab(tabPos.floatValue(), RtfTab.TAB_LEFT_ALIGN);
                    tab.setRtfDocument(rtfDoc);
                    rtfElements.add(tab);
                    rtfElements.add(new RtfChunk(rtfDoc, new Chunk("\t")));
                } else {
                    rtfElements.add(new RtfChunk(rtfDoc, (Chunk) element));
                }
            } else {
                rtfElements.add(new RtfChunk(rtfDoc, (Chunk) element));
            }
            break;
        case Element.PHRASE:
            rtfElements.add(new RtfPhrase(rtfDoc, (Phrase) element));
            break;
        case Element.PARAGRAPH:
            rtfElements.add(new RtfParagraph(rtfDoc, (Paragraph) element));
            break;
        case Element.ANCHOR:
            rtfElements.add(new RtfAnchor(rtfDoc, (Anchor) element));
            break;
        case Element.ANNOTATION:
            rtfElements.add(new RtfAnnotation(rtfDoc, (Annotation) element));
            break;
        case Element.IMGRAW:
        case Element.IMGTEMPLATE:
        case Element.JPEG:
            rtfElements.add(new RtfImage(rtfDoc, (Image) element));
            break;
        case Element.AUTHOR:
        case Element.SUBJECT:
        case Element.KEYWORDS:
        case Element.TITLE:
        case Element.PRODUCER:
        case Element.CREATIONDATE:
            rtfElements.add(new RtfInfoElement(rtfDoc, (Meta) element));
            break;
        case Element.LIST:
            // TODO: Testing
            rtfElements.add(new RtfList(rtfDoc, (List) element));
            break;
        case Element.LISTITEM:
            // TODO: Testing
            rtfElements.add(new RtfListItem(rtfDoc, (ListItem) element));
            break;
        case Element.SECTION:
            rtfElements.add(new RtfSection(rtfDoc, (Section) element));
            break;
        case Element.CHAPTER:
            rtfElements.add(new RtfChapter(rtfDoc, (Chapter) element));
            break;
        case Element.TABLE:
            try {
                rtfElements.add(new RtfTable(rtfDoc, (Table) element));
            } catch (ClassCastException e) {
                rtfElements.add(new RtfTable(rtfDoc, ((SimpleTable) element).createTable()));
            }
            break;
        case Element.PTABLE:
            try {
                rtfElements.add(new RtfTable(rtfDoc, (PdfPTable) element));
            } catch (ClassCastException e) {
                rtfElements.add(new RtfTable(rtfDoc, ((SimpleTable) element).createTable()));
            }
            break;
    }
    return (RtfBasicElement[]) rtfElements.toArray(new RtfBasicElement[rtfElements.size()]);
}
Also used : Meta(com.lowagie.text.Meta) ArrayList(java.util.ArrayList) RtfImage(com.lowagie.text.rtf.graphic.RtfImage) Image(com.lowagie.text.Image) RtfImage(com.lowagie.text.rtf.graphic.RtfImage) RtfTab(com.lowagie.text.rtf.text.RtfTab) RtfAnchor(com.lowagie.text.rtf.field.RtfAnchor) PdfPTable(com.lowagie.text.pdf.PdfPTable) RtfNewPage(com.lowagie.text.rtf.text.RtfNewPage) ArrayList(java.util.ArrayList) List(com.lowagie.text.List) RtfList(com.lowagie.text.rtf.list.RtfList) RtfListItem(com.lowagie.text.rtf.list.RtfListItem) RtfTable(com.lowagie.text.rtf.table.RtfTable) PdfPTable(com.lowagie.text.pdf.PdfPTable) Table(com.lowagie.text.Table) SimpleTable(com.lowagie.text.SimpleTable) RtfTable(com.lowagie.text.rtf.table.RtfTable) RtfParagraph(com.lowagie.text.rtf.text.RtfParagraph) RtfChapter(com.lowagie.text.rtf.text.RtfChapter) Chapter(com.lowagie.text.Chapter) RtfSection(com.lowagie.text.rtf.text.RtfSection) RtfChunk(com.lowagie.text.rtf.text.RtfChunk) Phrase(com.lowagie.text.Phrase) RtfPhrase(com.lowagie.text.rtf.text.RtfPhrase) RtfAnnotation(com.lowagie.text.rtf.text.RtfAnnotation) Chunk(com.lowagie.text.Chunk) RtfChunk(com.lowagie.text.rtf.text.RtfChunk) RtfChapter(com.lowagie.text.rtf.text.RtfChapter) RtfSection(com.lowagie.text.rtf.text.RtfSection) Section(com.lowagie.text.Section) Annotation(com.lowagie.text.Annotation) RtfAnnotation(com.lowagie.text.rtf.text.RtfAnnotation) RtfParagraph(com.lowagie.text.rtf.text.RtfParagraph) Paragraph(com.lowagie.text.Paragraph) RtfList(com.lowagie.text.rtf.list.RtfList) RtfAnchor(com.lowagie.text.rtf.field.RtfAnchor) Anchor(com.lowagie.text.Anchor) RtfInfoElement(com.lowagie.text.rtf.document.RtfInfoElement) ListItem(com.lowagie.text.ListItem) RtfListItem(com.lowagie.text.rtf.list.RtfListItem) RtfPhrase(com.lowagie.text.rtf.text.RtfPhrase)

Aggregations

Paragraph (com.lowagie.text.Paragraph)3 RtfTab (com.lowagie.text.rtf.text.RtfTab)3 Document (com.lowagie.text.Document)2 Test (org.junit.Test)2 Anchor (com.lowagie.text.Anchor)1 Annotation (com.lowagie.text.Annotation)1 Chapter (com.lowagie.text.Chapter)1 Chunk (com.lowagie.text.Chunk)1 Font (com.lowagie.text.Font)1 Image (com.lowagie.text.Image)1 List (com.lowagie.text.List)1 ListItem (com.lowagie.text.ListItem)1 Meta (com.lowagie.text.Meta)1 Phrase (com.lowagie.text.Phrase)1 Section (com.lowagie.text.Section)1 SimpleTable (com.lowagie.text.SimpleTable)1 Table (com.lowagie.text.Table)1 PdfPTable (com.lowagie.text.pdf.PdfPTable)1 RtfInfoElement (com.lowagie.text.rtf.document.RtfInfoElement)1 RtfAnchor (com.lowagie.text.rtf.field.RtfAnchor)1