Search in sources :

Example 1 with RtfWriter2

use of com.lowagie.text.rtf.RtfWriter2 in project javamelody by javamelody.

the class MRtfWriter method createWriter.

/**
 * We create a writer that listens to the document and directs a RTF-stream to out
 *
 * @param table
 *           MBasicTable
 * @param document
 *           Document
 * @param out
 *           OutputStream
 * @return DocWriter
 */
@Override
protected DocWriter createWriter(final MBasicTable table, final Document document, final OutputStream out) {
    final RtfWriter2 writer = RtfWriter2.getInstance(document, out);
    // title
    final String title = buildTitle(table);
    if (title != null) {
        final HeaderFooter header = new RtfHeaderFooter(new Paragraph(title));
        header.setAlignment(Element.ALIGN_LEFT);
        header.setBorder(Rectangle.NO_BORDER);
        document.setHeader(header);
        document.addTitle(title);
    }
    // advanced page numbers : x/y
    final Paragraph footerParagraph = new Paragraph();
    final Font font = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL);
    footerParagraph.add(new RtfPageNumber(font));
    footerParagraph.add(new Phrase(" / ", font));
    footerParagraph.add(new RtfTotalPageNumber(font));
    footerParagraph.setAlignment(Element.ALIGN_CENTER);
    final HeaderFooter footer = new RtfHeaderFooter(footerParagraph);
    footer.setBorder(Rectangle.TOP);
    document.setFooter(footer);
    return writer;
}
Also used : RtfHeaderFooter(com.lowagie.text.rtf.headerfooter.RtfHeaderFooter) HeaderFooter(com.lowagie.text.HeaderFooter) RtfHeaderFooter(com.lowagie.text.rtf.headerfooter.RtfHeaderFooter) RtfTotalPageNumber(com.lowagie.text.rtf.field.RtfTotalPageNumber) Phrase(com.lowagie.text.Phrase) Font(com.lowagie.text.Font) RtfWriter2(com.lowagie.text.rtf.RtfWriter2) Paragraph(com.lowagie.text.Paragraph) RtfPageNumber(com.lowagie.text.rtf.field.RtfPageNumber)

Example 2 with RtfWriter2

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

the class RtfTest method main.

/**
 * Creates 1 RTF file.
 */
@Test
public void main() throws Exception {
    Document doc = new Document();
    RtfWriter2 writer2 = RtfWriter2.getInstance(doc, PdfTestBase.getOutputStream("testNew.rtf"));
    writer2.setAutogenerateTOCEntries(true);
    Table headerTable = new Table(3);
    headerTable.addCell("Test Cell 1");
    headerTable.addCell("Test Cell 2");
    headerTable.addCell("Test Cell 3");
    HeaderFooter header = new RtfHeaderFooter(headerTable);
    RtfHeaderFooterGroup footer = new RtfHeaderFooterGroup();
    footer.setHeaderFooter(new RtfHeaderFooter(new Phrase("This is the footer on the title page")), com.lowagie.text.rtf.headerfooter.RtfHeaderFooter.DISPLAY_FIRST_PAGE);
    footer.setHeaderFooter(new RtfHeaderFooter(new Phrase("This is a left side page")), com.lowagie.text.rtf.headerfooter.RtfHeaderFooter.DISPLAY_LEFT_PAGES);
    footer.setHeaderFooter(new RtfHeaderFooter(new Phrase("This is a right side page")), com.lowagie.text.rtf.headerfooter.RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
    doc.setHeader(header);
    doc.setFooter(footer);
    doc.open();
    Paragraph p = new Paragraph();
    p.add(new RtfTableOfContents("UPDATE ME!"));
    doc.add(p);
    p = new Paragraph("", new RtfFont("Staccato222 BT"));
    p.add(new Chunk("Hello! "));
    p.add(new Chunk("How do you do?"));
    doc.add(p);
    p.setAlignment(Element.ALIGN_RIGHT);
    doc.add(p);
    Anchor a = new Anchor("http://www.uni-klu.ac.at");
    a.setReference("http://www.uni-klu.ac.at");
    doc.add(a);
    Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR + "pngnow.png");
    doc.add(new Chunk(img, 0, 0));
    doc.add(new Annotation("Mark", "This works!"));
    Chunk c = new Chunk("");
    c.setNewPage();
    doc.add(c);
    List subList = new List(true, 40);
    subList.add(new ListItem("Sub list 1"));
    subList.add(new ListItem("Sub list 2"));
    List list = new List(true, 20);
    list.add(new ListItem("Test line 1"));
    list.add(new ListItem("Test line 2 - This is a really long test line to test that linebreaks are working the way they are supposed to work so a really really long line of drivel is required"));
    list.add(subList);
    list.add(new ListItem("Test line 3 - \t\u20ac\t 60,-"));
    doc.add(list);
    list = new List(false, 20);
    list.add(new ListItem("Bullet"));
    list.add(new ListItem("Another one"));
    doc.add(list);
    doc.newPage();
    Chapter chapter = new Chapter(new Paragraph("This is a Chapter"), 1);
    chapter.add(new Paragraph("This is some text that belongs to this chapter."));
    chapter.add(new Paragraph("A second paragraph. What a surprise."));
    Section section = chapter.addSection(new Paragraph("This is a subsection"));
    section.add(new Paragraph("Text in the subsection."));
    doc.add(chapter);
    com.lowagie.text.rtf.field.RtfTOCEntry rtfTOC = new com.lowagie.text.rtf.field.RtfTOCEntry("Table Test");
    doc.add(rtfTOC);
    Table table = new Table(3);
    table.setPadding(2);
    table.setAlignment(Element.ALIGN_LEFT);
    table.setSpacing(2);
    Cell emptyCell = new Cell("");
    Cell cellLeft = new Cell("Left Alignment");
    cellLeft.setHorizontalAlignment(Element.ALIGN_LEFT);
    Cell cellCenter = new Cell("Center Alignment");
    cellCenter.setHorizontalAlignment(Element.ALIGN_CENTER);
    Cell cellRight = new Cell("Right Alignment");
    cellRight.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(cellLeft);
    table.addCell(cellCenter);
    table.addCell(cellRight);
    Cell cellSpanHoriz = new Cell("This Cell spans two columns");
    cellSpanHoriz.setColspan(2);
    table.addCell(cellSpanHoriz);
    table.addCell(emptyCell);
    Cell cellSpanVert = new Cell("This Cell spans two rows");
    cellSpanVert.setRowspan(2);
    table.addCell(emptyCell);
    table.addCell(cellSpanVert);
    table.addCell(emptyCell);
    table.addCell(emptyCell);
    table.addCell(emptyCell);
    Cell cellSpanHorizVert = new Cell("This Cell spans both two columns and two rows");
    cellSpanHorizVert.setColspan(2);
    cellSpanHorizVert.setRowspan(2);
    table.addCell(emptyCell);
    table.addCell(cellSpanHorizVert);
    table.addCell(emptyCell);
    RtfCell cellDotted = new RtfCell("Dotted border");
    cellDotted.setBorders(new RtfBorderGroup(Rectangle.BOX, RtfBorder.BORDER_DOTTED, 1, new Color(0, 0, 0)));
    RtfCell cellEmbossed = new RtfCell("Embossed border");
    cellEmbossed.setBorders(new RtfBorderGroup(Rectangle.BOX, RtfBorder.BORDER_EMBOSS, 1, new Color(0, 0, 0)));
    RtfCell cellNoBorder = new RtfCell("No border");
    cellNoBorder.setBorders(new RtfBorderGroup());
    table.addCell(cellDotted);
    table.addCell(cellEmbossed);
    table.addCell(cellNoBorder);
    doc.add(table);
    for (int i = 0; i < 300; i++) {
        doc.add(new Paragraph("Dummy line to get multi-page document. Line " + (i + 1)));
    }
    doc.close();
}
Also used : RtfFont(com.lowagie.text.rtf.style.RtfFont) HeaderFooter(com.lowagie.text.HeaderFooter) RtfHeaderFooter(com.lowagie.text.rtf.headerfooter.RtfHeaderFooter) Document(com.lowagie.text.Document) Image(com.lowagie.text.Image) RtfHeaderFooterGroup(com.lowagie.text.rtf.headerfooter.RtfHeaderFooterGroup) List(com.lowagie.text.List) RtfCell(com.lowagie.text.rtf.table.RtfCell) Cell(com.lowagie.text.Cell) Table(com.lowagie.text.Table) RtfBorderGroup(com.lowagie.text.rtf.table.RtfBorderGroup) Color(java.awt.Color) Chapter(com.lowagie.text.Chapter) RtfCell(com.lowagie.text.rtf.table.RtfCell) Phrase(com.lowagie.text.Phrase) Chunk(com.lowagie.text.Chunk) Section(com.lowagie.text.Section) Annotation(com.lowagie.text.Annotation) Paragraph(com.lowagie.text.Paragraph) RtfHeaderFooter(com.lowagie.text.rtf.headerfooter.RtfHeaderFooter) Anchor(com.lowagie.text.Anchor) ListItem(com.lowagie.text.ListItem) RtfTableOfContents(com.lowagie.text.rtf.field.RtfTableOfContents) RtfWriter2(com.lowagie.text.rtf.RtfWriter2) Test(org.junit.Test)

Example 3 with RtfWriter2

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

the class ExtendingStylesheetsTest method main.

/**
 * Creation of new paragraph stylesheets.
 */
@Test
public void main() throws Exception {
    Document document = new Document();
    RtfWriter2 writer = RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("ExtendingStylesheets.rtf"));
    // Create the new RtfParagraphStyle. The second parameter is the name of
    // the RtfParagraphStyle that this style will inherit default properties
    // from.
    RtfParagraphStyle incorrectStyle = new RtfParagraphStyle("Incorrect", "Normal");
    // Change the desired properties
    incorrectStyle.setColor(Color.RED);
    incorrectStyle.setStyle(Font.STRIKETHRU);
    // Register the new paragraph stylesheet with the RtfWriter2.
    writer.getDocumentSettings().registerParagraphStyle(incorrectStyle);
    // Create a new RtfParagraphStyle that does not inherit from any other
    // style.
    RtfParagraphStyle correctStyle = new RtfParagraphStyle("Correct", "Arial", 12, Font.NORMAL, Color.GREEN);
    // Register the new paragraph stylesheet with the RtfWriter2.
    writer.getDocumentSettings().registerParagraphStyle(correctStyle);
    // Change the default font name. This will propagate to the paragraph
    // stylesheet
    // that inherits, but not the other one.
    RtfParagraphStyle.STYLE_NORMAL.setFontName("Times New Roman");
    document.open();
    // Simply set the stylesheet you wish to use as the Font
    // of the Paragraph
    document.add(new Paragraph("This is a heading level 1", RtfParagraphStyle.STYLE_HEADING_1));
    document.add(new Paragraph("This is a heading level 2", RtfParagraphStyle.STYLE_HEADING_2));
    document.add(new Paragraph("Just some text that is formatted " + "in the default style.", RtfParagraphStyle.STYLE_NORMAL));
    document.add(new Paragraph("This paragraph should be removed.", incorrectStyle));
    document.add(new Paragraph("It should be replaced with this.", correctStyle));
    document.close();
}
Also used : RtfParagraphStyle(com.lowagie.text.rtf.style.RtfParagraphStyle) Document(com.lowagie.text.Document) RtfWriter2(com.lowagie.text.rtf.RtfWriter2) Paragraph(com.lowagie.text.Paragraph) Test(org.junit.Test)

Example 4 with RtfWriter2

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

the class TableOfContentsTest method main.

/**
 * Demonstrates creating and styling a table of contents
 */
@Test
public void main() throws Exception {
    Document document = new Document();
    RtfWriter2 rtfWriter2 = RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("TableOfContents.rtf"));
    // Create paragraph stylesheets for each heading level. They must be named
    // "toc N" for each heading level you are using
    RtfParagraphStyle tocLevel1Style = new RtfParagraphStyle("toc 1", "Times New Roman", 11, Font.NORMAL, Color.BLACK);
    RtfParagraphStyle tocLevel2Style = new RtfParagraphStyle("toc 2", "Times New Roman", 10, Font.NORMAL, Color.BLACK);
    tocLevel2Style.setIndentLeft(10);
    // Register the paragraph stylesheets with the RtfWriter2
    rtfWriter2.getDocumentSettings().registerParagraphStyle(tocLevel1Style);
    rtfWriter2.getDocumentSettings().registerParagraphStyle(tocLevel2Style);
    document.open();
    // Create a Paragraph and add the table of contents to it
    Paragraph par = new Paragraph();
    par.add(new RtfTableOfContents("Right-click here and select \"Update\" " + "to see the table of contents."));
    document.add(par);
    for (int i = 1; i <= 5; i++) {
        // Create a level 1 heading
        document.add(new Paragraph("Heading " + i, RtfParagraphStyle.STYLE_HEADING_1));
        for (int j = 1; j <= 3; j++) {
            // Create a level 2 heading
            document.add(new Paragraph("Heading " + i + "." + j, RtfParagraphStyle.STYLE_HEADING_2));
            for (int k = 1; k <= 20; k++) {
                document.add(new Paragraph("Line " + k + " in section " + i + "." + k));
            }
        }
    }
    document.close();
}
Also used : RtfParagraphStyle(com.lowagie.text.rtf.style.RtfParagraphStyle) Document(com.lowagie.text.Document) RtfTableOfContents(com.lowagie.text.rtf.field.RtfTableOfContents) RtfWriter2(com.lowagie.text.rtf.RtfWriter2) Paragraph(com.lowagie.text.Paragraph) Test(org.junit.Test)

Example 5 with RtfWriter2

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

the class HelloWorldMultipleTest method main.

/**
 * Generates simple PDF, RTF and HTML files using only one Document object.
 */
@Test
public void main() throws Exception {
    // step 1: creation of a document-object
    Document document = new Document();
    // step 2:
    // we create 3 different writers that listen to the document
    PdfWriter pdf = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("HelloWorldPdf.pdf"));
    RtfWriter2 rtf = RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("HelloWorldRtf.rtf"));
    HtmlWriter.getInstance(document, PdfTestBase.getOutputStream("HelloWorldHtml.html"));
    // step 3: we open the document
    document.open();
    // step 4: we add a paragraph to the document
    document.add(new Paragraph("Hello World"));
    // we make references
    Anchor pdfRef = new Anchor("see Hello World in PDF.");
    pdfRef.setReference("./HelloWorldPdf.pdf");
    Anchor rtfRef = new Anchor("see Hello World in RTF.");
    rtfRef.setReference("./HelloWorldRtf.rtf");
    // we add the references, but only to the HTML page:
    pdf.pause();
    rtf.pause();
    document.add(pdfRef);
    document.add(Chunk.NEWLINE);
    document.add(rtfRef);
    pdf.resume();
    rtf.resume();
    // step 5: we close the document
    document.close();
}
Also used : Anchor(com.lowagie.text.Anchor) PdfWriter(com.lowagie.text.pdf.PdfWriter) Document(com.lowagie.text.Document) RtfWriter2(com.lowagie.text.rtf.RtfWriter2) Paragraph(com.lowagie.text.Paragraph) Test(org.junit.Test)

Aggregations

Paragraph (com.lowagie.text.Paragraph)7 RtfWriter2 (com.lowagie.text.rtf.RtfWriter2)7 Document (com.lowagie.text.Document)6 Test (org.junit.Test)6 RtfTableOfContents (com.lowagie.text.rtf.field.RtfTableOfContents)3 Anchor (com.lowagie.text.Anchor)2 Chapter (com.lowagie.text.Chapter)2 HeaderFooter (com.lowagie.text.HeaderFooter)2 Phrase (com.lowagie.text.Phrase)2 Table (com.lowagie.text.Table)2 RtfHeaderFooter (com.lowagie.text.rtf.headerfooter.RtfHeaderFooter)2 RtfParagraphStyle (com.lowagie.text.rtf.style.RtfParagraphStyle)2 RtfBorderGroup (com.lowagie.text.rtf.table.RtfBorderGroup)2 RtfCell (com.lowagie.text.rtf.table.RtfCell)2 Color (java.awt.Color)2 Annotation (com.lowagie.text.Annotation)1 Cell (com.lowagie.text.Cell)1 Chunk (com.lowagie.text.Chunk)1 Font (com.lowagie.text.Font)1 Image (com.lowagie.text.Image)1