Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
C#
 #region setFonts
 iTextSharp.text.Font titleFont = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.BOLD);
 iTextSharp.text.Font subTitleFont = FontFactory.GetFont("Times New Roman", 8, iTextSharp.text.Font.BOLD);
 iTextSharp.text.Font noteFont = FontFactory.GetFont("Times New Roman", 8, iTextSharp.text.Font.ITALIC);
 iTextSharp.text.Font qTitleFont = FontFactory.GetFont("Times New Roman", 9, iTextSharp.text.Font.BOLD);
 iTextSharp.text.Font QuesFont = FontFactory.GetFont("Times New Roman", 9, iTextSharp.text.Font.BOLD);
 iTextSharp.text.Font footnote = FontFactory.GetFont("Times New Roman", 8, iTextSharp.text.Font.ITALIC);
 iTextSharp.text.Font optionFont = FontFactory.GetFont("Times New Roman", 8, iTextSharp.text.Font.NORMAL);
 iTextSharp.text.Font ansFont = FontFactory.GetFont("Times New Roman", 8, iTextSharp.text.Font.BOLD);
 #endregion
Document doc = new Document(iTextSharp.text.PageSize.A4, 50, 50, 25, 25);
try
{
    PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("d:\\" + lblName.Text + ".pdf", FileMode.Create));

    //Open Document to write
    doc.Open();

    InitDoc(iTextSharp.text.PageSize.A4, 50, 50, 25, 25);
    strTotalMarks = lblMarks.Text;
    strEDate = lblDate.Text;
    strTotalTime = lblTimeMsg.Text.Trim();
    examName = lblExam.Text;
    string stdname = lblStd.Text;
    SetHeading(true);

    Paragraph headPara = new Paragraph(lblSchool.Text, titleFont);
    headPara.SetAlignment("Center");
    doc.Add(headPara);

    iTextSharp.text.Table HeadTable = new iTextSharp.text.Table(2, 5);    // 3 columns, 2 rows
    HeadTable.BorderColor = new iTextSharp.text.Color(255, 255, 255);
    HeadTable.Width = 100F;

    Chunk chk1 = new Chunk("Standard : " + lblStd.Text, subTitleFont);
    Cell c1 = new Cell(chk1);
    c1.Border = iTextSharp.text.Rectangle.NO_BORDER;
    c1.HorizontalAlignment = Element.ALIGN_LEFT;

    Chunk chk2 = new Chunk("Exam : " + lblExam.Text, subTitleFont);
    Cell c2 = new Cell(chk2);
    c2.Border = iTextSharp.text.Rectangle.NO_BORDER;
    c2.HorizontalAlignment = Element.ALIGN_RIGHT;

    Chunk chk3 = new Chunk("Subject : " + label2.Text.Substring(11).ToString(), subTitleFont);
    Cell c3 = new Cell(chk3);
    c3.Border = iTextSharp.text.Rectangle.NO_BORDER;
    c3.HorizontalAlignment = Element.ALIGN_LEFT;

    Chunk chk4 = new Chunk("Total Marks : " + lblMarks.Text, subTitleFont);
    Cell c4 = new Cell(chk4);
    c4.Border = iTextSharp.text.Rectangle.NO_BORDER;
    c4.HorizontalAlignment = Element.ALIGN_RIGHT;

    Chunk chk5 = new Chunk("Division : " + lblDiv.Text, subTitleFont);
    Cell c5 = new Cell(chk5);
    c5.Border = iTextSharp.text.Rectangle.NO_BORDER;
    c5.HorizontalAlignment = Element.ALIGN_LEFT;

    Chunk chk6 = new Chunk("Duration in Min : " + Durationinmin, subTitleFont);
    Cell c6 = new Cell(chk6);
    c6.Border = iTextSharp.text.Rectangle.NO_BORDER;
    c6.HorizontalAlignment = Element.ALIGN_RIGHT;

    Chunk chk7 = new Chunk();
    Cell c7 = new Cell(chk7);
    c7.Border = iTextSharp.text.Rectangle.NO_BORDER;

    Chunk chk8 = new Chunk("Exam Date : " + lblDate.Text, subTitleFont);
    Cell c8 = new Cell(chk8);
    c8.Border = iTextSharp.text.Rectangle.NO_BORDER;
    c8.HorizontalAlignment = Element.ALIGN_RIGHT;

    HeadTable.AddCell(c1);
    HeadTable.AddCell(c2);
    HeadTable.AddCell(c3);
    HeadTable.AddCell(c4);
    HeadTable.AddCell(c5);
    HeadTable.AddCell(c6);
    HeadTable.AddCell(c7);
    HeadTable.AddCell(c8);
    doc.Add(HeadTable);

    int counts = 0;
    int k = 0;
    Paragraph p = new Paragraph();
    Chunk ch;
    DataTable dt = new DataTable();
    dt = c.display("select Question,A,B,C,D,Ans from Questions");
    foreach (DataRow dr in dt.Rows)
    {
        counts++;
        if (dt.Rows[k].ItemArray[0].ToString().Length > 0)
        {
            ch = new Chunk(counts + ". " + dr[0].ToString(), QuesFont);
            Chunk ch1;
            Chunk ch2;
            Chunk ch3;
            Chunk ch4;
            Chunk ch1 = new Chunk("A. " + dr[1].ToString(), optionFont);
            Chunk ch2 = new Chunk("B. " + dr[2].ToString(), optionFont);
            Chunk ch3 = new Chunk("C. " + dr[3].ToString(), optionFont);
            Chunk ch4 = new Chunk("D. " + dr[4].ToString(), optionFont);
            p.Add("\n");
            if (counts > 1)
            {
                p.Add("\n");
            }
            p.Add(ch);
            p.Add("\n");
            p.Add(ch1);
            p.Add("\n");
            p.Add(ch2);
            p.Add("\n");
            p.Add(ch3);
            p.Add("\n");
            p.Add(ch4);
            //p.Add("a");
            //p.Add("b");
            p.Add("\n");
            //p.Add("\n");
        }
        else
        {

        }
        k++;
    }
    doc.Add(p);

    strEndNote = "All The Best!!";
    //AddEndNote();
    doc.Add(Chunk.NEWLINE);
    Paragraph endMessage = new Paragraph(endNote, footnote);
    endMessage.SetAlignment("Center");
    doc.Add(endMessage);
}
Posted
Updated 23-May-12 23:41pm
v5

1 solution

This code cannot even compile. What's the use of asking questions, if you fail to copy-paste the real code? Please see: ch1 is attempted to define 4 times, and ch2, ch3 and ch4 are not defined. Just look at your actual code with more attention, perhaps you see some simple problem like that, use the debugger.

If it does not help, post the real code; use "Improve question" above.

Good luck,
—SA
 
Share this answer
 
Comments
Maciej Los 24-May-12 4:19am    
Good answer, my 5!
Sergey Alexandrovich Kryukov 24-May-12 11:27am    
Thank you, Maciej.
--SA
Ravi Sargam 24-May-12 4:32am    
Now can you help me SA
Sergey Alexandrovich Kryukov 24-May-12 11:29am    
Run it under debugger and check up each p.Add. Why did you remove the text explaining how the wrong output looks?
--SA
Ravi Sargam 24-May-12 23:54pm    
I checked it each chunk, it is adding in right order but displaying in random

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