コード例 #1
0
        public static PdfPTable PDFTable1(Client client)
        {
            List <Simulation> listSim = new List <Simulation>();

            var table1 = new PdfPTable(9);     // Create table for 9 columns

            table1.DefaultCell.Border = Rectangle.NO_BORDER;

            var      Calibri1 = FontFactory.GetFont("TimesNewRoman", 14, Font.BOLD);
            PdfPCell cell     = new PdfPCell(new Phrase("Simulation list of " + client.FullName, Calibri1));

            cell.Colspan             = 9;
            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            cell.Border = Rectangle.NO_BORDER;
            table1.AddCell(cell);
            table1.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
            var Calibri9 = FontFactory.GetFont("TimesNewRoman", 12, Font.BOLD);

            int[] a = { 1, 3, 3, 3, 3, 3, 3, 3, 3 };
            table1.NormalizeHeadersFooters();// Set columns' width
            table1.SetWidths(a);
            table1.AddCell(" ");
            table1.AddCell(new Paragraph("Simulation Id", Calibri9));
            table1.AddCell(new Paragraph("Product name", Calibri9));
            table1.AddCell(new Paragraph("Price", Calibri9));
            table1.AddCell(new Paragraph("Start date", Calibri9));
            table1.AddCell(new Paragraph("End date", Calibri9));
            table1.AddCell(new Paragraph("Interest rate", Calibri9));
            table1.AddCell(new Paragraph("Settlement price", Calibri9));
            table1.AddCell(new Paragraph("Tatal months", Calibri9));


            listSim = client.getSimulationList();

            if (listSim.Count != 0)
            {
                int i          = 0;
                var FontColour = new BaseColor(35, 31, 32);
                var Calibri8   = FontFactory.GetFont("TimesNewRoman", 11, FontColour);
                foreach (Simulation Sim in listSim)
                {
                    if (i % 2 == 0)
                    {
                        table1.DefaultCell.BackgroundColor = BaseColor.WHITE;
                    }
                    else
                    {
                        table1.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
                    }
                    table1.AddCell(new Paragraph((i + 1).ToString(), Calibri8)); // Add cell
                    table1.AddCell(new Paragraph(Sim.Id.ToString(), Calibri8));
                    table1.AddCell(new Paragraph(Sim.Product.Name.ToString(), Calibri8));
                    table1.AddCell(new Paragraph(Sim.Price.ToString(), Calibri8));
                    table1.AddCell(new Paragraph(Sim.StartDate.Day.ToString() + "-" + Sim.StartDate.Month + "-" + Sim.StartDate.Year.ToString(), Calibri8));
                    table1.AddCell(new Paragraph(Sim.EndDate.Day.ToString() + "-" + Sim.EndDate.Month.ToString() + "-" + Sim.EndDate.Year.ToString(), Calibri8));
                    table1.AddCell(new Paragraph(Sim.InterestRate.ToString(), Calibri8));
                    table1.AddCell(new Paragraph(Sim.SettlementPrice.ToString(), Calibri8));
                    table1.AddCell(new Paragraph(((int)((Sim.EndDate - Sim.StartDate).TotalDays) / 30).ToString(), Calibri8));
                    i++;
                }
            }
            return(table1);
        }
コード例 #2
0
        private void createPdfButton(object sender, EventArgs e)
        {
            string test = string.Format(textBox1.Text);

            if (test.Equals(""))
            {
                MessageBox.Show("Please enter a file name.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (test.Contains("\\") || test.Equals("/"))
            {
                MessageBox.Show("Invalid character \\", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (!test.Equals(""))
            {
                string name = string.Format(textBox2.Text);


                //ss = string.Format(name.FullName);
                if (File.Exists(name + "\\" + textBox1.Text + ".pdf"))
                {
                    MessageBox.Show("File name must be unique.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    try
                    {
                        string   path     = string.Format(textBox2.Text + "\\");
                        string   fileName = path + test + ".pdf";
                        Document document = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 30, 10);

                        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
                        document.Open();
                        String st = Application.StartupPath;
                        st += "\\jj.jpg";
                        iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(st);
                        png.ScaleAbsolute(70, 70);
                        png.SetAbsolutePosition(document.PageSize.Width - 10f - 340f, document.PageSize.Height - 20f - 50f);
                        document.Add(png);
                        Paragraph paragraph = new Paragraph("\n\n             Campuse Library \n   Islamic University,Kushtia-7003\n E-mail:[email protected]    \n Mobile:01913-540251,01740197701\n  " + "             Date:" + DateTime.Now.ToShortDateString());
                        paragraph.IndentationLeft = 200f;
                        document.Add(paragraph);
                        Paragraph p = new Paragraph("" + '\n');
                        document.Add(p);

                        PdfPTable table = new PdfPTable(dataGridView1.ColumnCount - 1);
                        PdfPCell  cell  = new PdfPCell(new Phrase("Total Book Order", new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL, 16f, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.ORANGE)));
                        cell.BackgroundColor = new iTextSharp.text.BaseColor(200, 20, 120);
                        iTextSharp.text.Font fntTableFont = FontFactory.GetFont("Times New Roman", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
                        table.WidthPercentage     = 100;
                        table.HorizontalAlignment = 0;
                        table.SpacingAfter        = 10;

                        cell.Colspan             = dataGridView1.ColumnCount - 1;
                        cell.HorizontalAlignment = 1;
                        table.AddCell(cell);

                        for (int i = 0; i < dataGridView1.ColumnCount - 1; i++)
                        {
                            table.AddCell(new Phrase(dataGridView1.Columns[i].HeaderText));
                        }
                        table.HeaderRows = 1;
                        table.NormalizeHeadersFooters();
                        float[] widths = new float[] { 1f, 4f, 4.5f, 1.25f, 1f, 1.25f, 1.50f };
                        table.SetWidths(widths);


                        for (int i = 0; i < dataGridView1.RowCount; i++)
                        {
                            for (int j = 0; j < dataGridView1.ColumnCount - 1; j++)
                            {
                                if (dataGridView1[j, i].Value != null)
                                {
                                    PdfPCell CellOne = new PdfPCell(new Phrase(dataGridView1[j, i].Value.ToString(), fntTableFont));

                                    table.AddCell(CellOne);
                                }
                            }
                        }
                        document.Add(table);
                        int sum = 0;

                        for (int i = 0; i < dataGridView1.Rows.Count; ++i)
                        {
                            sum += Convert.ToInt32(dataGridView1.Rows[i].Cells["Quantity"].Value);
                        }
                        Paragraph paragraph1 = new Paragraph("Total = " + sum + "  pices", new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL, 12f, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK));
                        paragraph1.IndentationLeft = 450f;
                        document.Add(paragraph1);
                        MessageBox.Show("Successfully Created pdf file.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        document.Close();
                        System.Diagnostics.Process.Start(path + test + ".pdf");
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }