Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 3rd, 2015, 04:28 AM
Authorized User
 
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default iTextSharp class for export pdf gridview in c#

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--%>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Do I print one record perpage in iTextSharp ysfkay BOOK: Professional ASP.NET MVC 3 0 December 27th, 2011 08:13 AM
Crystal export to pdf rebeccab Crystal Reports 6 August 25th, 2008 02:38 AM
export to pdf nilsan ASP.NET 1.0 and 1.1 Professional 0 February 21st, 2007 06:24 AM
Fail To Export To PDF muhammadhammad78 BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 March 9th, 2005 01:04 AM
Export to .pdf bgottschall Crystal Reports 1 July 21st, 2004 06:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.