這裏我試圖將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:
這裏是轉換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:
我想要在PDF中具有相同列寬的確切gridview。
感謝您的回答。我在列表視圖中提到了列寬,但是我的問題沒有在pdf中顯示完整的gridview(即)查看分組的gridview圖像,總共有8行,但我只有5行pdf(pdf圖像)。 – raje