本文介绍了iTextSharp的问题:使用iTextSharp时,文本未按插入段落的方式显示块中的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #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);
}

推荐答案


这篇关于iTextSharp的问题:使用iTextSharp时,文本未按插入段落的方式显示块中的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 04:26