PdfPTable单元格宽度

我正在使用iTextSharp在我的应用程序中创建PDF。 但是我必须重新创建一个表,我必须为一个非常小的列设置大小。 下面是显示我想要设置列的大小的图片: 在此处输入图像描述

当表创建的其余部分都很好时, 我无法设置此宽度

码:

 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."); 

尝试使用此代码

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

如果你使用XmlParser而不是普通的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); 

检查这个nuget MvcRazorToPdf ,我发现这比RazorPDF更好

以下代码对我有用:

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

@IntellectWizard的答案帮助我找到了解决方案。

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

给出一个损坏的文件,然后我尝试:

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

这有效!