Click here to Skip to main content
15,892,269 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
please modify my code.

ive been working for the font and the margin from left and right for about an hours..

this works perfectly.but margin from left and right seems not changing as well as font.

this is the part where the button export pdf.
C#
PdfPTable pdftable = new PdfPTable(GridView1.HeaderRow.Cells.Count);
foreach (TableCell headerCell in GridView1.HeaderRow.Cells)
{
    Font font = new Font();
    font.Color = new BaseColor(GridView1.HeaderStyle.ForeColor);

    PdfPCell pdfcell = new PdfPCell(new Phrase(headerCell.Text, font));
    pdfcell.BackgroundColor = new BaseColor(GridView1.HeaderStyle.BackColor);
    pdftable.AddCell(pdfcell); 
}       

foreach (GridViewRow gridViewRow in GridView1.Rows)
{
    foreach (TableCell tableCell in gridViewRow.Cells)
    {
        Font font = new Font();
        font.Color = new BaseColor(GridView1.RowStyle.ForeColor);

        PdfPCell pdfcell = new PdfPCell(new Phrase(tableCell.Text));
        pdfcell.BackgroundColor = new BaseColor(GridView1.RowStyle.BackColor);
        pdftable.AddCell(pdfcell);
    }
}

Document pdfDocument = new Document(PageSize.LETTER, 10f, 10f, 10f, 10f);
pdfDocument.SetPageSize(PageSize.LETTER.Rotate());
PdfWriter.GetInstance(pdfDocument, Response.OutputStream);
pdfDocument.Open();
float[] widths = new float[] { 1f, 2f,3f,10f,6f,4f}; // size per column
pdftable.SetWidths(widths);
pdfDocument.Add(pdftable);
pdfDocument.Close();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-disposition", "attachment;filename=jalisto.pdf");
Response.Write(pdfDocument);
Response.Flush();
Response.End();
Posted
Updated 26-May-15 18:39pm
v4
Comments
Sergey Alexandrovich Kryukov 26-May-15 13:58pm    
"Not working" is not informative. This is not really a question, but mostly some code dump.
—SA
jaylisto 27-May-15 0:43am    
better not answer or comment if you dont know the answer. your the one who is not informative. i remove the "not working" word from my question. you interpret that word wrong. im serous about this. can you give some little respect? my code is not for dump. you and your answer is.
Sergey Alexandrovich Kryukov 27-May-15 0:47am    
No, it's not better. I am not supposed to be informative, because I'm not formally answering yet, I'm just helping you to make your question answerable and face shameless aggression on your side. Who would like to help you if you behave this way? How can you expect respect if you are so disrespectful yourself?
—SA

1 solution

For font you can use font factory like
C#
Font font = FontFactory.GetFont("Arial", 10f, Font.NORMAL, iTextSharp.text.BaseColor.BLACK);


or instantiate new font() object like:
C#
BaseFont basefont= BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font font= new Font(basefont, 12, Font.ITALIC, Color.RED);



For more clarification you can follow this link :
http://www.mikesdotnetting.com/Article/81/itextsharp-working-with-fonts[^]
 
Share this answer
 
Comments
jaylisto 28-May-15 8:05am    
"sorry" at what part do i insert
Font font = FontFactory.GetFont("Arial", 10f, Font.NORMAL, iTextSharp.text.BaseColor.BLACK);
Rojalin Sahoo 28-May-15 8:14am    
inside your foreach loop you created one font object instead of that you can use the fontfactory code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900