Question

Im attempting to view a table in a PDF file using iTextSharp. I've figured out how to add the table, and make the PDF file open off a button click, but the table font is too big, and words are wrapped making them hard to read. I've entered this code, but nothing happens to the tables font.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Lewis_Warby_Airbrushing
{
    public partial class selectedorderForm : Form
    {
        public selectedorderForm(string strValue)
        {
            InitializeComponent();
            textBox1.Text = strValue;

        }
        DataTable dt;
        private void selectedorderForm_Load(object sender, EventArgs e)
        {
            SqlCeConnection con = new SqlCeConnection(@"Data Source=|DataDirectory|\LWADataBase.sdf;");
            SqlCeDataAdapter sda = new SqlCeDataAdapter("select * from orderTBL ", con);

            dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            DataView dv = new DataView(dt);
            dv.RowFilter = "[Order Number] like '%" + textBox1.Text.Trim() + "%'";
            dataGridView1.DataSource = dv;
        }

        private void pdfBTN_Click(object sender, EventArgs e)
        {

            iTextSharp.text.Font fontTitle = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.BOLD, BaseColor.BLACK);


            Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 10, 10);


            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("PurchaseOrder-"+textBox1.Text+".pdf", FileMode.Create));
            doc.Open();


            Paragraph title = new Paragraph("PURCHASE ORDER " + textBox1.Text +"\n Lewis Warby Airbrushing\n", fontTitle);
            title.Alignment = Element.ALIGN_CENTER;
            doc.Add(title);

            title.SpacingAfter = 15f;







            iTextSharp.text.Font fontTable = FontFactory.GetFont("Arial", 5, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

            PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);
            table.SpacingBefore = 45f;
            table.TotalWidth = 216f;
            table.DefaultCell.Phrase = new Phrase() { Font = fontTable };

            for (int j = 0; j < dataGridView1.Columns.Count; j++)
            {
                table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText));

            }

            table.HeaderRows = 1;

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                for (int k = 0; k < dataGridView1.Columns.Count; k++)
                {
                    if (dataGridView1[k, i].Value != null)
                    {
                        table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString()));
                    }
                }
            }



            doc.Add(table);




            doc.Close();

            System.Diagnostics.Process.Start("PurchaseOrder-"+textBox1.Text+".pdf");


        }
    }
}
Was it helpful?

Solution

Try adding font to Phrase constructors:

for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
    table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText,fontTable));

}

//..

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    for (int k = 0; k < dataGridView1.Columns.Count; k++)
    {
         if (dataGridView1[k, i].Value != null)
         {
              table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString(),fontTable));
         }
     }
}

Check this link for more info about Chunkc, Phrases etc. http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top