public void createDoc() throws FileNotFoundException{ /** 创建Document对象(word文档) */ Rectangle rectPageSize = new Rectangle(PageSize.A4); rectPageSize = rectPageSize.rotate(); // 创建word文档,并设置纸张的大小 doc = new Document(PageSize.A4); file=new File(path+docFileName); fileOutputStream=new FileOutputStream(file); /** 建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中 */ RtfWriter2.getInstance(doc, fileOutputStream ); doc.open(); //设置页边距,上、下25.4毫米,即为72f,左、右31.8毫米,即为90f doc.setMargins(90f, 90f, 72f, 72f); //设置标题字体样式,粗体、二号、华文中宋 tfont = DocStyleUtils.setFontStyle("华文中宋", 22f, Font.BOLD); //设置正文内容的字体样式,常规、三号、仿宋_GB2312 bfont = DocStyleUtils.setFontStyle("仿宋_GB2312", 16f, Font.NORMAL); }
/** * Hello World! example * */ @Test public void main() throws Exception { // Step 1: Create a new Document Document document = new Document(); // Step 2: Create a new instance of the RtfWriter2 with the document // and target output stream. RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("HelloWorld.rtf")); // Step 3: Open the document. document.open(); // Step 4: Add content to the document. document.add(new Paragraph("Hello World!")); // Step 5: Close the document. It will be written to the target output // stream. document.close(); }
/** * Document settings example. * * */ @Test public void main() throws Exception { Document document = new Document(); // Keep a reference to the RtfWriter2 instance. RtfWriter2 writer2 = RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("DocumentSettings.rtf")); // Specify that the document caching is to be done on disk. writer2.getDocumentSettings().setDataCacheStyle(RtfDataCache.CACHE_DISK); // Specify that all \n are translated into soft linebreaks. writer2.getDocumentSettings().setAlwaysGenerateSoftLinebreaks(true); document.open(); document.add(new Paragraph("This example has been cached on disk\nand all " + "\\n have been translated into soft linebreaks.")); document.close(); }
/** * Use of paragraph stylesheets. * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("BasicStylesheets.rtf")); document.open(); // Simply set the stylesheet you wish to use as the Font // of the Paragraph document.add(new Paragraph("This is a heading", RtfParagraphStyle.STYLE_HEADING_1)); document.add(new Paragraph("Just some text that is formatted " + "in the default style.", RtfParagraphStyle.STYLE_NORMAL)); document.close(); }
/** * 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(); }
/** * Demonstrates setting text into an RTF drawing object. * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("DrawingText.rtf")); document.open(); // Create a new rectangle RtfShape. RtfShapePosition position = new RtfShapePosition(1000, 1000, 3000, 2000); RtfShape shape = new RtfShape(RtfShape.SHAPE_RECTANGLE, position); // Set the text to display in the drawing object shape.setShapeText("This text will appear in the drawing object."); document.add(shape); document.close(); }
/** * Extended font example. * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("ExtendedFont.rtf")); document.open(); // Create a RtfFont with the desired font name. RtfFont msComicSans = new RtfFont("Comic Sans MS"); // Use the RtfFont like any other Font. document.add(new Paragraph("This paragraph uses the" + " Comic Sans MS font.", msComicSans)); // Font size, font style and font colour can also be specified. RtfFont bigBoldGreenArial = new RtfFont("Arial", 36, Font.BOLD, Color.GREEN); document.add(new Paragraph("This is a really big bold green Arial text", bigBoldGreenArial)); document.close(); }
/** * 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(); }
/** * Changing paragraph stylesheets properties. * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("ChangingStylesheets.rtf")); // Set the default style font name. This is inherited // by all other styles. RtfParagraphStyle.STYLE_NORMAL.setFontName("Times New Roman"); // Set the colour of the level 1 heading to blue. RtfParagraphStyle.STYLE_HEADING_1.setColor(Color.BLUE); // Set the font name of the heading back to Arial again. RtfParagraphStyle.STYLE_HEADING_2.setFontName("Arial"); // Change the font size RtfParagraphStyle.STYLE_HEADING_2.setSize(12); // Change the style properties to the desired values // before document.open() 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.close(); }
/** * Demonstrates creating a footer with the current page number * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("PageNumber.rtf")); // Create a new Paragraph for the footer Paragraph par = new Paragraph("Page "); par.setAlignment(Element.ALIGN_RIGHT); // Add the RtfPageNumber to the Paragraph par.add(new RtfPageNumber()); // Create an RtfHeaderFooter with the Paragraph and set it // as a footer for the document RtfHeaderFooter footer = new RtfHeaderFooter(par); document.setFooter(footer); document.open(); for (int i = 1; i <= 300; i++) { document.add(new Paragraph("Line " + i + ".")); } document.close(); }
/** * Demonstrates creating a header with page number and total number of pages * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("TotalPageNumber.rtf")); // Create a new Paragraph for the footer Paragraph par = new Paragraph("Page "); // Add the RtfPageNumber to the Paragraph par.add(new RtfPageNumber()); // Add the RtfTotalPageNumber to the Paragraph par.add(" of "); par.add(new RtfTotalPageNumber()); // Create an RtfHeaderFooter with the Paragraph and set it // as a header for the document RtfHeaderFooter header = new RtfHeaderFooter(par); document.setHeader(header); document.open(); for (int i = 1; i <= 300; i++) { document.add(new Paragraph("Line " + i + ".")); } document.close(); }
/** * Demonstrates setting the horizontal and vertical anchors for a drawing * object * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("DrawingAnchor.rtf")); document.open(); document.add(new Paragraph("This text is above the horizontal rule")); // Construct a new RtfShapePosition that covers the whole page // horizontally RtfShapePosition position = new RtfShapePosition(150, 0, 10400, 150); // The horizontal position is relative to the margins of the page position.setXRelativePos(RtfShapePosition.POSITION_X_RELATIVE_MARGIN); // The vertical position is relative to the paragraph position.setYRelativePos(RtfShapePosition.POSITION_Y_RELATIVE_PARAGRAPH); // Create a new line drawing object RtfShape shape = new RtfShape(RtfShape.SHAPE_LINE, position); // Add the shape to the paragraph, so that it is anchored to the // correct paragraph Paragraph par = new Paragraph(); par.add(shape); document.add(par); document.add(new Paragraph("This text is below the horizontal rule")); document.close(); }
/** * Demonstrates basic use of RTF drawing objects * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("DrawingObjects.rtf")); document.open(); document.add(new Paragraph("This text will wrap around the shape that\n" + "we will define. Different\n" + "wrapping modes are possible.")); // Create a new RtfShape of the type RECTANGLE. // The position defines the extent of the shape in the page (in Twips) RtfShape shape = new RtfShape(RtfShape.SHAPE_RECTANGLE, new RtfShapePosition(1000, 2000, 3000, 2000)); // Set the line colour to red shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_LINE_COLOR, Color.RED)); // Set the fill colour to cyan shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_FILL_COLOR, Color.CYAN)); // Text wraps around the shape on both sides shape.setWrapping(RtfShape.SHAPE_WRAP_BOTH); // Add the shape to the document document.add(shape); document.close(); }
/** * Demonstrates creating freeform drawing objects * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("DrawingFreeform.rtf")); document.open(); // Create a new rectangle RtfShape using the SHAPE_FREEFORM constant. RtfShapePosition position = new RtfShapePosition(1000, 1000, 4000, 4000); RtfShape shape = new RtfShape(RtfShape.SHAPE_FREEFORM, position); // Set the bottom and right extents of the drawing object. shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_GEO_RIGHT, 3000)); shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_GEO_BOTTOM, 3000)); // Define the vertices that make up the drawing object. // This list draws a basic table shape. shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_VERTICIES, new Point[] { new Point(100, 100), new Point(2900, 100), new Point(2900, 200), new Point(2600, 200), new Point(2600, 1500), new Point(2520, 1500), new Point(2520, 200), new Point(480, 200), new Point(480, 1500), new Point(400, 1500), new Point(400, 200), new Point(100, 200) })); // A nice red Table :-) shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_FILL_COLOR, Color.red)); document.add(shape); document.close(); }
/** * Extended headers / footers example * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("ExtendedHeaderFooter.rtf")); // Create the Paragraphs that will be used in the header. Paragraph date = new Paragraph("01.01.2010"); date.setAlignment(Paragraph.ALIGN_RIGHT); Paragraph address = new Paragraph("TheFirm\nTheRoad 24, TheCity\n" + "+00 99 11 22 33 44"); // Create the RtfHeaderFooter with an array containing the Paragraphs to // add RtfHeaderFooter header = new RtfHeaderFooter(new Element[] { date, address }); // Set the header document.setHeader(header); // Create the table that will be used as the footer Table footer = new Table(2); footer.setBorder(0); footer.getDefaultCell().setBorder(0); footer.setWidth(100); footer.addCell(new Cell("(c) Mark Hall")); Paragraph pageNumber = new Paragraph("Page "); // The RtfPageNumber is an RTF specific element that adds a page number // field pageNumber.add(new RtfPageNumber()); pageNumber.setAlignment(Paragraph.ALIGN_RIGHT); footer.addCell(new Cell(pageNumber)); // Create the RtfHeaderFooter and set it as the footer to use document.setFooter(new RtfHeaderFooter(footer)); document.open(); document.add(new Paragraph("This document has headers and footers created" + " using the RtfHeaderFooter class.")); document.close(); }
/** * Extended font styles example. * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("ExtendedFontStyles.rtf")); document.open(); // Use the RtfFont.STYLE_* instead of the Font styles. RtfFont doubleStrikethrough = new RtfFont("Arial", RtfFont.UNDEFINED, RtfFont.STYLE_DOUBLE_STRIKETHROUGH); RtfFont shadow = new RtfFont("Arial", RtfFont.UNDEFINED, RtfFont.STYLE_SHADOW); // Or combine them with Font styles. RtfFont engravedItalic = new RtfFont("Arial", RtfFont.UNDEFINED, RtfFont.STYLE_ENGRAVED | Font.ITALIC); // The hidden style is special since it hides text. RtfFont hidden = new RtfFont("Arial", RtfFont.UNDEFINED, RtfFont.STYLE_HIDDEN); Paragraph paragraph = new Paragraph("This text is ", new RtfFont("Arial", 12)); // Use the RtfFonts when creating the text. paragraph.add(new Chunk("deleted,", doubleStrikethrough)); paragraph.add(new Chunk(" shady,", shadow)); paragraph.add(new Chunk(" engraved and italic", engravedItalic)); paragraph.add(" and"); paragraph.add(new Chunk(" you won't see this", hidden)); paragraph.add(" nothing."); document.add(paragraph); document.close(); }
public void printRtf()throws IOException, DocumentException{ //create an input stream from the rtf string bytes byte[] rtfBytes = handler.getOBXResult(0, 0).getBytes(); ByteArrayInputStream rtfStream = new ByteArrayInputStream(rtfBytes); //create & open the document we are going to write to and its writer document = new Document(); RtfWriter2 writer = RtfWriter2.getInstance(document,os); document.setPageSize(PageSize.LETTER); document.addTitle("Title of the Document"); document.addCreator("OSCAR"); document.open(); //Create the fonts that we are going to use bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); font = new Font(bf, 11, Font.NORMAL); boldFont = new Font(bf, 12, Font.BOLD); // redFont = new Font(bf, 11, Font.NORMAL, Color.RED); //add the patient information addRtfPatientInfo(); //add the results writer.importRtfDocument(rtfStream, null); document.close(); os.flush(); }
/** * 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; }
/** * Initializes the document writer format. * @param document the document * @param outputStream the export stream * @param exportType the export format * @throws DocumentException if the document cannot be initialized or is not one of the itext supported format */ public static void initDocumentFormat(Document document, ByteArrayOutputStream outputStream, ExportType exportType) throws DocumentException { if (exportType == ExportType.PDF) { PdfWriter.getInstance(document, outputStream); } else if (exportType == ExportType.RTF || exportType == ExportType.DOC) { RtfWriter2.getInstance(document, outputStream); } }
protected void initExport() throws QueryException { if (!bean.isSubreport()) { Padding margins = bean.getReportLayout().getPagePadding(); Rectangle rectangle; if (ReportLayout.CUSTOM.equals(bean.getReportLayout().getPageFormat())) { PaperSize customSize = bean.getReportLayout().getPaperSize(); rectangle = new Rectangle(customSize.getWidthPoints(), customSize.getHeightPoints()); } else { rectangle = PageSize.getRectangle(getPageFormat()); } document = new Document(rectangle, getPoints(margins.getLeft()), getPoints(margins.getRight()), getPoints(margins.getTop()), getPoints(margins.getBottom())); if (bean.getReportLayout().getOrientation() == LANDSCAPE) { document.setPageSize(rectangle.rotate()); } RtfWriter2 writer2 = RtfWriter2.getInstance(document, getOut()); } try { if (!bean.isSubreport()) { buildHeader(); buildFooter(); addMetaData(); document.open(); } table = buildRtfTable(PRINT_DOCUMENT); } catch (DocumentException e) { e.printStackTrace(); throw new QueryException(e); } }
public static void exportDoc(String fileName){ try { Document doc = new Document(); RtfWriter2.getInstance(doc, new FileOutputStream(fileName)); // 打开文档 doc.open(); //设置页边距,上、下25.4毫米,即为72f,左、右31.8毫米,即为90f doc.setMargins(90f, 90f, 72f, 72f); //设置标题字体样式,粗体、二号、华文中宋 Font tfont = DocStyleUtils.setFontStyle("华文中宋", 22f, Font.BOLD); //设置正文内容的字体样式,常规、三号、仿宋_GB2312 Font bfont = DocStyleUtils.setFontStyle("仿宋_GB2312", 16f, Font.NORMAL); //构建标题,居中对齐,12f表示单倍行距 Paragraph title = DocStyleUtils.setParagraphStyle("测试Itext导出Word文档", tfont, 12f, Paragraph.ALIGN_CENTER); //构建正文内容 StringBuffer contentSb = new StringBuffer(); contentSb.append("最近项目很忙,这个是项目中使用到的,所以现在总结一下,以便今后可以参考使用,"); contentSb.append("2011年4月27日 — 2011年5月20日,对以下技术进行使用,"); contentSb.append("Itext、"); contentSb.append("Excel、"); contentSb.append("Word、"); contentSb.append("PPT。"); //首行缩进2字符,行间距1.5倍行距 Paragraph bodyPar = DocStyleUtils.setParagraphStyle(contentSb.toString(), bfont, 32f, 18f); Paragraph bodyEndPar = DocStyleUtils.setParagraphStyle("截至2011年4月28日,各种技术已经完全实现。", bfont, 32f, 18f); //设置空行 Paragraph blankRow = new Paragraph(18f, " ", bfont); Paragraph deptPar = DocStyleUtils.setParagraphStyle("(技术开发部盖章)", bfont, 12f, Paragraph.ALIGN_RIGHT); Paragraph datePar = DocStyleUtils.setParagraphStyle("2011-04-30", bfont, 12f, Paragraph.ALIGN_RIGHT); //向文档中添加内容 doc.add(title); doc.add(blankRow); doc.add(bodyPar); doc.add(bodyEndPar); doc.add(blankRow); doc.add(blankRow); doc.add(blankRow); doc.add(deptPar); doc.add(datePar); //最后一定要记住关闭 doc.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * Returns a PDF, RTF or HTML document. * * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // we retrieve the presentationtype String presentationtype = request.getParameter("presentationtype"); // step 1 Document document = new Document(); try { // step 2: we set the ContentType and create an instance of the corresponding Writer if ("pdf".equals(presentationtype)) { response.setContentType("application/pdf"); PdfWriter.getInstance(document, response.getOutputStream()); } else if ("html".equals(presentationtype)) { response.setContentType("text/html"); HtmlWriter.getInstance(document, response.getOutputStream()); } else if ("rtf".equals(presentationtype)) { response.setContentType("text/rtf"); RtfWriter2.getInstance(document, response.getOutputStream()); } else { response.sendRedirect("http://itextdocs.lowagie.com/tutorial/general/webapp/index.html#HelloWorld"); } // step 3 document.open(); // step 4 document.add(new Paragraph("Hello World")); document.add(new Paragraph(new Date().toString())); } catch(DocumentException de) { de.printStackTrace(); System.err.println("document: " + de.getMessage()); } // step 5: we close the document (the outputstream is also closed internally) document.close(); }
/** * Creates an RTF document with a TOC and Table with special Cellborders. * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2 writer2 = RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("toc.rtf")); writer2.setAutogenerateTOCEntries(true); document.open(); Paragraph para = new Paragraph(); para.add(new RtfTableOfContents("RIGHT CLICK AND HERE AND SELECT \"UPDATE FIELD\" TO UPDATE.")); document.add(para); Paragraph par = new Paragraph("This is some sample content."); Chapter chap1 = new Chapter("Chapter 1", 1); chap1.add(par); Chapter chap2 = new Chapter("Chapter 2", 2); chap2.add(par); document.add(chap1); document.add(chap2); for (int i = 0; i < 300; i++) { if (i == 158) { document.add(new RtfTOCEntry("This is line 158.")); } document.add(new Paragraph("Line " + i)); } document.add(new RtfTOCEntry("Cell border demonstration")); Table table = new Table(3); 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); document.add(table); document.close(); }
/** * 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(); }
/** * Demonstrates setting different text wrapping modes and the z-ordering. * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("DrawingWrap.rtf")); document.open(); Paragraph par = new Paragraph(); for (int i = 0; i < 600; i++) { par.add("bla "); } document.add(par); // Create a new rectangle RtfShape. By default it will be above the text RtfShapePosition position = new RtfShapePosition(1000, 1000, 2000, 2000); RtfShape shape = new RtfShape(RtfShape.SHAPE_RECTANGLE, position); document.add(shape); // Create a rounded rectangle RtfShape and position it below the text position = new RtfShapePosition(4000, 1500, 4500, 5000); position.setShapeBelowText(true); shape = new RtfShape(RtfShape.SHAPE_ROUND_RECTANGLE, position); shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_ADJUST_VALUE, 4500)); shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_FILL_COLOR, Color.GRAY)); document.add(shape); // Create a triangle RtfShape, around which text will wrap on both sides position = new RtfShapePosition(1500, 3000, 4000, 2500); shape = new RtfShape(RtfShape.SHAPE_TRIANGLE_RIGHT, position); shape.setWrapping(RtfShape.SHAPE_WRAP_BOTH); document.add(shape); // Create an elliptical RtfShape, around which text will only wrap on // the left side position = new RtfShapePosition(3000, 6000, 10500, 4500); shape = new RtfShape(RtfShape.SHAPE_ELLIPSE, position); shape.setWrapping(RtfShape.SHAPE_WRAP_LEFT); shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_FILL_COLOR, Color.BLUE)); document.add(shape); // Create a circular RtfShape and set its z-order to 1 position = new RtfShapePosition(5850, 6800, 8200, 7250); position.setZOrder(1); shape = new RtfShape(RtfShape.SHAPE_ELLIPSE, position); shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_FILL_COLOR, Color.RED)); document.add(shape); // Create a star RtfShape and set its z-order to 2, above the circle // defined above position = new RtfShapePosition(6000, 7000, 8000, 7000); position.setZOrder(2); shape = new RtfShape(RtfShape.SHAPE_STAR, position); shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_FILL_COLOR, Color.YELLOW)); document.add(shape); document.close(); }
/** * 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(); }
/** * Extended font example. * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("MultipleHeaderFooter.rtf")); // Create the Paragraph that will be used in the header. Paragraph date = new Paragraph("01.01.2010"); date.setAlignment(Element.ALIGN_CENTER); // Create the RtfHeaderFooterGroup for the header. // To display the same header on both pages, but not the // title page set them to left and right pages explicitly. RtfHeaderFooterGroup header = new RtfHeaderFooterGroup(); header.setHeaderFooter(new RtfHeaderFooter(date), RtfHeaderFooter.DISPLAY_LEFT_PAGES); header.setHeaderFooter(new RtfHeaderFooter(date), RtfHeaderFooter.DISPLAY_RIGHT_PAGES); // Set the header document.setHeader(header); // Create the paragraphs that will be used as footers Paragraph titleFooter = new Paragraph("Multiple headers / footers example"); titleFooter.setAlignment(Element.ALIGN_CENTER); Paragraph leftFooter = new Paragraph("Page "); leftFooter.add(new RtfPageNumber()); Paragraph rightFooter = new Paragraph("Page "); rightFooter.add(new RtfPageNumber()); rightFooter.setAlignment(Element.ALIGN_RIGHT); // Create the RtfHeaderGroup for the footer and set the footers // at the desired positions RtfHeaderFooterGroup footer = new RtfHeaderFooterGroup(); footer.setHeaderFooter(new RtfHeaderFooter(titleFooter), RtfHeaderFooter.DISPLAY_FIRST_PAGE); footer.setHeaderFooter(new RtfHeaderFooter(leftFooter), RtfHeaderFooter.DISPLAY_LEFT_PAGES); footer.setHeaderFooter(new RtfHeaderFooter(rightFooter), RtfHeaderFooter.DISPLAY_RIGHT_PAGES); // Set the document footer document.setFooter(footer); document.open(); document.add(new Paragraph("This document has headers and footers created" + " using the RtfHeaderFooterGroup class.\n\n")); // Add some content, so that the different headers / footers show up. for (int i = 0; i < 300; i++) { document.add(new Paragraph("Just a bit of content so that the headers become visible.")); } document.close(); }
/** * Extended borders for Table Cells. * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("ExtendedTableCell.rtf")); document.open(); Table table = new Table(3); // Create a simple RtfCell with a dotted border. RtfCell cellDotted = new RtfCell("Dotted border"); cellDotted.setBorders(new RtfBorderGroup(Rectangle.BOX, RtfBorder.BORDER_DOTTED, 1, new Color(0, 0, 0))); // Create a simple RtfCell with an embossed border. RtfCell cellEmbossed = new RtfCell("Embossed border"); cellEmbossed.setBorders(new RtfBorderGroup(Rectangle.BOX, RtfBorder.BORDER_EMBOSS, 1, new Color(0, 0, 0))); // Create a simple RtfCell with no border. RtfCell cellNoBorder = new RtfCell("No border"); cellNoBorder.setBorders(new RtfBorderGroup()); cellNoBorder.setRowspan(2); // Create a simple RtfCell that only has a border // on the bottom side. RtfCell bottomBorder = new RtfCell("Bottom border"); bottomBorder.setBorders(new RtfBorderGroup(Rectangle.BOTTOM, RtfBorder.BORDER_SINGLE, 2, new Color(255, 0, 0))); // Create a simple RtfCell that has different borders // on the left and bottom sides. RtfCell mixedBorder = new RtfCell("Mixed border"); RtfBorderGroup mixedBorders = new RtfBorderGroup(); mixedBorders.addBorder(Rectangle.RIGHT, RtfBorder.BORDER_DOUBLE_WAVY, 2, Color.GREEN); mixedBorders.addBorder(Rectangle.BOTTOM, RtfBorder.BORDER_DOT_DASH, 1, Color.BLUE); mixedBorder.setBorders(mixedBorders); // Add the cells to the table table.addCell(cellDotted); table.addCell(cellEmbossed); table.addCell(cellNoBorder); table.addCell(bottomBorder); table.addCell(mixedBorder); document.add(table); document.close(); }
@Override public void export(OutputStream output) throws Exception { loadReport(); // Label displayed instead of the Table of Contents during the export. final String tocLabel = localize("projectReportTableOfContents"); if (report != null) { final ProjectReportVersion version = report.getCurrentVersion(); final ProjectReportDTO reportDTO = GetProjectReportHandler.toDTO(report, version); // Generating the RTF try { final Document document = new Document(); final RtfWriter2 writer = RtfWriter2.getInstance(document, output); writer.setAutogenerateTOCEntries(true); document.open(); // Title final Paragraph titleParagraph = new Paragraph(report.getName()); titleParagraph.getFont().setSize(24); titleParagraph.getFont().setStyle(Font.BOLD); document.add(titleParagraph); document.add(new Paragraph()); // Empty paragraph // Table of contents final Paragraph tocParagraph = new Paragraph(); final RtfTableOfContents toc = new RtfTableOfContents(tocLabel); tocParagraph.add(toc); document.add(tocParagraph); // Sections final List<ProjectReportSectionDTO> sections = reportDTO.getSections(); final StringBuilder prefix = new StringBuilder(); for (int index = 0; index < sections.size(); index++) { final ProjectReportSectionDTO section = sections.get(index); prefix.append(index + 1).append('.'); addSection(section, prefix, index + 1, document); prefix.setLength(0); } document.close(); } catch (DocumentException ex) { LOG.error("An error occured while generating the RTF.", ex); } catch (IOException e) { LOG.debug("An error occured while converting HTML to RTF."); } } else { final String idAsString = requireParameter(RequestParameter.ID); LOG.error("[export] No project report is identified by '" + idAsString + "'."); throw new Exception("[export] No project report is identified by '" + idAsString + "'."); } }
/** * Demonstrates adding a soft linebreak to a Paragraph * * */ @Test public void main() throws Exception { Document document = new Document(); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("SoftLineBreak.rtf")); document.open(); document.add(new Paragraph("This is just a paragraph.")); Paragraph par = new Paragraph(); // Set the spacings just to demonstrate that the soft linebreak // does not cause spacing before or after par.setSpacingBefore(10); par.setSpacingAfter(10); // Add the contents before the linebreak par.add("This paragraph contains a soft linebreak"); // Add the soft linebreak par.add(RtfDirectContent.DIRECT_SOFT_LINEBREAK); // Add the contents after the linebreak par.add("just before the just."); // Add the paragraph to the document document.add(par); document.add(new Paragraph("This is just a paragraph.")); document.close(); }
/** * main logic to create the rtf report * * @param urn * the urn specification we report on * @param mapDiagrams * list of diagrams to be documented * @param filename * the report filename */ public void export(URNspec urn, HashMap mapDiagrams, String filename) throws InvocationTargetException { // Create a report document with page size from preferences, 72 points per inch String sReportHeight = ReportGeneratorPreferences.getHeight(); fHeight = Float.parseFloat(sReportHeight) * 72; String sReportWidth = ReportGeneratorPreferences.getWidth(); fWidth = Float.parseFloat(sReportWidth) * 72; pagesize = new Rectangle(fWidth, fHeight); Document document = new Document(pagesize); try { RtfWriter2.getInstance(document, new FileOutputStream(filename)); document.open(); document.add(Chunk.NEWLINE); document.add(Chunk.NEWLINE); super.export(urn, mapDiagrams, filename, document, pagesize); if (!urndef.getSpecDiagrams().isEmpty()) { RTFReportDiagram reportDiagrams = new RTFReportDiagram(); reportDiagrams.createRTFReportDiagramsAndDescription(document, urndef, mapDiagrams, pagesize); } super.writeScenarioDocumentation(document, ucmspec); document.close(); } catch (Exception e) { jUCMNavErrorDialog error = new jUCMNavErrorDialog(e.getMessage()); e.printStackTrace(); } }