Increase the width of two columns in PDF in c#

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Increase the width of two columns in PDF in c#



I am downloading a PDF using Itextsharp .I need to increase the width of two columns in pdf.I have tried using celcols.width ,however its not working.Now all the columns have same width.Any help will be really appreciated.Thanks in Advance`.


protected void Button6_Click(object sender, EventArgs e)

try


string fromdate = "", todate = "";
string compgrp = "All";


var pdfDoc = new Document(PageSize.A3, 10f, 10f, 10f, 0f);
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
PdfPageEventHelper pageEventHelper = new PdfPageEventHelper();
writer.PageEvent = pageEventHelper;
HeaderFooter header = new HeaderFooter(new Phrase("Schedule report" + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt") + ""), false);
header.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#62e456"));
// Remove the border that is set by default
header.Border = iTextSharp.text.Rectangle.TITLE;
// Align the text: 0 is left, 1 center and 2 right.
header.Alignment = Element.ALIGN_CENTER;
pdfDoc.Header = header;
// Header.
pdfDoc.Open();
string Connectionstring = ConfigurationManager.ConnectionStrings["SPCFConnectionString"].ConnectionString;
SqlConnection cn = new SqlConnection(Connectionstring);
cn.Open();
try

if (ddl_clntsearch.SelectedValue.ToString() == "")

compgrp = "All";

else

compgrp = ddl_clntsearch.SelectedValue.ToString();

string querylist = new string

"exec [SP0480_05] '" + compgrp +"','" + txt_mobnum.Text +"','"+txt_searchdate.Text+"','" + ddl_status.SelectedValue.ToString() +"'",

;

string Headers = new string

"Date"," Name","Shift time","User","Remarks","Amount","Signature"
;

foreach (string query in querylist)

iTextSharp.text.Table pdfTableheader = new iTextSharp.text.Table(7, 1);
pdfTableheader.BorderWidth = 1; pdfTableheader.Width = 100; pdfTableheader.Spacing = 1;
//pdfTableheader.Padding = 1;
foreach (string items in Headers)

Cell cellCols = new Cell();
Chunk chunkCols = new Chunk();
cellCols.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#62e05e"));
iTextSharp.text.Font ColFont = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK);
//if (items == "Name")
//
// cellCols.Width = 300f;
//
//else
//
// cellCols.Width = 100f;
//
chunkCols = new Chunk(items, ColFont);

cellCols.Add(chunkCols);
pdfTableheader.AddCell(cellCols);

pdfDoc.Add(pdfTableheader);
SqlCommand cmd = new SqlCommand(query, cn);
cmd.CommandTimeout = 30;
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dr);
object array = new object[dataTable.Columns.Count];

//dataTable.Rows.Add(array);
int cols = dataTable.Columns.Count;
int rows = dataTable.Rows.Count;
iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);
pdfTable.BorderWidth = 1; pdfTable.Width = 100; pdfTable.Spacing = 2;
//pdfTable.Padding = 1;

//creating table data (actual result)

for (int k = 0; k <= rows - 1; k++)

for (int j = 0; j < cols; j++)



Cell cellRows = new Cell();
//if (k % 2 == 0)
cellRows.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#ffffff"));

iTextSharp.text.Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA,12);
Chunk chunkRows = new Chunk(dataTable.Rows[k][j].ToString(), RowFont);
cellRows.Add(chunkRows);

pdfTable.AddCell(cellRows);



pdfDoc.Add(pdfTable);


catch (Exception exc)




//creating table headers
cn.Close();
pdfDoc.Close();

Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename=REF-0.pdf", "Report"));
Response.BinaryWrite(mStream.ToArray());



catch (Exception ex)






Here I need to increase the width of 2nd and 3rd column ie Name and User




1 Answer
1



You should set the width of the columns. And you can do that by applying them to the table object. For example:


float widths = new float 1f, 2f ; //relative col widths in proportions - 1/3 and 2/3
table.SetWidths(widths);



You can visit this link to refer more: https://www.mikesdotnetting.com/article/86/itextsharp-introducing-tables
Hope to help, my friend!





That is correct, hence the up-vote. Unfortunately, Mike Dotnetting is outdated, because it still talks about iText 5. Starting with iText 7, you don't need to worry about setting the widths of the columns. In iText 7, the columns adapt their width based on their content.
– Bruno Lowagie
Aug 8 at 18:24





@BrunoLowagie: Thank you for your suggestions, my friend :))
– Tomato32
Aug 9 at 1:46






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard