1

I'm using iTextSharp to create PDFs in my application. However I have to recreate a table, where I have to set the size for a very small column. Below is the picture that shows the size I want to set the column: enter image description here

When the rest of the table creation all is well, I can't set this width.

Code:

PdfPTable table = new PdfPTable(2);
table.WidthPercentage = 82.0f;
PdfPCell cell = new PdfPCell(new Phrase("Com a assinatura autógrafa, o signatário desta Auto-declaração garante ter cumprido estas condições:", fontetexto));
cell.PaddingBottom = 10f;
cell.PaddingTop = 10f;
cell.Colspan = 2;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
table.AddCell("1. ");
table.AddCell("Os óleos e gorduras vegetais velhos fornecidos são biomassa conforme o Decreto de biomassa.");
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
CesarMiguel
  • 3,756
  • 4
  • 24
  • 34

4 Answers4

5

try with this code

PdfPTable table =  new PdfPTable(new float[] { 30f, 400f });
table.HorizontalAlignment = 0;
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(widths);
Ranjith Kumar Nagiri
  • 865
  • 3
  • 18
  • 42
2

You can do this with much less efforts if you use XmlParser rather then the normal HtmlParser

var document = new Document();
var workStream = new MemoryStream(); // or you can use fileStream also.
 PdfWriter writer = PdfWriter.GetInstance(document, workStream);
 XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, stream, Encoding.UTF8);

Check this nuget MvcRazorToPdf ,I found this better then RazorPDF

Nikitesh
  • 1,287
  • 1
  • 17
  • 38
2

The following code worked for me:

PdfPTable table = new PdfPTable(2);
table.TotalWidth = 500;
table.SetTotalWidth(new float[] { 30f, 400f });

PdfPCell c1 = new PdfPCell();
PdfPCell c2 = new PdfPCell();

c1.AddElement(new Phrase("first column"));
c1.AddElement(new Phrase("second column"));

table.Rows.Add(new PdfPRow(new PdfPCell[] { c1, c2 }));
Jeff B
  • 8,572
  • 17
  • 61
  • 140
Ravi
  • 95
  • 1
  • 1
  • 8
1

The answer from @IntellectWizard help me to reach a solution.

float[] widths = new float[] { 100f, 4000f }; // Code @IntellectWizard

gives a corrupted file, then i try:

PdfPTable table = new PdfPTable(new float[] { 30f, 400f });

This works!

CesarMiguel
  • 3,756
  • 4
  • 24
  • 34