2013-12-13 87 views
2

這裏我試圖將PDF轉換爲PDF時沒有獲得PDF中的GridView列寬。因此,我使用另一種方法生成PDF(即)創建了itextsharp從GridView。在這裏我得到了列寬的大小,我給了GridView列ItemStyle-Width.But中,但我已經在該GridView中做了分組列,所以我沒有得到完全GridView相同的列寬在PDF.can任何人都請幫助我。將gridview轉換爲itextsharp表並使用asp.net生成pdf

這裏是源:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    onsorting="GridView1_Sorting"> 
    <HeaderStyle BackColor="#4F81BD" Font-Bold="true" ForeColor="#ffffff" HorizontalAlign="Center" Font-Size="12px"/> 
    <AlternatingRowStyle BackColor="#D3DFEE" /> 
    <RowStyle /> 
    <Columns> 
     <asp:BoundField DataField="Category" HeaderText="Category" 
      SortExpression="Category" ItemStyle-Width="100px"/> 
      <asp:BoundField DataField="Partno" HeaderText="Partno" 
      ItemStyle-Width="100px"/> 
      <asp:BoundField DataField="Productname" HeaderText="Productname" 
      ItemStyle-Width="300px"/> 
      <asp:BoundField DataField="Brand" HeaderText="Brand" 
      ItemStyle-Width="100px"/> 
      <asp:BoundField DataField="Qty" HeaderText="Qty" 
      ItemStyle-Width="50px"/> 
      <asp:BoundField DataField="Unitprice" HeaderText="Unitprice" 
      ItemStyle-Width="50px"/> 
      <asp:BoundField DataField="Totalprice" HeaderText="Totalprice" 
      ItemStyle-Width="50px"/> 
    </Columns> 

</asp:GridView> 

這裏是分組的GridView:

enter image description here

這裏是轉換GridView控件iTextSharp的表,生成PDF的代碼:

//Create a table 
iTextSharp.text.Table table = new iTextSharp.text.Table(GridView1.Columns.Count); 
table.Cellpadding = 5; 
//Set the column widths 
int[] widths = new int[GridView1.Columns.Count]; 
for (int x = 0; x < GridView1.Columns.Count; x++) 
{ 
    widths[x] = (int)GridView1.Columns[x].ItemStyle.Width.Value; 
    string cellText = Server.HtmlDecode(GridView1.HeaderRow.Cells[x].Text); 
    iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText); 
    cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#008000")); 
    table.AddCell(cell); 
} 

table.SetWidths(widths); 
//Transfer rows from GridView to table 

for (int i = 0; i < GridView1.Rows.Count; i++) 
{ 
    if (GridView1.Rows[i].RowType == DataControlRowType.DataRow) 
    { 
     for (int j = 0; j < GridView1.Columns.Count; j++) 
     { 
      string cellText = Page.Server.HtmlDecode(GridView1.Rows[i].Cells[j].Text); 
      iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText); 

      //Set Color of Alternating row 
      if (i % 2 != 0) 
      { 
       cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#C2D69B")); 
      } 

      table.AddCell(cell); 
     } 
    } 
} 

//Create the PDF Document 

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); 
PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 
pdfDoc.Open(); 
pdfDoc.Add(table); 
pdfDoc.Close(); 
Response.ContentType = "application/pdf"; 
Response.AddHeader("content-disposition", "attachment;" + 
           "filename=GridViewExport.pdf"); 
Response.Cache.SetCacheability(HttpCacheability.NoCache); 
Response.Write(pdfDoc); 
Response.End(); 

這裏是PDF:

enter image description here

我想要在PDF中具有相同列寬的確切gridview。

回答

2

PdfPTable默認使用相對寬度。當你說table.SetWidths(widths) iText總結widths並將每個單獨的寬度除以該總和以獲得百分比。所以1, 2, 310, 20, 30甚至2, 4, 6相同。您想要使用SetTotalWidth(widths)以及設置LockedWidth = true強制絕對寬度。

table.SetTotalWidth(widths); 
table.LockedWidth = true; 

但是,這樣做是在聲明您現在知道並控制了表的水平佈局。如果你橫向溢出頁面,你將不會得到任何包裝,事情就會中斷。如果需要,您還可以聲明保證金空間可以接受。

+0

感謝您的回答。我在列表視圖中提到了列寬,但是我的問題沒有在pdf中顯示完整的gridview(即)查看分組的gridview圖像,總共有8行,但我只有5行pdf(pdf圖像)。 – raje

相關問題