Esempio n. 1
0
        public static void CreatePDF(DataTable dataTable, string destinationPath, int value)
        {
            if (!Directory.Exists(destinationPath))
            {
                Directory.CreateDirectory(destinationPath);
            }

            try
            {
                string path = destinationPath + $"\\{value}_test.pdf";

                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                #region -- ************* --

                Document document = new Document(PageSize.A4, 10, 10, 42, 35);
                using (var fileStream = new FileStream(path, FileMode.Create))
                {
                    string _imagePath = "https://ftp.mediaperf.com/img/logo.gif";

                    _imagePath = @"C:\Users\Sweet Family\Desktop\logo.jpg";

                    PdfWriter.GetInstance(document, fileStream);
                    document.Open();

                    #region --  --
                    document.AddAuthor("Micke Blomquist");
                    document.AddCreator("Sample application using iTextSharp");
                    document.AddKeywords("PDF tutorial education");
                    document.AddSubject("Document subject - Describing the steps creating a PDF document");
                    document.AddTitle("The document title - PDF creation using iTextSharp");
                    #endregion

                    //Document _document = new Document();
                    using (Document _document = new Document(PageSize.LETTER, 26, 16, 25, 10))
                    {
                        PdfWriter pdfWriter = PdfWriter.GetInstance(_document, fileStream);
                        _document.Open();

                        // -- Set the left header --
                        OnBeginPage(pdfWriter, _document);

                        // -- Set the rigth header --
                        OnRigthBeginPage(pdfWriter, _document);

                        ////InitalisePDFHelper(dataTable);
                        //SetImageLogo();

                        PdfPTable table = new PdfPTable(dataTable.Columns.Count)
                        {
                            WidthPercentage = 100
                        };

                        #region -- Set Logo --
                        Image imagePath = Image.GetInstance(_imagePath);
                        imagePath.ScalePercent(45f);
                        //imagePath.ScaleToFit(100f, 200f);
                        //imagePath.SetAbsolutePosition(250, 300);
                        _document.Add(imagePath);
                        #endregion

                        #region -- Set the left title --
                        var Title = new Paragraph("Relevé de redévences",
                                                  new Font(Font.FontFamily.TIMES_ROMAN,
                                                           18, Font.NORMAL,
                                                           BaseColor.BLACK)
                                                  );
                        Title.Alignment = Element.ALIGN_RIGHT;
                        _document.Add(Title);
                        #endregion

                        //_document.Add(new Paragraph("\n\n\r"));

                        Chunk verticalPositionMark = new Chunk(new VerticalPositionMark());

                        BaseFont baseFont = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);

                        #region ---  ---

                        // -----------------------------

                        // -------------------------------------------

                        _document.Add(new Paragraph("\n\r"));
                        _document.Add(new Paragraph("\n\r"));

                        #region -- Set first ligne --
                        Font     font     = new Font(baseFont, 11, Font.BOLD, BaseColor.BLACK);
                        PdfPCell pdfPCell = new PdfPCell(new Phrase("N° RdR      $NoRdR", font))
                        {
                            VerticalAlignment   = Element.ALIGN_MIDDLE,
                            HorizontalAlignment = Element.ALIGN_CENTER,
                            BorderWidth         = 1
                        };

                        PdfPTable      table0 = new PdfPTable(2);
                        PdfContentByte cb     = pdfWriter.DirectContent;
                        table0            = new PdfPTable(1);
                        table0.TotalWidth = 160;
                        table0.AddCell(pdfPCell);
                        table0.WriteSelectedRows(0, -1, 35, 650, cb);

                        pdfPCell = new PdfPCell(new Phrase("Date       $Date", font))
                        {
                            VerticalAlignment   = Element.ALIGN_MIDDLE,
                            HorizontalAlignment = Element.ALIGN_CENTER,
                            BorderWidth         = 1
                        };

                        PdfPTable table10 = new PdfPTable(2);
                        table10            = new PdfPTable(1);
                        table10.TotalWidth = 160;
                        table10.AddCell(pdfPCell);
                        table10.WriteSelectedRows(0, -1, 220, 650, cb);

                        pdfPCell = new PdfPCell(new Phrase("Destinataire", font))
                        {
                            VerticalAlignment   = Element.ALIGN_MIDDLE,
                            HorizontalAlignment = Element.ALIGN_CENTER,
                            BorderWidth         = 1
                        };

                        PdfPTable table30 = new PdfPTable(2);
                        table30 = new PdfPTable(1);
                        table30.HorizontalAlignment = Element.ALIGN_CENTER;
                        table30.TotalWidth          = 190;
                        //table30. = 65;
                        table10.AddCell(pdfPCell);
                        var p = new Paragraph
                        {
                            new Phrase("Destinataire", new Font(Font.FontFamily.TIMES_ROMAN, 20, Font.NORMAL, BaseColor.BLACK)),
                            "$Destinataire \n\n",
                            "$Prestataire \n\n",
                            "$Adresse$Prestataire \n\n\n"
                        };
                        pdfPCell.Border = 2;
                        table30.AddCell(p);
                        table30.WriteSelectedRows(0, -1, 400, 650, cb);

                        #endregion

                        _document.Add(new Paragraph("\n\r"));
                        // ----------------------------------------------

                        //var image = Image.GetInstance(_imagePath);
                        //var paragraph = new Paragraph();
                        //paragraph.Add(new Chunk(image, 0, 0));
                        //var tables = new PdfPTable(1);
                        //var cell = new PdfPCell { PaddingLeft = 5, PaddingTop = 5, PaddingBottom = 5, PaddingRight = 5 };
                        //cell.HorizontalAlignment = Element.ALIGN_CENTER;
                        //cell.AddElement(paragraph);
                        //tables.AddCell(cell);

                        //_document.Add(new Paragraph("\n\r"));
                        //_document.Add(tables);

                        #endregion

                        #region --- Set images side by side !!!!!!!!! ---
                        //PdfPTable resimtable = new PdfPTable(2); // two colmns create tabble
                        //resimtable.WidthPercentage = 100f;//table %100 width
                        //Image imgsag = Image.GetInstance(_imagePath);
                        //resimtable.AddCell(imgsag);//Table One colmns added first image
                        //imgsag.ScalePercent(60f);

                        //Image imgsol = Image.GetInstance(_imagePath);
                        //imgsol.ScalePercent(60f);
                        //resimtable.AddCell(imgsag);//Table One colmns added first image
                        //resimtable.AddCell(imgsol);//Table two colmns added second image
                        //_document.Add(resimtable);
                        #endregion

                        #region -- Set Text Side by side --

                        //var docTitle = new Paragraph();
                        //var titleFont = FontFactory.GetFont(FontFactory.TIMES_BOLD, 10f, BaseColor.BLACK);
                        //docTitle.Font = titleFont;

                        //docTitle.Add("Simulation n° : " + 013235 + "\n");
                        //docTitle.Add(new Chunk("TS : " + "AZ5462132032003." + "\n", titleFont));
                        //docTitle.Add(new Chunk("Société : " + "CUST NAME" + "\n", titleFont));
                        //docTitle.Add(new Chunk("Date de simulation :  " + DateTime.Now.ToString() + "\n", titleFont));
                        //docTitle.Add(new Chunk("Montant total dû : " + 5265.20 + " €\n\n", titleFont));
                        ////docTitle.Font.Size = 6f;

                        //// -- Set Interline --
                        ////docTitle.SetLeading(1.8f, 1.2f);
                        //_document.Add(docTitle);

                        // -- Ajout de saut ligne --
                        _document.Add(new Paragraph("\n\n"));

                        //Chunk verticalPositionMark = new Chunk(new VerticalPositionMark());
                        Paragraph footerParagraph = new Paragraph("Text to the left");
                        var       titleFont       = FontFactory.GetFont(FontFactory.TIMES_BOLD, 10f, BaseColor.BLACK);
                        footerParagraph.Font = titleFont;
                        footerParagraph.Add(new Chunk(verticalPositionMark));
                        footerParagraph.Add("Suppose that some day, somebody asks you");

                        _document.Add(footerParagraph);

                        //// -- Set Interligne --
                        //footerParagraph.SetLeading(2.8f, 1.2f);
                        #endregion

                        _document.Add(new Paragraph("\n\r"));
                        VerticalPositionMark seperator = new LineSeparator();
                        seperator.Offset = -6f;

                        // -- Ajout de saut ligne --
                        _document.Add(new Paragraph("\n"));

                        #region --  Fill PDF DataGrid  --
                        ////Set columns names in the pdf file
                        for (int k = 0; k < dataTable.Columns.Count; k++)
                        {
                            PdfPCell pdfColumnCell = new PdfPCell(new Phrase(dataTable.Columns[k].ColumnName));

                            pdfColumnCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
                            pdfColumnCell.VerticalAlignment   = PdfPCell.ALIGN_CENTER;
                            pdfColumnCell.BackgroundColor     = new BaseColor(51, 102, 102);

                            table.AddCell(pdfColumnCell);
                        }

                        ////Add values of DataTable in pdf file
                        for (int i = 0; i < dataTable.Rows.Count; i++)
                        {
                            for (int j = 0; j < dataTable.Columns.Count; j++)
                            {
                                PdfPCell pdfColumnCell = new PdfPCell(new Phrase(dataTable.Rows[i][j].ToString()));

                                //Align the pdfColumnCell in the center
                                pdfColumnCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
                                pdfColumnCell.VerticalAlignment   = PdfPCell.ALIGN_CENTER;

                                table.AddCell(pdfColumnCell);
                            }
                        }
                        #endregion

                        // -- Ajout de saut ligne --
                        _document.Add(new Paragraph("\n"));

                        System.Drawing.Font fonts = new System.Drawing.Font("Courier", 16);


                        //// -- Set Interligne --
                        //table.SetLeading(9.8f, 1.2f);

                        table.GetFooter();

                        //PdfPCell cell = new PdfPCell(new Phrase("Liste des machines déclarées par le client", new Font(Font.NORMAL, 9f, Font.NORMAL, BaseColor.WHITE)));
                        //cell.BackgroundColor = new BaseColor(223, 0, 27);    // -- RGB couleur --
                        //table.AddCell(cell);

                        var docFooter  = new Paragraph();
                        var footerFont = FontFactory.GetFont(FontFactory.TIMES_BOLD, 10f, BaseColor.BLACK);
                        //docFooter.Font = titleFont;

                        //////// -- A supprimer -- **************************
                        //docFooter.Add("Simulation n° : \n");
                        //docFooter.Add(new Chunk("Montant total dû : "));
                        //docFooter.Alignment = Element.ALIGN_RIGHT;
                        //_document.Add(docFooter);

                        OnEndPage(pdfWriter, _document);

                        _document.Add(table);
                        _document.Close();
                        pdfWriter.Close();
                        fileStream.Close();

                        #region Display PDF
                        //Process.Start(path);
                        #endregion
                    }
                }
                #endregion
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                throw;
            }
        }