Exemple #1
0
        public Chap0702()
        {
            Console.WriteLine("Chapter 7 example 2: parsing the result of example 1");

            // step 1: creation of a document-object
            Document document = new Document();

            try
            {
                // step 2:
                // we create a writer that listens to the document
                // and directs a XML-stream to a file
                PdfWriter.GetInstance(document, new FileStream("Chap0702.pdf", FileMode.Create));
                HtmlWriter.GetInstance(document, new FileStream("Chap0702.htm", FileMode.Create));
                RtfWriter.GetInstance(document, new FileStream("Chap0702.rtf", FileMode.Create));
                // step 3: we create a parser

                ITextHandler h = new ITextHandler(document);

                // step 4: we parse the document
                h.Parse("Chap0701.xml");
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.StackTrace);
                Console.Error.WriteLine(e.Message);
            }
        }
Exemple #2
0
        public Chap0801()
        {
            Console.WriteLine("Chapter 8 example 1: Hello World");

            // step 1: creation of a document-object
            Document document = new Document();

            try
            {
                // step 2:
                // we create a writer that listens to the document
                // and directs a RTF-stream to a file

                RtfWriter.GetInstance(document, new FileStream("Chap0801.rtf", FileMode.Create));

                // step 3: we open the document
                document.Open();

                // step 4: we add a paragraph to the document
                document.Add(new Paragraph("Hello World"));
            }
            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Console.Error.WriteLine(ioe.Message);
            }

            // step 5: we close the document
            document.Close();
        }
Exemple #3
0
        public Chap0804()
        {
            Console.WriteLine("Chapter 8 example 4: Tables and RTF");
            // step 1: creation of a document-object
            Document document = new Document();

            try
            {
                // step 2:
                // we create a writer that listens to the document
                // and directs a PDF-stream to a file
                RtfWriter.GetInstance(document, new FileStream("Chap0804.rtf", FileMode.Create));
                // step 3: we open the document
                document.Open();
                // step 4: we create a table and add it to the document
                Table table = new Table(3);
                table.BorderWidth = 1;
                table.BorderColor = new Color(0, 0, 255);
                table.Padding     = 5;
                table.Spacing     = 5;
                Cell cell = new Cell("header");
                cell.Header  = true;
                cell.Colspan = 3;
                table.AddCell(cell);
                cell             = new Cell("example cell with colspan 1 and rowspan 2");
                cell.Rowspan     = 2;
                cell.BorderColor = new Color(255, 0, 0);
                table.AddCell(cell);
                table.AddCell("1.1");
                table.AddCell("2.1");
                table.AddCell("1.2");
                table.AddCell("2.2");
                table.AddCell("cell test1");
                cell                 = new Cell("big cell");
                cell.Rowspan         = 2;
                cell.Colspan         = 2;
                cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
                table.AddCell(cell);
                table.AddCell("cell test2");
                document.Add(table);
            }
            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Console.Error.WriteLine(ioe.Message);
            }
            // step 5: we close the document
            document.Close();
        }
Exemple #4
0
        public Chap0803()
        {
            Console.WriteLine("Chapter 8 example 3: RTF with the RtfHeaderFooters class");

            /* Create a new document */
            Document document = new Document(PageSize.A4);

            try
            {
                /* Create a RtfWriter and a PdfWriter for the document */
                RtfWriter rtf = RtfWriter.GetInstance(document, new FileStream("Chap0803.rtf", FileMode.Create));

                /* We specify that the RTF file has a Title Page */
                rtf.SetHasTitlePage(true);

                /* We create headers and footers for the RTF file */
                RtfHeaderFooters header = new RtfHeaderFooters();
                RtfHeaderFooters footer = new RtfHeaderFooters();

                /* We add a header that will only appear on the first page */
                header.Set(RtfHeaderFooters.FIRST_PAGE, new HeaderFooter(new Phrase("This header is only on the first page"), false));
                /* We add a header that will only appear on left-side pages */
                header.Set(RtfHeaderFooters.LEFT_PAGES, new HeaderFooter(new Phrase("This header is only on left pages"), false));
                /* We add a header that will only appear on right-side pages */
                header.Set(RtfHeaderFooters.RIGHT_PAGES, new HeaderFooter(new Phrase("This header is only on right pages. "), false));

                /* We add a footer that will appear on all pages except the first (because of the title page)
                 * Because the header has different left and right page footers, we have to add the footer to
                 * both the left and right pages. */
                footer.Set(RtfHeaderFooters.LEFT_PAGES, new HeaderFooter(new Phrase("This footer is on all pages except the first. Page: "), true));
                footer.Set(RtfHeaderFooters.RIGHT_PAGES, new HeaderFooter(new Phrase("This footer is on all pages except the first. Page: "), true));

                /* Open the document */
                document.Open();


                /* We add the header and footer */
                document.Header = header;
                document.Footer = footer;


                /* We add some content */
                Chapter chapter = new Chapter(new Paragraph("Advanced RTF headers and footers", new Font(Font.HELVETICA, 16, Font.BOLD)), 1);

                chapter.Add(new Paragraph("This document demonstrates the use of advanced RTF headers and footers."));

                for (int i = 0; i < 300; i++)
                {
                    chapter.Add(new Paragraph("Line " + i));
                }
                document.Add(chapter);
            }
            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Console.Error.WriteLine(ioe.Message);
            }

            /* Close the document */
            document.Close();
        }
Exemple #5
0
        private MemoryStream ITEXTRender(DataTable dt, bool rtf)
        {
            Document document = new Document(PageSize.A4, 80, 50, 30, 65);

            document.AddAuthor("Tustena CRM");


            MemoryStream ms = new MemoryStream();

            if (rtf)
            {
                RtfWriter writerA = RtfWriter.GetInstance(document, ms);
            }
            else
            {
                PdfWriter writerA = PdfWriter.GetInstance(document, ms);
                writerA.ViewerPreferences = PdfWriter.PageLayoutSinglePage;
            }

            HeaderFooter header = new HeaderFooter(new Phrase("Tustena Report", FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.BOLD)), false);
            HeaderFooter footer = new HeaderFooter(new Phrase("page ", FontFactory.GetFont(FontFactory.HELVETICA, 11)), true);

            document.Header = header;
            document.Footer = footer;
            document.Open();
            document.SetPageSize(PageSize.A4);
            Cell cell;

            if (dt.Rows.Count > 0)
            {
                Table table = new Table(dt.Columns.Count);
                table.TableFitsPage              = false;
                table.WidthPercentage            = 100;
                table.SpaceInsideCell            = 2;
                table.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
                table.DefaultVerticalAlignment   = Element.ALIGN_MIDDLE;
                foreach (DataColumn cc in dt.Columns)
                {
                    string cellName = (cc.ColumnName.Substring(0, 3) == "{+}" || cc.ColumnName.Substring(0, 3) == "{t}") ? cc.ColumnName.Substring(3, cc.ColumnName.Length - 3) : cc.ColumnName;
                    cell = new Cell(cellName.ToString());
                    cell.BackgroundColor     = new Color(200, 200, 200);
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    cell.VerticalAlignment   = Element.ALIGN_MIDDLE;
                    cell.Header = true;
                    table.AddCell(cell);
                }
                table.EndHeaders();
                double[] sum     = new double[dt.Columns.Count];
                bool[]   sumType = new bool[dt.Columns.Count];
                bool     boolSum = false;
                foreach (DataRow rowname in dt.Rows)
                {
                    int i        = 0;
                    int indexSum = 0;
                    foreach (DataColumn cc in dt.Columns)
                    {
                        if (cc.ColumnName.Substring(0, 3) == "{+}" || cc.ColumnName.Substring(0, 3) == "{t}")
                        {
                            sum[indexSum]    += Convert.ToDouble(rowname[cc.ColumnName]);
                            sumType[indexSum] = (cc.ColumnName.Substring(0, 3) == "{t}"); // true = tempo , false = numerico
                            boolSum           = true;
                        }
                        indexSum++;
                        string timeToPrint = String.Empty;
                        if (cc.ColumnName.Substring(0, 3) == "{t}")
                        {
                            int duration = Convert.ToInt32(rowname[cc.ColumnName]);
                            if (duration > 0)
                            {
                                if (duration < 60)
                                {
                                    timeToPrint = "00:" + ((duration > 9) ? duration.ToString() : "0" + duration.ToString());
                                }
                                else
                                {
                                    timeToPrint = ((Convert.ToInt32(duration / 60) > 9) ? Convert.ToInt32(duration / 60).ToString() : "0" + Convert.ToInt32(duration / 60).ToString()) + ":" +
                                                  ((Convert.ToInt32(duration % 60) > 9) ? Convert.ToInt32(duration % 60).ToString() : "0" + Convert.ToInt32(duration % 60).ToString());
                                }
                            }
                        }
                        if (cc.ColumnName.Substring(0, 3) == "{+}" || cc.ColumnName.Substring(0, 3) == "{t}")
                        {
                            if (cc.ColumnName.Substring(0, 3) == "{t}")
                            {
                                cell = new Cell(timeToPrint);
                            }
                            else
                            {
                                cell = new Cell(rowname[cc.ColumnName].ToString());
                            }
                        }
                        else
                        {
                            cell = new Cell(rowname[cc.ColumnName].ToString());
                        }
                        if ((i++ % 2) == 0)
                        {
                            cell.BackgroundColor = new Color(230, 230, 230);
                        }
                        table.AddCell(cell);
                    }
                }
                if (boolSum)
                {
                    for (int i = 0; i < sum.Length; i++)
                    {
                        if (sum[i] > 0)
                        {
                            if (sumType[i])
                            {
                                int    duration    = Convert.ToInt32(sum[i]);
                                string timeToPrint = String.Empty;
                                if (duration > 0)
                                {
                                    if (duration < 60)
                                    {
                                        timeToPrint = "00:" + ((duration > 9) ? duration.ToString() : "0" + duration.ToString());
                                    }
                                    else
                                    {
                                        timeToPrint = ((Convert.ToInt32(duration / 60) > 9) ? Convert.ToInt32(duration / 60).ToString() : "0" + Convert.ToInt32(duration / 60).ToString()) + ":" +
                                                      ((Convert.ToInt32(duration % 60) > 9) ? Convert.ToInt32(duration % 60).ToString() : "0" + Convert.ToInt32(duration % 60).ToString());
                                    }
                                }

                                cell = new Cell(timeToPrint);
                            }
                            else
                            {
                                cell = new Cell(sum[i].ToString());
                            }
                        }
                        else
                        {
                            cell = new Cell("");
                        }

                        cell.BackgroundColor = new Color(200, 200, 200);
                        table.AddCell(cell);
                    }
                }
                document.Add(table);
            }
            document.Close();
            return(ms);
        }