PdfPTable cell width

懵懂的女人 提交于 2019-12-04 20:32:02

try with this code

PdfPTable table =  new PdfPTable(new float[] { 30f, 400f });
table.HorizontalAlignment = 0;
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(widths);

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

Ravi

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 }));

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!