Add() public method

public Add ( IElement o ) : bool
o IElement
return bool
    public void NestedListAtTheEndOfAnotherNestedList() {
        String pdfFile = "nestedListAtTheEndOfAnotherNestedList.pdf";
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.GetInstance(document, File.Create(DEST_FOLDER + pdfFile));
        // step 3
        document.Open();
        // step 4
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell();
        cell.BackgroundColor = BaseColor.ORANGE;

        RomanList romanlist = new RomanList(true, 20);
        romanlist.IndentationLeft = 10f;
        romanlist.Add("One");
        romanlist.Add("Two");
        romanlist.Add("Three");

        RomanList romanlist2 = new RomanList(true, 20);
        romanlist2.IndentationLeft = 10f;
        romanlist2.Add("One");
        romanlist2.Add("Two");
        romanlist2.Add("Three");

        romanlist.Add(romanlist2);
        //romanlist.add("Four");

        List list = new List(List.ORDERED, 20f);
        list.SetListSymbol("\u2022");
        list.IndentationLeft = 20f;
        list.Add("One");
        list.Add("Two");
        list.Add("Three");
        list.Add("Four");
        list.Add("Roman List");
        list.Add(romanlist);

        list.Add("Five");
        list.Add("Six");

        cell.AddElement(list);
        table.AddCell(cell);
        document.Add(table);
        // step 5
        document.Close();

        CompareTool compareTool = new CompareTool();
        String error = compareTool.CompareByContent(DEST_FOLDER + pdfFile, SOURCE_FOLDER + pdfFile, DEST_FOLDER, "diff_");
        if (error != null) {
            Assert.Fail(error);
        }
    }
Ejemplo n.º 2
0
// ===========================================================================
    public void Write(Stream stream) {
      // step 1
      using (Document document = new Document()) {
        // step 2
        PdfWriter.GetInstance(document, stream);
        // step 3
        document.Open();
        // step 4
        var SQL = 
@"SELECT DISTINCT mc.country_id, c.country, count(*) AS c
FROM film_country c, film_movie_country mc
WHERE c.id = mc.country_id
GROUP BY mc.country_id, country ORDER BY c DESC";  
        // Create a list for the countries
        List list = new RomanList();
        // loop over the countries
        using (var c =  AdoDB.Provider.CreateConnection()) {
          c.ConnectionString = AdoDB.CS;
          using (DbCommand cmd = c.CreateCommand()) {
            cmd.CommandText = SQL;        
            c.Open();            
            using (var r = cmd.ExecuteReader()) {
              while (r.Read()) {
              // create a list item for the country
                ListItem item = new ListItem(
                  string.Format("{0}: {1} movies",
                    r["country"].ToString(), r["c"].ToString()
                  )
                );
                // Create a list for the movies
                List movielist = new GreekList();
                movielist.Lowercase = List.LOWERCASE;
                // Loop over the movies
                foreach (Movie movie in 
                    PojoFactory.GetMovies(r["country_id"].ToString())
                ) {
                  ListItem movieitem = new ListItem(movie.MovieTitle);
                  // Create a list for the directors
                  List directorlist = new ZapfDingbatsNumberList(0); 
                  // Loop over the directors                 
                  foreach (Director director in movie.Directors) {
                    directorlist.Add(String.Format("{0}, {1}",
                      director.Name, director.GivenName
                    ));
                  }
                  movieitem.Add(directorlist);
                  movielist.Add(movieitem);
                }
                item.Add(movielist);
                list.Add(item);
              }
              document.Add(list);
            }
          }
        }
      }
    }
Ejemplo n.º 3
0
        static void MainExample()
        {
            var doc = new Document ();
            PdfWriter.GetInstance(doc, new FileStream(@"Document.pdf", FileMode.Create));
            doc.Open();

            //Normal text
            string text = "Normal Text. ";
            Paragraph par = new Paragraph ();
            par.FirstLineIndent = firstLineIndent;
            par.SpacingAfter = spacingAfter;
            for (int i = 0; i < 15; i++)
            {
                par.Add (text);
            }
            doc.Add (par);

            //Bold text
            text = "Bold Text. ";
            BaseFont baseFont = BaseFont.CreateFont (BaseFont.HELVETICA_BOLD, BaseFont.CP1257, false);
            iTextSharp.text.Font font = new iTextSharp.text.Font (baseFont, textFontSize);
            Chunk chunk = new Chunk (text, font);
            par = new Paragraph ();
            par.FirstLineIndent = firstLineIndent;
            par.SpacingAfter = spacingAfter;
            for (int i = 0; i < 15; i++)
            {
                par.Add (chunk);
            }
            doc.Add (par);

            //Italic Text
            text = "Italic Text. ";
            baseFont = BaseFont.CreateFont (BaseFont.TIMES_ITALIC, BaseFont.CP1257, false);
            font = new iTextSharp.text.Font (baseFont, textFontSize);
            chunk = new Chunk (text, font);
            par = new Paragraph ();
            par.FirstLineIndent = firstLineIndent;
            par.SpacingAfter = spacingAfter;
            for (int i = 0; i < 15; i++)
            {
                par.Add (chunk);
            }
            doc.Add (par);

            //Underline text
            text = "Underline Text. ";
            baseFont = BaseFont.CreateFont (BaseFont.HELVETICA, BaseFont.CP1257, false);
            font = new iTextSharp.text.Font (baseFont, textFontSize, iTextSharp.text.Font.UNDERLINE);
            chunk = new Chunk (text, font);
            par = new Paragraph ();
            par.FirstLineIndent = firstLineIndent;
            par.SpacingAfter = spacingAfter;
            for (int i = 0; i < 15; i++)
            {
                par.Add (chunk);
            }
            doc.Add (par);

            //Strikethrough text
            text = "Strikethrough Text. ";
            baseFont = BaseFont.CreateFont (BaseFont.HELVETICA, BaseFont.CP1257, false);
            font = new iTextSharp.text.Font (baseFont, textFontSize, iTextSharp.text.Font.STRIKETHRU);
            chunk = new Chunk (text, font);
            par = new Paragraph ();
            par.FirstLineIndent = firstLineIndent;
            par.SpacingAfter = spacingAfter;
            for (int i = 0; i < 15; i++)
            {
                par.Add (chunk);
            }
            doc.Add (par);

            //Hyperlink text
            text = "Hyperlink Text. ";
            baseFont = BaseFont.CreateFont (BaseFont.HELVETICA, BaseFont.CP1257, false);
            //font = new iTextSharp.text.Font (baseFont, textFontSize, iTextSharp.text.Font.UNDERLINE);
            font = FontFactory.GetFont("Arial", textFontSize, iTextSharp.text.Font.UNDERLINE, new iTextSharp.text.BaseColor(0, 0, 255));
            Anchor link = new Anchor (text, font);
            link.Reference = "http://google.com";
            par = new Paragraph ();
            par.FirstLineIndent = firstLineIndent;
            par.SpacingAfter = spacingAfter;
            for (int i = 0; i < 15; i++)
            {
                par.Add (link);
            }
            doc.Add (par);

            //Russian Text
            //Warning
            text = "Русский Текст. ";
            font = FontFactory.GetFont(GetFontByName("Arial"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            chunk = new Chunk (text, font);
            par = new Paragraph ();
            par.FirstLineIndent = firstLineIndent;
            par.SpacingAfter = spacingAfter;
            for (int i = 0; i < 15; i++)
            {
                par.Add (chunk);
            }
            doc.Add (par);

            //Bold & Italic & Underline & Hyperlink Text
            text = "Bold & Italic & Underline & Hyperlink Text. ";
            font = FontFactory.GetFont(
                GetFontByName("Times New Roman"),
                textFontSize,
                iTextSharp.text.Font.UNDERLINE | iTextSharp.text.Font.BOLD | iTextSharp.text.Font.ITALIC,
                new iTextSharp.text.BaseColor(0, 0, 255)
            );
            link = new Anchor (text, font);
            link.Reference = "http://google.com";
            par = new Paragraph ();
            par.FirstLineIndent = firstLineIndent;
            par.SpacingAfter = spacingAfter;
            for (int i = 0; i < 7; i++)
            {
                par.Add (link);
            }
            doc.Add (par);

            //Before (After) line break
            text="Before line break. ";
            par = new Paragraph ();
            par.FirstLineIndent = firstLineIndent;
            par.SpacingAfter = spacingAfter;
            for (int i = 0; i < 13; i++)
            {
                par.Add (text);
            }
            par.Add (Chunk.NEWLINE);
            par.Add ("After line break.");
            doc.Add (par);

            //Span text
            text="Span text. ";
            par = new Paragraph ();
            par.FirstLineIndent = firstLineIndent;
            par.SpacingAfter = spacingAfter;
            Font font1 = FontFactory.GetFont (GetFontByName ("Arial"));
            Font font2 = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, Font.BOLD);
            Font font3 = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, Font.ITALIC);
            Font font4 = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, Font.UNDERLINE);
            Font font5 = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, Font.UNDERLINE, new BaseColor(0, 0, 255));
            link = new Anchor (text, font5);
            link.Reference = "http://google.com";
            par.Add (new Chunk (text, font1));
            par.Add (new Chunk (text, font2));
            par.Add (new Chunk (text, font3));
            par.Add (new Chunk (text, font4));
            par.Add (" ");
            par.Add (link);
            doc.Add (par);

            //Image
            par = new Paragraph ();
            par.SpacingAfter = spacingAfter;

            Image jpg1 = Image.GetInstance ("images.jpg");
            Image jpg2 = Image.GetInstance ("images.jpg");
            Image jpg3 = Image.GetInstance ("images.jpg");
            Image jpg4 = Image.GetInstance ("images.jpg");

            jpg1.ScalePercent (scalePercent);
            jpg1.RotationDegrees = 0;

            jpg2.ScalePercent (scalePercent);
            jpg2.RotationDegrees = -90;

            jpg3.ScalePercent (scalePercent);
            jpg3.RotationDegrees = -180;

            jpg4.ScalePercent (scalePercent);
            jpg4.RotationDegrees = -270;

            par.Add (new Chunk (jpg1, 0, 0, true));
            par.Add (new Chunk (jpg2, 0, 0, true));
            par.Add (new Chunk (jpg3, 0, 0, true));
            par.Add (new Chunk (jpg4, 0, 0, true));

            doc.Add (par);

            //Page break
            doc.Add (new Paragraph("Before page break."));
            doc.Add (Chunk.NEXTPAGE);
            doc.Add (new Paragraph("After page break."));

            //List with 'None' marker style:
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            par.Add ("List with 'None' marker style:");
            doc.Add(par);
            List list = new List (List.UNORDERED, symbolIndent);
            list.SetListSymbol ("");
            list.IndentationLeft = indentationLeft;
            list.Add ("Item1");
            list.Add ("Item2");
            list.Add ("Item3");
            doc.Add (list);

            //List with 'Disc' marker style:
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            par.Add ("List with 'Disc' marker style:");
            doc.Add(par);
            list = new List (List.UNORDERED, symbolIndent);
            list.SetListSymbol (symbolDisk);
            list.IndentationLeft = indentationLeft;
            list.Add ("Item1");
            list.Add ("Item2");
            list.Add ("Item3");
            doc.Add (list);

            //List with 'Box' marker style:
            //Not working...
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            par.Add ("List with 'Box' marker style:");
            doc.Add(par);
            list = new List (List.UNORDERED, symbolIndent);
            list.SetListSymbol (symbolBox);
            list.IndentationLeft = indentationLeft;
            list.Add ("Item1");
            list.Add ("Item2");
            list.Add ("Item3");
            doc.Add (list);

            //List with 'LowerRoman' marker style:
            //Warning
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            par.Add ("List with 'LowerRoman' marker style:");
            doc.Add(par);
            RomanList romanList = new RomanList (true, symbolIndent); //true == Roman Lower
            romanList.IndentationLeft = indentationLeft;
            romanList.Add ("Item1");
            romanList.Add ("Item2");
            romanList.Add ("Item3");
            doc.Add (romanList);

            //List with 'UpperRoman' marker style:
            //Warning
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            par.Add ("List with 'UpperRoman' marker style:");
            doc.Add(par);
            romanList = new RomanList (false, symbolIndent); //true == Roman Lower
            romanList.IndentationLeft = indentationLeft;
            romanList.Add ("Item1");
            romanList.Add ("Item2");
            romanList.Add ("Item3");
            romanList.Add ("Item4");
            romanList.Add ("Item5");
            romanList.Add ("Item6");
            doc.Add (romanList);

            //Table
            //http://www.mikesdotnetting.com/Article/86/itextsharp-introducing-tables
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            par.Add ("Table:");
            doc.Add(par);
            PdfPTable table = new PdfPTable (5); //5 Columns

            table.TotalWidth = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin;
            table.LockedWidth = true;
            float columt1Width = 50f;
            float columt2Width = 80f;
            float delta = (table.TotalWidth - columt1Width - columt2Width) / 3;
            float[] widths = { columt1Width, columt2Width, delta, delta, delta };
            table.SetWidths (widths);

            table.SpacingBefore = spacingAfter;

            PdfPCell cell = new PdfPCell(new Phrase("Colspan = Rowspan = 2"));
            cell.Colspan = 2;
            cell.Rowspan = 2;
            table.AddCell (cell);

            cell = new PdfPCell (new Phrase("Rowspan = 4"));
            cell.Rowspan = 4;
            table.AddCell (cell);

            cell = new PdfPCell (new Phrase("Colspan = 2"));
            cell.Colspan = 2;
            table.AddCell (cell);

            for (int i = 0; i < 20; i++)
                table.AddCell ("Text " + (i + 1).ToString ());

            doc.Add (table);

            //Before (After) line
            //Warning
            par = new Paragraph("Before line");
            par.SpacingBefore = spacingAfter;
            doc.Add (par);

            cell = new PdfPCell();
            cell.BorderWidth = 0f;
            cell.BorderWidthBottom = 0.5f;
            table = new PdfPTable (1);
            table.AddCell (cell);
            table.TotalWidth = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin;
            table.LockedWidth = true;
            table.SpacingBefore = spacingAfter;
            doc.Add (table);

            par = new Paragraph("After line");
            doc.Add (par);

            //Fonts (Arial)
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            text="Arial 10pt. ";
            font = FontFactory.GetFont (GetFontByName ("Arial"), 10);
            par.Add (new Chunk (text, font));
            text="Arial 15pt. ";
            font = FontFactory.GetFont (GetFontByName ("Arial"), 15);
            par.Add (new Chunk (text, font));
            text="Arial 20pt. ";
            font = FontFactory.GetFont (GetFontByName ("Arial"), 20);
            par.Add (new Chunk (text, font));
            doc.Add (par);

            //Fonts (Tahoma)
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            text="Tahoma 10pt. ";
            font = FontFactory.GetFont (GetFontByName ("Tahoma"), 10);
            par.Add (new Chunk (text, font));
            text="Tahoma 15pt. ";
            font = FontFactory.GetFont (GetFontByName ("Tahoma"), 15);
            par.Add (new Chunk (text, font));
            text="Tahoma 20pt. ";
            font = FontFactory.GetFont (GetFontByName ("Tahoma"), 20);
            par.Add (new Chunk (text, font));
            doc.Add (par);

            //Fonts (Courier New)
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            text="Courier New 10pt. ";
            font = FontFactory.GetFont (GetFontByName ("Courier New"), 10);
            par.Add (new Chunk (text, font));
            text="Courier New 15pt. ";
            font = FontFactory.GetFont (GetFontByName ("Courier New"), 15);
            par.Add (new Chunk (text, font));
            text="Courier New 20pt. ";
            font = FontFactory.GetFont (FontFactory.COURIER, 20); //GetFontByName ("Courier New") == FontFactory.COURIER
            par.Add (new Chunk (text, font));
            doc.Add (par);

            //Normal, Italic, Oblique
            //Not Finished
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            par.SpacingAfter = spacingAfter;
            text = "Normal. ";
            font = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, Font.NORMAL);
            par.Add (new Chunk (text, font));
            text = "Italic. ";
            font = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, Font.ITALIC);
            par.Add (new Chunk (text, font));
            text = "Oblique. (Not Working)";
            font = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize);
            par.Add (new Chunk (text, font));
            doc.Add (par);

            //Normal. Subscript. Superscript.
            //H 2 O
            doc.Add(new Paragraph("Normal. Subscript. Superscript."));
            par = new Paragraph ();
            par.SpacingBefore = spacingAfter;
            chunk = new Chunk ("H", FontFactory.GetFont (GetFontByName ("Arial")));
            par.Add (chunk);
            chunk = new Chunk ("2", FontFactory.GetFont (GetFontByName ("Arial")));
            chunk.SetTextRise (-5f);
            par.Add (chunk);
            chunk = new Chunk ("O", FontFactory.GetFont (GetFontByName ("Arial")));
            par.Add (chunk);
            doc.Add (par);
            //C 2 H 5 OH
            par = new Paragraph ();
            par.SpacingBefore = spacingAfter;
            chunk = new Chunk ("C", FontFactory.GetFont (GetFontByName ("Arial")));
            par.Add (chunk);
            chunk = new Chunk ("2", FontFactory.GetFont (GetFontByName ("Arial")));
            chunk.SetTextRise (-5f);
            par.Add (chunk);
            chunk = new Chunk ("H", FontFactory.GetFont (GetFontByName ("Arial")));
            par.Add (chunk);
            chunk = new Chunk ("5", FontFactory.GetFont (GetFontByName ("Arial")));
            chunk.SetTextRise (-5f);
            par.Add (chunk);
            chunk = new Chunk ("OH", FontFactory.GetFont (GetFontByName ("Arial")));
            par.Add (chunk);
            doc.Add (par);
            //a^2+b^2=c^2
            par = new Paragraph ();
            par.SpacingBefore = spacingAfter;
            chunk = new Chunk ("a", FontFactory.GetFont (GetFontByName ("Arial")));
            par.Add (chunk);
            chunk = new Chunk ("2", FontFactory.GetFont (GetFontByName ("Arial")));
            chunk.SetTextRise (5f);
            par.Add (chunk);
            chunk = new Chunk (" + b", FontFactory.GetFont (GetFontByName ("Arial")));
            par.Add (chunk);
            chunk = new Chunk ("2", FontFactory.GetFont (GetFontByName ("Arial")));
            chunk.SetTextRise (5f);
            par.Add (chunk);
            chunk = new Chunk (" = c", FontFactory.GetFont (GetFontByName ("Arial")));
            par.Add (chunk);
            chunk = new Chunk ("2", FontFactory.GetFont (GetFontByName ("Arial")));
            chunk.SetTextRise (5f);
            par.Add (chunk);
            doc.Add (par);

            //Foreground and Background (text color and cell backgroudcolor) (table)
            //Warning
            table=new PdfPTable(3);

            chunk = new Chunk ("Green foreground", FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, new BaseColor (0, 200, 0)));
            cell = new PdfPCell (new Phrase (chunk));
            cell.BorderWidth = 0f;
            table.AddCell (cell);

            cell = new PdfPCell (new Phrase ("Red Background"));
            cell.BackgroundColor = new BaseColor (255, 0, 0);
            cell.BorderWidth = 0f;
            table.AddCell (cell);

            chunk = new Chunk ("Green foreground & Red background", FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, new BaseColor (0, 200, 0)));
            cell = new PdfPCell (new Phrase (chunk));
            cell.BackgroundColor = new BaseColor (255, 0, 0);
            cell.BorderWidth = 0f;
            table.AddCell (cell);

            table.TotalWidth = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin;
            table.LockedWidth = true;
            table.SpacingBefore = spacingAfter;

            doc.Add (table);

            //Left text
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            par.FirstLineIndent = firstLineIndent;
            for (int i = 0; i < 15; i++)
                par.Add ("Left text. ");
            par.Alignment = Element.ALIGN_LEFT;
            doc.Add (par);

            //Rigth text
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            par.FirstLineIndent = firstLineIndent;
            for (int i = 0; i < 15; i++)
                par.Add ("Rigth text. ");
            par.Alignment = Element.ALIGN_RIGHT;
            doc.Add (par);

            //Center text
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            for (int i = 0; i < 15; i++)
                par.Add ("Center text. ");
            par.Alignment = Element.ALIGN_CENTER;
            doc.Add (par);

            //Justify text
            par = new Paragraph();
            par.SpacingBefore = spacingAfter;
            par.FirstLineIndent = firstLineIndent;
            for (int i = 0; i < 15; i++)
                par.Add ("Justify text. ");
            par.Alignment = Element.ALIGN_JUSTIFIED_ALL;
            doc.Add (par);

            //Boxes
            //Warning
            PdfPTable box1 = new PdfPTable (1);
            PdfPTable box2 = new PdfPTable (1);
            PdfPCell addCellInBox2 = new PdfPCell (new Phrase ("Section(table) & !Margin & Padding & Border & Background"));

            addCellInBox2.Padding = 20f;
            addCellInBox2.BackgroundColor = new BaseColor (255, 255, 0);
            addCellInBox2.BorderWidth = 4f;
            addCellInBox2.BorderColor = new BaseColor (0, 0, 255);

            box2.AddCell (addCellInBox2);

            PdfPCell addCellinBox1 = new PdfPCell (box2);

            addCellinBox1.Padding = 20f;
            addCellinBox1.BackgroundColor = new BaseColor (0, 200, 0);
            addCellinBox1.BorderWidth = 4f;
            addCellinBox1.BorderColor = new BaseColor (255, 0, 0);

            box1.AddCell (addCellinBox1);

            box1.SpacingBefore = spacingAfter;
            box1.TotalWidth = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin;
            box1.LockedWidth = true;
            doc.Add (box1);

            doc.Close ();
            System.Diagnostics.Process.Start ("Document.pdf");
        }
Ejemplo n.º 4
0
        private void btn_createPDF_Click(object sender, EventArgs e)
        {
            Document doc = new Document(iTextSharp.text.PageSize.A4);
            string pdfFileName = "Test.pdf";
            PdfWriter wri = PdfWriter.GetInstance(doc,new FileStream(pdfFileName,FileMode.Create));
            doc.Open(); // Open Document to write

            // add image
            iTextSharp.text.Image JPEG = iTextSharp.text.Image.GetInstance("Basic_logo.jpg");

            // resize image
            //JPEG.ScalePercent(50f);
            JPEG.ScaleToFit(150f,150f);
            JPEG.Border = iTextSharp.text.Rectangle.BOX;
            JPEG.BorderColor = iTextSharp.text.BaseColor.YELLOW;
            JPEG.BorderWidth = 1f;
            // location of image
            JPEG.SetAbsolutePosition(doc.PageSize.Width -32f-212f,doc.PageSize.Height - 36f);
            doc.Add(JPEG);

            // write some content
            Paragraph paragraph = new Paragraph("This is my first line using Paragraph.\n Hello world!");
            // add the above created text using different class object to our PDF document
            doc.Add(paragraph);

            //--- 編號為羅馬編號
            RomanList romanlist = new RomanList(true,15);   // true:lowwer case, 50: 編號後的間距
            romanlist.IndentationLeft = 30f;
            romanlist.Add("One");
            romanlist.Add("two");
            romanlist.Add("Three");
            romanlist.Add("Four");
            romanlist.Add("Five");

            //doc.Add(romanlist);

            List list = new List(List.ORDERED,40f);
            list.SetListSymbol("\u2022");
            list.IndentationLeft = 40f;  // 向左移

            list.Add("One");
            list.Add("Two");
            list.Add("Three");
            list.Add("Roman List");
            list.Add(romanlist);        // list in list
            list.Add("Four");
            list.Add("Five");

            doc.Add(list);

            paragraph = new Paragraph("\n");

            doc.Add(paragraph);
            //PdfPTable table = new PdfPTable(3);
            ////--- table header
            //PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns", new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL,20f,iTextSharp.text.Font.NORMAL,iTextSharp.text.BaseColor.YELLOW)));
            //cell.BackgroundColor = new iTextSharp.text.BaseColor(0,150,0);  // RGB
            //cell.Colspan = 3;   // header size expand to 3 column
            //cell.HorizontalAlignment = 1; // 0 = Left, 1 = Centre, 2 = Right
            //table.AddCell(cell);
            ////---

            //table.AddCell("Col 1 Row 1");
            //table.AddCell("Col 2 Row 1");
            //table.AddCell("Col 3 Row 1");

            //table.AddCell("Col 1 Row 2");
            //table.AddCell("Col 2 Row 2");
            //table.AddCell("Col 3 Row 2");
            //doc.Add(table);

            PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);

            // add the headers from the DGV to the table
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                table.AddCell(new Phrase(dataGridView1.Columns[i].HeaderText));
            }

            // Flag the firt row as a header
            table.HeaderRows = 1;

            // add the actual rows from the DGV to the table
            for (int j = 0; j < dataGridView1.Rows.Count; j++)
            {
                for (int k = 0; k < dataGridView1.Columns.Count; k++)
                {
                    if (dataGridView1[k,j].Value != null)
                    {
                        table.AddCell(new Phrase(dataGridView1[k,j].Value.ToString()));
                    }
                }
            }

            doc.Add(table);

            // add chart to pdf
            var chartimage = new MemoryStream();
            chart1.SaveImage(chartimage, ChartImageFormat.Tiff);
            iTextSharp.text.Image Chart_image = iTextSharp.text.Image.GetInstance(chartimage.GetBuffer());
            Chart_image.ScalePercent(150f);
            doc.Add(Chart_image);

            doc.Close();

            //--- After finish create pdf, open the file's path
            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            try
            {
                #region 開路徑
                //if (!Directory.Exists(path))
                //{
                //    DirectoryInfo di = Directory.CreateDirectory(path);
                //}
                //System.Diagnostics.Process.Start(path);
                #endregion

                #region 開路徑後選擇執行exe
                //OpenFileDialog openFileDialog = new OpenFileDialog();
                //openFileDialog.InitialDirectory = path;
                //openFileDialog.Filter = "PDF (*.pdf)|*.pdf";
                //if (openFileDialog.ShowDialog() == DialogResult.OK)
                //{
                //    string FilePath = openFileDialog.FileName;
                //    Process.Start(FilePath);
                //}
                #endregion

                #region automatically open PDF teached by youtube
                System.Diagnostics.Process.Start(path+@"\"+pdfFileName);
                #endregion
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }

        }
Ejemplo n.º 5
0
        private void button4_Click(object sender, EventArgs e)
        {
            //Control ctl = this.dataGridView1;

            Control ctl = Control.FromHandle(this.dataGridView1.Handle);

            {
                Bitmap bt = new Bitmap(ctl.Width, ctl.Height);
                ctl.DrawToBitmap(bt, new System.Drawing.Rectangle(0, 0, bt.Width, bt.Height));
                bt.Save("abc.gif",System.Drawing.Imaging.ImageFormat.Gif);

                Document document = new Document(PageSize.A4, 10,10, 10,10);
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Application.StartupPath + @"\abcd.pdf", FileMode.Create));
                writer.ViewerPreferences = (PdfWriter.CenterWindow | PdfWriter.FitWindow | PdfWriter.PageModeUseNone);
                document.Open();

                //使用宋体字体
                BaseFont baseFont = BaseFont.CreateFont("C:\\WINDOWS\\FONTS\\simsun.ttc,0",
                                                        BaseFont.IDENTITY_H,
                                                        BaseFont.NOT_EMBEDDED);

                PdfContentByte cb = writer.DirectContent;

                Chapter chapter1 = new Chapter(new Paragraph("This is Chapter 1"), 1);
                Section section1 = chapter1.AddSection(20f, "Section 1.1", 2);
                Section section2 = chapter1.AddSection(20f, "Section 1.2", 2);
                Section subsection1 = section2.AddSection(20f, "Subsection 1.2.1", 3);
                Section subsection2 = section2.AddSection(20f, "Subsection 1.2.2", 3);
                Section subsubsection = subsection2.AddSection(20f, "Sub Subsection 1.2.2.1", 4);
                Chapter chapter2 = new Chapter(new Paragraph("This is Chapter 2"), 1);
                Section section3 = chapter2.AddSection("Section 2.1", 2);
                Section subsection3 = section3.AddSection("Subsection 2.1.1", 3);
                Section section4 = chapter2.AddSection("Section 2.2", 2);
                chapter1.BookmarkTitle = "Changed Title";
                chapter1.BookmarkOpen = true;
                chapter2.BookmarkOpen = false;
                document.Add(chapter1);
                document.Add(chapter2);

                ZapfDingbatsList zlist = new ZapfDingbatsList(49, 15);
                zlist.Add("One");
                zlist.Add("Two");
                zlist.Add("Three");
                zlist.Add("Four");
                zlist.Add("Five");
                document.Add(zlist);

                RomanList romanlist = new RomanList(true, 20);
                romanlist.IndentationLeft = 30f;
                romanlist.Add("One");
                romanlist.Add("Two");
                romanlist.Add("Three");
                romanlist.Add("Four");
                romanlist.Add("Five");
                document.Add(romanlist);

                PdfPTable table1 = new PdfPTable(3);
                PdfPCell cell1 = new PdfPCell(new Phrase("Header spanning 3 columns"));
                cell1.Colspan = 3;
                cell1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                table1.AddCell(cell1);
                table1.AddCell(cell1);
                table1.AddCell("Col 1 Row 2");
                table1.AddCell("Col 2 Row 2");
                table1.AddCell("Col 3 Row 2");

                table1.SetWidths(new int[]{50,100,100});

                table1.WidthPercentage = 100f;
                document.Add(table1);

                //using it = iTextSharp.text;

                //PdfPTable table2 = new PdfPTable(3);
                //table2.AddCell("Cell 1");
                //PdfPCell cell2 = new PdfPCell(new it.Phrase("Cell 2", new Font(Font.HELVETICA, 8f, Font.NORMAL, Color.YELLOW)));
                //cell2.BackgroundColor = new Color(0, 150, 0);
                //cell2.BorderColor = new Color(255, 242, 0);
                //cell2.Border = Rectangle.BOTTOM_BORDER | Rectangle.TOP_BORDER;
                //cell2.BorderWidthBottom = 3f;
                //cell2.BorderWidthTop = 3f;
                //cell2.PaddingBottom = 10f;
                //cell2.PaddingLeft = 20f;
                //cell2.PaddingTop = 4f;
                //table2.AddCell(cell2);
                //table2.AddCell("Cell 3");
                //document.Add(table2);

                System.Drawing.Image img = bt;
                MemoryStream mem = new MemoryStream();
                img.Save(mem, System.Drawing.Imaging.ImageFormat.Gif);
                byte[] bytes = mem.ToArray();

                iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(bytes);
                iTextSharp.text.Image img3 = iTextSharp.text.Image.GetInstance(bytes);
                img2.ScalePercent(100f);
                img3.ScalePercent(100f);
                img2.SetAbsolutePosition(50f, 400f);
                img3.SetAbsolutePosition(50f, 400f - img2.Height);
                //cb.AddImage(img2);
                //cb.AddImage(img3);

                cb.BeginText();

                float Xleading = 27.5f;
                float Xdelta = 10f;
                float Yleading = 27.5f;
                float Ydelta = 20f;

                cb.SetLineWidth(4f);
                cb.MoveTo(Xleading, (842 - Yleading - Ydelta));
                cb.LineTo((595f - Xleading), (842f - Yleading - Ydelta));
                cb.Stroke();

                cb.EndText();

                document.NewPage();

                //绘制近下方细直线上的文字
                cb.BeginText();

                BaseFont fbaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
                iTextSharp.text.Font font = new iTextSharp.text.Font(fbaseFont);

                cb.SetFontAndSize(fbaseFont,15);
                cb.SetColorFill(BaseColor.LIGHT_GRAY);

                PdfPCell cell = new PdfPCell();
                PdfPTable table = new PdfPTable(5);

                cell.HorizontalAlignment = Element.ALIGN_LEFT;

                table.AddCell(cell);
                document.Add(table);

                document.AddAuthor("ms");
                document.AddCreationDate();
                document.AddTitle("TEST");
                //document.Add(new Paragraph("", font));
                //document.Add(new Paragraph("    你好, PDF !", font));
                //document.Add(new Paragraph("    你好, PDF !", font));
                //document.Add(new Paragraph("    你好, PDF !", font));

                cb.ShowTextAligned(Element.ALIGN_LEFT, "一二三   :", 50, 800f, 0);
                cb.ShowTextAligned(Element.ALIGN_LEFT, "一二三   :", 50, 770f, 0);

                cb.EndText();

                cb.AddImage(img2);

                document.Close();
            }

            Control.ControlCollection ctls = this.Controls;

            foreach (Control item in ctls)
            {
                item.Enabled = false;
                //item.Width += 100;
            }
        }