Hi there, hope in your help.
With this c# code I need export in PDF format an GridView.
I use iTextSharp class.
My problem is the last column of gridview where I have set the Hyperlink tag.
In the export pdf the image of Hyperlink It is not aligned within the corresponding cell, because moved to the right all the values of the other cells.
Please check the attached image.
http://www.c-sharpcorner.com/forums/...28AM/US5Js.png
Anybody know how can I resolve this?
Can you suggest any other method?
Thank you in advance.
Please check the code below.
Code:
protected void ExportToPDFWithFormatting()
{
PdfPTable table = null;
int colCount = gvProducts.Columns.Count;
table = new PdfPTable(colCount);
table.HorizontalAlignment = 1;
table.WidthPercentage = 100;
int[] colWidths = new int[gvProducts.Columns.Count];
PdfPCell cell;
string cellText;
for (int colIndex = 0; colIndex < colCount; colIndex++)
{
colWidths[colIndex] = (int)gvProducts.Columns[colIndex].ItemStyle.Width.Value;
cellText = Server.HtmlDecode(gvProducts.HeaderRow.Cells[colIndex].Text);
BaseFont bf = BaseFont.CreateFont(
BaseFont.HELVETICA,
BaseFont.CP1252,
BaseFont.EMBEDDED,
false);
iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.BOLD, BaseColor.WHITE);
cell = new PdfPCell(new Phrase(cellText.Replace("<br />", Environment.NewLine), font));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.FixedHeight = 55f;
cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#a52a2a"));
table.AddCell(cell);
}
for (int rowIndex = 0; rowIndex < gvProducts.Rows.Count; rowIndex++)
{
if (gvProducts.Rows[rowIndex].RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < gvProducts.Columns.Count; j++)
{
if (j == 10)
{
cellText = (gvProducts.Rows[rowIndex].Cells[10].FindControl("img") as HyperLink).NavigateUrl;
string imagePath = Server.MapPath((gvProducts.Rows[rowIndex].Cells[10].FindControl("img") as HyperLink).ImageUrl);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagePath);
Chunk cImage = new Chunk(image, 0, 0, false);
Anchor anchor = new Anchor(cImage);
anchor.Reference = cellText.ToString();
table.AddCell(anchor);
}
else
{
cellText = Server.HtmlDecode(gvProducts.Rows[rowIndex].Cells[j].Text);
}
cell = new PdfPCell(new Phrase(cellText, FontFactory.GetFont("PrepareForExport", 8)));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.FixedHeight = 25f;
table.AddCell(cell);
}
}
}
Document pdfDoc = new Document(PageSize.A3.Rotate(), 30f, 30f, 30f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
table.HeaderRows = 1;
iTextSharp.text.Font fdefault = FontFactory.GetFont("Verdana", 18, iTextSharp.text.Font.BOLD, BaseColor.BLUE);
string s;
s = "Test Export";
pdfDoc.Add(new Paragraph(s, fdefault));
pdfDoc.Add(table);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;" + "filename=" + DateTime.Now + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
.aspx
<%--Start 10--%>
<asp:TemplateField HeaderText="attached" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HyperLink ID="img" runat="server" NavigateUrl='<%# Eval("attached").ToString() %>'
ImageUrl='<%#(String.IsNullOrEmpty(Eval("attached").ToString()) ? "/Images/cross-button.png" : "/Images/download.gif")%>'
ToolTip='<%#(String.IsNullOrEmpty(Eval("attached").ToString()) ? "No available" : "Available")%>'
Target="_blank" BorderStyle="None" ForeColor="Transparent">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<%--End 10--%>