2013-01-10 83 views
-1

我無法設置pdf表格高度使用iTextSharp通過asp.net 2010與c#。如果我將任何單元格的固定高度設置爲15,則單元格的數據不會顯示。在這種情況下,單元格顯示沒有數據。我想要更改單元格的高度小於15px,並在單元格中顯示數據。如何使用iTextSharp管理pdf表格高度,使用asp.net 2010雖然c#

private void CreatePDF() 
{ 
    string timestamp = DateTime.Now.ToString("MMddyyyy.HHmmss"); 
    string pdfFileName = Request.PhysicalApplicationPath + "\\Files\\" + "PDFCreation_Reporting_" + timestamp + ".pdf"; 
    Document myDocument = new Document(PageSize.A4, 5, 20, 50, 25); 
    PdfWriter.GetInstance(myDocument, new FileStream(pdfFileName, FileMode.Create)); 
    myDocument.Open(); 
    PdfPTable UpperTable1 = new PdfPTable(3); 
    PdfPTable UpperTable2 = new PdfPTable(3); 
    PdfPCell utcell1 = new PdfPCell(); 
    PdfPCell utcell2 = new PdfPCell(); 
    PdfPCell utcell3 = new PdfPCell(); 
    PdfPCell utcell4 = new PdfPCell(); 
    PdfPCell utcell5 = new PdfPCell(); 
    PdfPCell utcell6 = new PdfPCell(); 
    utcell1.FixedHeight = 15f; 
    utcell2.FixedHeight = 15f; 
    utcell3.FixedHeight = 15f; 
    utcell1.Padding = 0; 
    utcell2.Padding = 0; 
    utcell3.Padding = 0; 

    utcell4.FixedHeight = 15f; 
    utcell5.FixedHeight = 15f; 
    utcell6.FixedHeight = 15f; 
    utcell4.Padding = 0; 
    utcell5.Padding = 0; 
    utcell6.Padding = 0; 

    float[] colWidthsut1 = { 800, 685, 800 };//35%,30%,35% [2285] 
    UpperTable1.SetWidths(colWidthsut1); 
    UpperTable1.WidthPercentage = 100; 

    float[] colWidthsut2 = { 457, 1028, 800 };//20%,45%,35% [2285] 
    UpperTable2.SetWidths(colWidthsut2); 
    UpperTable2.WidthPercentage = 100; 

    string strTest1 = "Test data"; 
    string strTest2 = "Test data"; 
    string strTest3 = "Test Data"; 
    string strTest4 = "Test data"; 
    string strTest5 = ""; 
    string strTest6 = ""; 

    Phrase phrTest1 = new Phrase(); 
    Phrase phrTest2 = new Phrase(); 
    Phrase phrTest3 = new Phrase(); 
    Phrase phrTest4 = new Phrase(); 
    Phrase phrTest5 = new Phrase(); 
    Phrase phrTest6 = new Phrase(); 

    phrTest1.Font = new Font(Font.FontFamily.TIMES_ROMAN, 6, Font.NORMAL, BaseColor.BLACK); 
    phrTest2.Font = new Font(Font.FontFamily.TIMES_ROMAN, 6, Font.NORMAL, BaseColor.BLACK); 
    phrTest3.Font = new Font(Font.FontFamily.TIMES_ROMAN, 6, Font.NORMAL, BaseColor.BLACK); 
    phrTest4.Font = new Font(Font.FontFamily.TIMES_ROMAN, 6, Font.NORMAL, BaseColor.BLACK); 

    phrTest1.Add(strTest1); 
    phrTest2.Add(strTest2); 
    phrTest3.Add(strTest3); 
    phrTest4.Add(strTest4); 
    phrTest5.Add(strTest5); 
    phrTest6.Add(strTest6); 
    utcell1.AddElement(phrTest1); 
    utcell2.AddElement(phrTest2); 
    utcell3.AddElement(phrTest3); 
    utcell4.AddElement(phrTest4); 
    utcell5.AddElement(phrTest5); 
    utcell6.AddElement(phrTest6); 

    PdfPCell[] firstrowCells = { utcell1, utcell2, utcell3 }; 
    PdfPRow row1 = new PdfPRow(firstrowCells); 
    UpperTable1.Rows.Add(row1); 
    PdfPCell[] secondrowCells = { utcell4, utcell5, utcell6 }; 
    PdfPRow row2 = new PdfPRow(secondrowCells); 
    UpperTable2.Rows.Add(row2); 

    myDocument.Add(UpperTable1); 
    myDocument.Add(UpperTable2); 
    myDocument.Close(); 
} 
+1

如果你真的weant幫助,請提供的示例代碼(短,易於編譯)證明你做什麼,什麼失敗。 – mkl

回答

0

也許你可以修改邊界,爲我工作。 另外,你什麼時候修改單元格?也許你在將它添加到表格後做了這件事?然後它可能不會工作...

+0

請注意代碼 –

0

FileStream file = new FileStream(newFullPath,FileMode.Create,System.IO.FileAccess.Write); //創建文件流長度的字節數組
// byte [] ImageData = new byte [file.Length]; //將流中的字節塊讀入字節數組
//file.Read(ImageData,0,System.Convert.ToInt32(file.Length)); //關閉文件流

// string appPath = HttpContext.Current.Request.ApplicationPath; 
    CheckBox1.Visible = false; 
    Response.ContentType = "application/pdf"; 
    string appPath = HttpContext.Current.Request.ApplicationPath; 
    // FileStream file = new FileStream(Server.MapPath("~/save/") + (Convert.ToInt32(ViewState["cust"])) + "_" + (n++) + ".PDF", FileMode.Create, System.IO.FileAccess.Write); 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter hw = new HtmlTextWriter(sw); 
    pnlPerson.RenderControl(hw); 

    StringReader sr = new StringReader(sw.ToString()); 
    Document pdfDoc = new Document(PageSize.A4, 5, 20, 50, 25); 
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 
    // MemoryStream ms = new MemoryStream(); 
    PdfWriter.GetInstance(pdfDoc, file); 
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 
    pdfDoc.Open(); 
    htmlparser.Parse(sr); 
    hw.Close(); 
    sw.Close(); 
    pdfDoc.Close(); 
    sr.Close(); 
    file.Close(); 
    htmlparser.Close(); 
相關問題