Reputation: 19199
I am using following code to generate PDF doc using iTextSharp library version 5.4.2.
// Create a Document object
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWriter object, specifying the output stream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
// Open the Document for writing
document.Open();
//Gets System.Web.UI.WebControls.Table
//Returns HTML table with 4 coulms
var flowsheetTable = GetTable(report);
var stringWriter = new StringWriter();
using (var htmlWriter = new HtmlTextWriter(stringWriter))
{
flowsheetTable.RenderControl(htmlWriter);
}
List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(stringWriter.ToString()), null);
for (int k = 0; k < htmlarraylist.Count; k++)
{
document.Add((IElement)htmlarraylist[k]);
}
document.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=Receipt-test.pdf");
Response.BinaryWrite(output.ToArray());
The method GetTable() returns a System.Web.UI.WebControls.Table which is having 4 columns. Is there any way to fix the width of first column in the table. I want first column to take 40% of total width.
Thank you
Upvotes: 0
Views: 1194
Reputation: 55457
While you're looping through the collection of IElement
objects you can individual cast, inspect and modify each one. In your case you might want to do something like this:
//Sample table with four columns
var sampleTable = "<table><tr><td>A</td><td>B</td><td>C</td><td>D</td></tr></table>";
//Parse sample HTML to collection if IElement objects
List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(sampleTable), null);
//Declare variables for use below
IElement ele;
PdfPTable t;
//Loop through the collection
for (int k = 0; k < htmlarraylist.Count; k++) {
//Get the individual item (no cast should be needed)
ele = htmlarraylist[k];
//If the item is a PdfPTable
if (ele is PdfPTable) {
//Get and cast it
t = ele as PdfPTable;
//Set the widths (40%/20%/20%/20%)
t.SetWidths(new float[] { 4, 2, 2, 2 });
}
//Regardless of what was done above, add the object to our document
document.Add(ele);
}
Upvotes: 1