Click here to Skip to main content
15,884,836 members

Problem with iTextSharp:Text is not displaying sequence in way chunks inserted in paragraph using iTextSharp

Ravi Sargam asked:

Open original thread
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);
}
Tags: C#, ADO.NET, Windows Forms, PDF

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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