Java Code Examples for com.lowagie.text.rtf.RtfWriter2#getInstance()
The following examples show how to use
com.lowagie.text.rtf.RtfWriter2#getInstance() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DrawingTextTest.java From itext2 with GNU Lesser General Public License v3.0 | 7 votes |
/** * 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(); }
Example 2
Source File: HelloWorldTest.java From itext2 with GNU Lesser General Public License v3.0 | 6 votes |
/** * 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(); }
Example 3
Source File: BasicTabsTest.java From itext2 with GNU Lesser General Public License v3.0 | 6 votes |
/** * 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(); }
Example 4
Source File: DocExportUtil.java From DWSurvey with GNU Affero General Public License v3.0 | 6 votes |
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); }
Example 5
Source File: DrawingFreeformTest.java From itext2 with GNU Lesser General Public License v3.0 | 5 votes |
/** * 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(); }
Example 6
Source File: DrawingObjectsTest.java From itext2 with GNU Lesser General Public License v3.0 | 5 votes |
/** * 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(); }
Example 7
Source File: DrawingAnchorTest.java From itext2 with GNU Lesser General Public License v3.0 | 5 votes |
/** * 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(); }
Example 8
Source File: ExtendedFontStylesTest.java From itext2 with GNU Lesser General Public License v3.0 | 5 votes |
/** * 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(); }
Example 9
Source File: MRtfWriter.java From javamelody with Apache License 2.0 | 5 votes |
/** * 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; }
Example 10
Source File: PageNumberTest.java From itext2 with GNU Lesser General Public License v3.0 | 5 votes |
/** * 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(); }
Example 11
Source File: TotalPageNumberTest.java From itext2 with GNU Lesser General Public License v3.0 | 5 votes |
/** * 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(); }
Example 12
Source File: ExtendingStylesheetsTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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(); }
Example 13
Source File: DrawingWrapTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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(); }
Example 14
Source File: MultipleHeaderFooterTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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(); }
Example 15
Source File: ExtendedHeaderFooterTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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(); }
Example 16
Source File: RtfTOCandCellbordersTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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(); }
Example 17
Source File: TablePdfPTableTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * Example that is used to test the TableAttributes class. * * @param args * no arguments needed */ @Test public void main() throws Exception { // creation of the document with a certain size and certain margins Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50); // creation of the different writers PdfWriter.getInstance(document, PdfTestBase.getOutputStream("tableattributes.pdf")); RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("tableattributes.rtf")); // open the document document.open(); // add content SimpleTable table = new SimpleTable(); table.setCellpadding(5); table.setCellspacing(8); SimpleCell row = new SimpleCell(SimpleCell.ROW); row.setBackgroundColor(Color.yellow); SimpleCell cell = new SimpleCell(SimpleCell.CELL); cell.setWidth(100f); cell.add(new Paragraph("rownumber")); row.add(cell); cell = new SimpleCell(SimpleCell.CELL); cell.setWidth(50f); cell.add(new Paragraph("A")); row.add(cell); cell = new SimpleCell(SimpleCell.CELL); cell.setWidth(50f); cell.add(new Paragraph("B")); row.add(cell); cell = new SimpleCell(SimpleCell.CELL); cell.setWidth(50f); cell.add(new Paragraph("C")); row.add(cell); table.addElement(row); for (int i = 0; i < 100; i++) { row = new SimpleCell(SimpleCell.ROW); switch (i % 3) { case 0: row.setBackgroundColor(Color.red); break; case 1: row.setBackgroundColor(Color.green); break; case 2: row.setBackgroundColor(Color.blue); break; } if (i % 2 == 1) { row.setBorderWidth(3f); } cell = new SimpleCell(SimpleCell.CELL); cell.add(new Paragraph("Row " + (i + 1))); cell.setSpacing_left(20f); row.add(cell); if (i % 5 == 4) { cell = new SimpleCell(SimpleCell.CELL); cell.setColspan(3); cell.setBorderColor(Color.orange); cell.setBorderWidth(5f); cell.add(new Paragraph("Hello!")); cell.setHorizontalAlignment(Element.ALIGN_CENTER); row.add(cell); } else { cell = new SimpleCell(SimpleCell.CELL); cell.add(new Paragraph("A")); row.add(cell); cell = new SimpleCell(SimpleCell.CELL); cell.add(new Paragraph("B")); cell.setBackgroundColor(Color.gray); row.add(cell); cell = new SimpleCell(SimpleCell.CELL); cell.add(new Paragraph("C")); row.add(cell); } table.addElement(row); } document.add(table); // we close the document document.close(); }
Example 18
Source File: TableOfContentsTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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(); }
Example 19
Source File: HelloWorldServlet.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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(); }
Example 20
Source File: TestDoc.java From DWSurvey with GNU Affero General Public License v3.0 | 4 votes |
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(); } }