【问题标题】:PdfPTable cell widthPdfP表格单元格宽度
【发布时间】:2014-02-28 11:41:17
【问题描述】:

我正在使用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.");

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 itextsharp


    【解决方案1】:

    试试这段代码

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

    【讨论】:

    • the file was corrupted。我认为错误是float[] widths
    • 好的,像这样工作:PdfPTable table = new PdfPTable(new float[] { 30f, 400f });
    【解决方案2】:

    如果你使用 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 更好

    【讨论】:

      【解决方案3】:

      以下代码对我有用:

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

      【讨论】:

        【解决方案4】:

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

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

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

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

        这行得通!

        【讨论】: