【问题标题】:Create PDF in asp.net using c# with row span & colspan使用带有rowspan和colspan的c#在asp.net中创建PDF
【发布时间】:2014-11-29 06:25:43
【问题描述】:

我在 c# asp.net 中使用 iTextSharp 创建 PDF 文档以显示文本框值。 pdf创建成功。但问题是,我以表格格式显示值。我想做行跨度和列跨度。请任何人帮助我..

         PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
         pdfDoc.Open();
         //Set Font Properties for PDF File
         Font fnt = FontFactory.GetFont("Times New Roman", 14);
         PdfPTable PdfTable = new PdfPTable(2);
         PdfPCell Cell = new PdfPCell();
         PdfTable.TotalWidth = 600f;
         float[] widths = new float[] { 4f, 8f };
         PdfTable.SetWidths(widths);

         PdfPCell cell=new PdfPCell(new Phrase("MOM TABLE HEADER"));
         cell.Rowspan=2;   // this not working
         PdfTable.AddCell("Meeting By");
         PdfTable.AddCell(txtheld.Text);

【问题讨论】:

  • 试试 cell.SetRowspan(2);
  • 它给出了错误 Sandeep

标签: c# asp.net pdf


【解决方案1】:

我认为您没有在表格中添加行跨单元格,这就是您遇到问题的原因。我已经尝试了以下代码及其按预期工作:

 protected void btnExport_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream("C://test1.pdf", FileMode.Create, FileAccess.Write, FileShare.None);

        try
        {


            Document pdfDoc = new Document();
            PdfWriter.GetInstance(pdfDoc, fs);
            pdfDoc.Open();
            //Set Font Properties for PDF File
            Font fnt = FontFactory.GetFont("Times New Roman", 14);
            PdfPTable PdfTable = new PdfPTable(2);
            PdfPCell Cell = new PdfPCell();
            PdfTable.TotalWidth = 600f;
            float[] widths = new float[] { 4f, 8f };
            PdfTable.SetWidths(widths);


            PdfPCell cell = new PdfPCell(new Phrase("MOM TABLE HEADER"));
            cell.Rowspan = 2;   // this not working
            PdfTable.AddCell(cell);
            PdfTable.AddCell("Meeting By");
            PdfTable.AddCell("test1");

            pdfDoc.Add(PdfTable);

            pdfDoc.Close();
        }
        catch
        {
        }
        finally
        {
            fs.Close();
        }
    }

【讨论】:

    【解决方案2】:

    如果您需要创建包含 rowspan 和 colspan 的表格,您可以使用我们的 PDFFlow 库,我们有许多功能可以让您自定义表格。也许这对你来说会容易得多。这就是它的工作原理:

        DocumentBuilder.New()
        .AddSection()
            .AddParagraph("Table with colspan and rowspan:")
                .SetBold().SetMarginBottom(5)
        .ToSection()
            .AddTable()
                .SetWidth(300)
                .AddColumnToTable().AddColumnToTable()
                .AddColumnToTable().AddColumnToTable()
                .AddRow()
                    .SetBackColor(Color.FromRgba(0, 0.69, 0.94, 1)).SetBold()
                    .AddCell("Product")
                        .SetRowSpan(2)
                        .SetVerticalAlignment(VerticalAlignment.Center)
                .ToRow()
                    .AddCell("Month")
                        .SetColSpan(3)
                        .SetHorizontalAlignment(HorizontalAlignment.Center)
            .ToTable()
                .AddRow()
                        .SetBackColor(Color.FromRgba(0, 0.69, 0.94, 1)).SetBold()
    .SetHorizontalAlignment(HorizontalAlignment.Center)
                        .AddCellToRow().AddCellToRow("January")
                        .AddCellToRow("February").AddCellToRow("March")
                .ToTable()
                    .AddRow()
                        .AddCell()
                            .SetHorizontalAlignment(HorizontalAlignment.Left)
                            .AddParagraphToCell("Product 1")
                    .ToRow()
                        .SetHorizontalAlignment(HorizontalAlignment.Center)
                        .AddCellToRow("100").AddCellToRow("115").AddCellToRow("103")
                .ToTable()
                    .AddRow()
                        .AddCell()
                            .SetHorizontalAlignment(HorizontalAlignment.Left)
                            .AddParagraphToCell("Product 2")
                    .ToRow()
                        .SetHorizontalAlignment(HorizontalAlignment.Center)
                        .AddCellToRow("200").AddCellToRow("204").AddCellToRow("207")
                .ToTable()
                    .AddRow()
                        .SetBackColor(Color.FromRgba(0, 0.69, 0.94, 1)).SetBold()
                        .AddCellToRow("Total")
                        .AddCell("929")
                            .SetColSpan(3)
                            .SetHorizontalAlignment(HorizontalAlignment.Right)
        .ToDocument().Build("Table.pdf");
    

    以上代码sn -p 生成Table.pdf 文档,其中包含以下table

    如您所见,您可以设置单元格跨越的列数或行数。您还可以单独自定义每一行和每一列,例如,通过定义单元格的字体和文本对齐方式或设置所需的背景颜色。库中的方法名称简单明了,因此很容易找到您要实现的功能。

    要查看更多示例,请浏览我们的真实文档samples。您可以按照随附文章中描述的步骤复制这些示例或创建类似的文档。我们的库对非商业和开源项目是免费的。

    【讨论】:

      【解决方案3】:

      对于在 asp.net 中使用 c# 和行跨度和列跨度创建 PDF

      1) 请安装安装包 iTextSharp-LGPL nuget

      2)对于行跨度

      private static void addCellWithRowSpan(PdfPTable table, string text, int rowspan, bool colorStatus, bool fontStatus)
      {
          BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
          iTextSharp.text.Font times;
          if (fontStatus == false)
          {
              times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL, Color.BLACK);
          }
          else
          {
              times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.BOLD, Color.BLACK);
          }
          PdfPCell cell = new PdfPCell(new Phrase(text, times));
          cell.Rowspan = rowspan;
      
          cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
          cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
      
          if (colorStatus == true)
          {
              cell.BackgroundColor = new Color(179, 179, 179);
          }
          table.AddCell(cell);
      }
      

      3)对于科尔斯潘

      private static void addCellWithColSpan(PdfPTable table, string text, int colspan, bool colorStatus, bool alignmentStatus, bool fontStatus)
      {
          BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
          iTextSharp.text.Font times;
          if (fontStatus == false)
          {
              times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL, Color.BLACK);
          }
          else
          {
              times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.BOLD, Color.BLACK);
          }
          PdfPCell cell = new PdfPCell(new Phrase(text, times));
          cell.Colspan = colspan;
          if (alignmentStatus == false)
          {
              cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
              cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
          }
          else
          {
              cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
              cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
          }
          if (colorStatus == true)
          {
              cell.BackgroundColor = new Color(179, 179, 179);
          }
          table.AddCell(cell);
      }
      

      4)下面的代码将让您了解如何使用 addCellWithRowSpan 和 addCellWithColSpan

      public void GenerateInvoice()
      {
          var doc = new Document(PageSize.A4);
      
          PdfWriter.GetInstance(doc, new FileStream(@"D:\" + "/Doc19.pdf", FileMode.Create));
          doc.Open();
      
          PdfPTable headerTable = new PdfPTable(new float[] { 30f, 40f });
          // add a image
          BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
          Font fontH1 = new Font(bfTimes, 9, Font.BOLD);
          PdfPCell imageCell;
          iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("ImagePath");
          logo.ScaleToFit(90f, 90f);
          imageCell = new PdfPCell(logo);
          imageCell.Colspan = 1; // either 1 if you need to insert one cell
          imageCell.Border = 0;
      
          // add a image
          PdfPCell rowCell;
          float[] headerWidths = new float[] { 250f, 250f };
          headerTable.AddCell(imageCell);
      
          rowCell = new PdfPCell(new Phrase("Invoice No : INV10001\nInvoices Type : ", fontH1));
          rowCell.Border = 0;
          rowCell.PaddingTop = 30f;
          headerTable.AddCell(rowCell);
          headerTable.HorizontalAlignment = 0;
          headerTable.TotalWidth = 700f;
          headerTable.LockedWidth = true;
          headerTable.SetWidths(headerWidths);
          headerTable.DefaultCell.Border = Rectangle.NO_BORDER;
          doc.Add(headerTable);
      
          Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, new Color(65, 105, 225), Element.ALIGN_LEFT, 1)));
          doc.Add(p);
      
          Paragraph p1 = new Paragraph("\n");
          doc.Add(p1);
      
      
          PdfPTable ContentTable = new PdfPTable(7);
          ContentTable.HorizontalAlignment = 0;
          ContentTable.TotalWidth = 523f;
          ContentTable.LockedWidth = true;
          float[] widths = new float[] { 20f, 60f, 60f, 60f, 60f, 60f, 60f };
          ContentTable.SetWidths(widths);
      
          addCellWithRowSpan(ContentTable, "#", 1, true, true);
      
          addCellWithRowSpan(ContentTable, "Request NO", 1, true, true);
          addCellWithRowSpan(ContentTable, "Shipment Date", 1, true, true);
          addCellWithRowSpan(ContentTable, "VIN", 1, true, true);
          addCellWithColSpan(ContentTable, "Vehicle Name", 2, true, false, true);
          addCellWithRowSpan(ContentTable, "Amount", 1, true, true);
      
          addCellWithRowSpan(ContentTable, "1", 3, false, false);
          addCellWithRowSpan(ContentTable, "SHC!10002", 1, false, false);
          addCellWithRowSpan(ContentTable, "1-1-1", 1, false, false);
          addCellWithRowSpan(ContentTable, "VIEEEIEWWNdfssssssssssssssssssssssssssssssssssssssssss", 1, false, false);
          addCellWithColSpan(ContentTable, "Vehicle Name", 2, false, false, false);
          addCellWithRowSpan(ContentTable, "254567576", 3, false, false);
      
      
          addCellWithRowSpan(ContentTable, "Consignee Name", 1, true, true);
          addCellWithRowSpan(ContentTable, "Notify Name", 1, true, true);
          addCellWithRowSpan(ContentTable, "Port Of Loading", 1, true, true);
          addCellWithRowSpan(ContentTable, "Port Of Destination", 1, true, true);
          addCellWithRowSpan(ContentTable, "Millage", 1, true, true);
      
          addCellWithRowSpan(ContentTable, "Tata Birla", 1, false, false);
          addCellWithRowSpan(ContentTable, "Dipanki Jadav", 1, false, false);
          addCellWithRowSpan(ContentTable, "POL", 1, false, false);
          addCellWithRowSpan(ContentTable, "POD@", 1, false, false);
          addCellWithRowSpan(ContentTable, "Millagekhefjkhjhf", 1, false, false);
      
      
      
      
          addCellWithRowSpan(ContentTable, "2", 3, false, false);
          addCellWithRowSpan(ContentTable, "SHC!10002", 1, false, false);
          addCellWithRowSpan(ContentTable, "1-1-1", 1, false, false);
          addCellWithRowSpan(ContentTable, "VIEEEIEWWNdfssssssssssssssssssssssssssssssssssssssssss", 1, false, false);
          addCellWithColSpan(ContentTable, "Vehicle Name", 2, false, false, false);
          addCellWithRowSpan(ContentTable, "254567576", 3, false, false);
      
      
          addCellWithRowSpan(ContentTable, "Consignee Name", 1, true, true);
          addCellWithRowSpan(ContentTable, "Notify Name", 1, true, true);
          addCellWithRowSpan(ContentTable, "Port Of Loading", 1, true, true);
          addCellWithRowSpan(ContentTable, "Port Of Destination", 1, true, true);
          addCellWithRowSpan(ContentTable, "Millage", 1, true, true);
      
          addCellWithRowSpan(ContentTable, "Tata Birla", 1, false, false);
          addCellWithRowSpan(ContentTable, "Dipanki Jadav", 1, false, false);
          addCellWithRowSpan(ContentTable, "POL", 1, false, false);
          addCellWithRowSpan(ContentTable, "POD@", 1, false, false);
          addCellWithRowSpan(ContentTable, "Millagekhefjkhjhf", 1, false, false);
      
          addCellWithColSpan(ContentTable, "20i083408", 6, true, true, true);
          addCellWithRowSpan(ContentTable, "-----", 1, true, true);
      
          doc.Add(ContentTable);
      
          doc.Add(p1);
          Font fontH2 = new Font(bfTimes, 9, Font.BOLD);
          Paragraph p2 = new Paragraph("Company Name \n -----------\nPhone No: ----------------", fontH2);
          doc.Add(p2);
          doc.Close();
      }
      

      【讨论】:

        猜你喜欢
        • 2018-03-27
        • 1970-01-01
        • 2022-01-02
        • 2018-11-02
        • 2013-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多