2011-04-25 168 views

回答

1

查看第四章中examples - 它完全與操作表交易。雖然,例子是在java中,你可以輕鬆地將它們與C#相關聯,因爲API是相同的。

1

請查看以下使用Itext的示例。

Itext Examples for tables lists and images

您可以進一步搜索HTML到PDF轉換器。有很多免費的工具可用。您需要傳遞包含該表格的HTML,並且它將返回PDF文檔。我開發了一個這樣的應用程序。讓我知道你是否需要它。

4

你可以看看還在http://www.mikesdotnetting.com/Category/20,東西的人的一些便利樣本通常後

PS:AbhiRoczz ...我個人做避免roseindia因爲他們往往竊取豐富的資源,這意味着他們將複製&粘貼而不會給原始所有者信用。加上現場被嚴重有組織,有一對多廣告

+1

roseindia實際上是一個非常糟糕的網站,有很多不好的做法和瑕疵 – 2015-12-04 05:10:56

+0

@VishalAnand yeap,自從我上面發佈後,沒有任何更改。它變得更糟 – 2015-12-04 15:29:37

7

添加命名空間:

using iTextSharp.text; 
using iTextSharp.text.pdf; 

代碼使用C#:

Document doc = new Document(PageSize.A4); 
    var output = new FileStream(Server.MapPath("MyFirstPDF.pdf"), FileMode.Create); 
    var writer = PdfWriter.GetInstance(doc, output); 


    doc.Open(); 


    var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/ABsIS_Logo.jpg")); 
    logo.SetAbsolutePosition(430, 770); 
    logo.ScaleAbsoluteHeight(30); 
    logo.ScaleAbsoluteWidth(70); 
    doc.Add(logo); 

    PdfPTable table1 = new PdfPTable(2); 
    table1.DefaultCell.Border = 0; 
    table1.WidthPercentage = 80; 


    PdfPCell cell11 = new PdfPCell(); 
    cell11.Colspan = 1; 
    cell11.AddElement(new Paragraph("ABC Traders Receipt", titleFont)); 

    cell11.AddElement(new Paragraph("Thankyou for shoping at ABC traders,your order details are below", subTitleFont)); 


    cell11.VerticalAlignment = Element.ALIGN_LEFT; 

    PdfPCell cell12 = new PdfPCell(); 


    cell12.VerticalAlignment = Element.ALIGN_CENTER; 


    table1.AddCell(cell11); 

    table1.AddCell(cell12); 


    PdfPTable table2 = new PdfPTable(3); 

    //One row added 

    PdfPCell cell21 = new PdfPCell(); 

    cell21.AddElement(new Paragraph("Photo Type")); 

    PdfPCell cell22 = new PdfPCell(); 

    cell22.AddElement(new Paragraph("No. of Copies")); 

    PdfPCell cell23 = new PdfPCell(); 

    cell23.AddElement(new Paragraph("Amount")); 


    table2.AddCell(cell21); 

    table2.AddCell(cell22); 

    table2.AddCell(cell23); 


    //New Row Added 

    PdfPCell cell31 = new PdfPCell(); 

    cell31.AddElement(new Paragraph("Safe")); 

    cell31.FixedHeight = 300.0f; 

    PdfPCell cell32 = new PdfPCell(); 

    cell32.AddElement(new Paragraph("2")); 

    cell32.FixedHeight = 300.0f; 

    PdfPCell cell33 = new PdfPCell(); 

    cell33.AddElement(new Paragraph("20.00 * " + "2" + " = " + (20 * Convert.ToInt32("2")) + ".00")); 

    cell33.FixedHeight = 300.0f; 



    table2.AddCell(cell31); 

    table2.AddCell(cell32); 

    table2.AddCell(cell33); 


    PdfPCell cell2A = new PdfPCell(table2); 

    cell2A.Colspan = 2; 

    table1.AddCell(cell2A); 

    PdfPCell cell41 = new PdfPCell(); 

    cell41.AddElement(new Paragraph("Name : " + "ABC")); 

    cell41.AddElement(new Paragraph("Advance : " + "advance")); 

    cell41.VerticalAlignment = Element.ALIGN_LEFT; 

    PdfPCell cell42 = new PdfPCell(); 

    cell42.AddElement(new Paragraph("Customer ID : " + "011")); 

    cell42.AddElement(new Paragraph("Balance : " + "3993")); 

    cell42.VerticalAlignment = Element.ALIGN_RIGHT; 


    table1.AddCell(cell41); 

    table1.AddCell(cell42); 


    doc.Add(table1); 

    doc.Close(); 
0
  byte[] bPDF = null; 
MemoryStream ms = new MemoryStream(); 
Document document = new Document(); //pdf document to write 
    var originalpath = HostingEnvironment.MapPath("~/PDFs/"); 
       if (!System.IO.Directory.Exists(originalpath)) 
        Directory.CreateDirectory(originalpath); 
       // Create a new PdfWriter object, specifying the outputstream 

       var pdfwriter = PdfWriter.GetInstance(document, ms); 

       // Open the Document for writing 
       document.Open(); 
       PdfPTable ParentTable = new PdfPTable(1); 
       ParentTable.TotalWidth = 500f; 
          ParentTable.LockedWidth = true; 
       ParentTable.HorizontalAlignment = 0; 
       ParentTable.ExtendLastRow = false; 
       PdfPCell heading = new PdfPCell(new Phrase("", HeaderFont)); 
       heading.PaddingBottom = 0f; 
       heading.PaddingTop = 0f; 
       heading.Border = 1; 
       ParentTable.AddCell(heading); 
       PdfPTable dataTableCellHeaderTable = new PdfPTable(3); 
       dataTableCellHeaderTable.HorizontalAlignment = 0; 
       float[] widths = new float[] { 2f, 2f, 5f }; 
       dataTableCellHeaderTable.SetWidths(widths); 
       PdfPCell cellSerialNumber = new PdfPCell(new Phrase(ScoringColoringModel.pdfFirstCellHeading, tableHeaderCellFont)) { Border = 0 }; 
       cellSerialNumber.PaddingTop = 7.5f; 
       cellSerialNumber.PaddingBottom = 7.5f; 
       cellSerialNumber.BorderColor = BaseColor.WHITE; 
       cellSerialNumber.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#D3D3D3").ToArgb()); 
       dataTableCellHeaderTable.AddCell(cellSerialNumber); 


       PdfPCell cellRegistration = new PdfPCell(new Phrase(ScoringColoringModel.pdfSecondCellHeading, tableHeaderCellFont)) { Border = PdfPCell.LEFT_BORDER }; 
       cellRegistration.PaddingTop = 7.5f; 
       cellRegistration.PaddingBottom = 7.5f; 
       cellRegistration.BorderColor = BaseColor.WHITE; 
       cellRegistration.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#D3D3D3").ToArgb()); 
       dataTableCellHeaderTable.AddCell(cellRegistration); 

       PdfPCell cellwordMark = new PdfPCell(new Phrase(ScoringColoringModel.pdfThirdCellHeading, tableHeaderCellFont)) { Border = PdfPCell.LEFT_BORDER }; 
       cellwordMark.PaddingTop = 7.5f; 
       cellwordMark.PaddingBottom = 7.5f; 
       cellwordMark.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#D3D3D3").ToArgb()); 
       cellwordMark.BorderColor = BaseColor.WHITE; 
       dataTableCellHeaderTable.AddCell(cellwordMark); 
       // to append more data create one table 
       PdfPTable datatable = new PdfPTable(3); 

       Font cellColor = new Font(); 

       foreach (DataRow dr in objDataTable.Rows) 
       { 

        if (dr.ItemArray[5].ToString() == "Yellow") 
        { 

         cellColor = FontFactory.GetFont(ScoringColoringModel.pdfFontSet, 12, new BaseColor(System.Drawing.ColorTranslator.FromHtml("#ffa800").ToArgb())); ; 

        } 
        else 
        { 
         cellColor = FontFactory.GetFont(ScoringColoringModel.pdfFontSet, 12, new BaseColor(System.Drawing.ColorTranslator.FromHtml(dr.ItemArray[5].ToString()).ToArgb())); ; 
        } 

        dataTableCellHeaderTable.AddCell(new PdfPCell(new Phrase(dr.ItemArray[0].ToString(), cellColor)) { PaddingBottom = 5, Border = 1, PaddingTop = 5 }); 



     dataTableCellHeaderTable.AddCell(new PdfPCell(new Phrase(dr.ItemArray[1].ToString(), cellColor)) { PaddingBottom = 5, Border = 1, PaddingTop = 5 }); 

         dataTableCellHeaderTable.AddCell(new PdfPCell(new Phrase(dr.ItemArray[3].ToString(), cellColor)) { PaddingBottom = 5, Border = 1, PaddingTop = 5 }); 

        } 
    //Here you can add multiple table 





        document.Add(ParentTable); 
    //document.Add(ParentTable1); and add table one by one to the document 

        document.Close(); 
        bPDF = ms.ToArray(); 
        // Close the writer instance 

        Response.ContentType = "application/pdf"; 
        Response.AddHeader("content-disposition", "attachment;filename=TrademarkSearchResult.pdf"); 
        Response.BinaryWrite(bPDF); 
        Response.End(); 
       }`enter code here`