使用带有行距&的c#在asp.net中创建PDF.科尔斯潘 [英] Create PDF in asp.net using c# with row span & colspan

查看:81
本文介绍了使用带有行距&的c#在asp.net中创建PDF.科尔斯潘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have create PDF document to display text box value using iTextSharp in c# asp.net. the pdf created successfully. but the problem is, i am displaying value in table format. i want to do row span and col span. please any one help me with that..

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

推荐答案

用于使用c#的行跨度&在ASP.NET中创建PDF.科尔斯潘

For Create PDF in asp.net using c# with row span & colspan

1)请安装Install-Package iTextSharp-LGPL nuget

1) Please install the Install-Package 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

4)Below code will gives you an idea of how to use addCellWithRowSpan and 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();
}

这篇关于使用带有行距&的c#在asp.net中创建PDF.科尔斯潘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆