使用iTextSharp.text将表格内容导出为pdf格式的文本格式

时间:2011-06-07 07:40:39

标签: c# asp.net-mvc

我正在尝试通过单击链接按钮的代码导出我的pdf中的数据库表内容我在链接按钮上调用此函数

        public void EXportCaseAsText(int caseid)
        {

        DateTime pubdate = new DateTime();
       // System.IO.MemoryStream msReport = new System.IO.MemoryStream();
        int pageno = 1, questionno = 1;
        int count = BlFactory.CipCaseWorkflowItemBl.LoadWorkflowForCase(caseid).Count;
        Font HeaderFont = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 14, Font.BOLD, iTextSharp.text.Color.RED);
        Font OptionsFont = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, Font.BOLD, iTextSharp.text.Color.BLACK);
        Font TrueFont = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, Font.UNDERLINE, iTextSharp.text.Color.BLUE);
        Font dataFont = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, Font.NORMAL, iTextSharp.text.Color.BLACK);
        Font datefont = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 14, Font.BOLD, iTextSharp.text.Color.BLUE);
        if (count > 0)
        {

            try
            {
                Document document = new Document(PageSize.A4);
                //PdfWriter.GetInstance(document, new FileStream("D:\\RCMS\\Acr3S.Cmf.Web\\bin\\Debug\\my.pdf", FileMode.Create));

                PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);

                document.Open();

                DateTime pub = getCasePublishedDate(caseid);
                if (pub.ToShortDateString().Equals("1/1/0001"))
                {
                    Paragraph paragraph = new Paragraph(new Paragraph("Not yet published",datefont));
                    paragraph.Alignment = Element.ALIGN_CENTER;
                    document.Add(paragraph);
                }
                else
                {
                    string date = DateFormatter.GetFormattedDate(pub);
                    Paragraph paragraph = new Paragraph(new Paragraph(date, datefont));
                    paragraph.Alignment = Element.ALIGN_CENTER;
                    document.Add(paragraph);

                }

                for (int i = 0; i < count; i++)
                {
                    CipCaseWorkflowItem item = (new NHibernate.DebugHelpers.CollectionProxy<Acr3S.Core.Domain.CipCaseWorkflowItem>
                                            (BlFactory.CipCaseWorkflowItemBl.LoadWorkflowForCase(caseid))).Items[i];

                    switch (item.Type)
                    {
                        case CaseContentItemType.BaseCaseTextElement:
                            {
                                CipTextElementScriptItem textItem = (CipTextElementScriptItem)BlFactory.CipCaseWorkflowItemBl.LoadWorkflowForCase(caseid, item.Id);

                                if (textItem != null && textItem.Id!=null && textItem.ContentElement !=null)
                                {
                                    switch (textItem.ElementType.ToString())
                                    {
                                        case "Diagnosis":
                                            {
                                                Paragraph par = new Paragraph("Diagnosis", HeaderFont);
                                                //Chunk chunk = new Chunk(textItem.ContentElement.PlainText.Trim(), dataFont);
                                                //par.Add("\n");
                                                //par.Add(chunk);
                                                document.Add(par);
                                                StyleSheet styles = new StyleSheet();
                                                ArrayList objects = HTMLWorker.ParseToList(new StringReader(textItem.ContentElement.Text), styles);
                                                for (int k = 0; k < objects.Count; ++k)
                                                {
                                                    document.Add((IElement)objects[k]);
                                                }
                                                break;
                                            }
                                        case "History":
                                            {
                                                Paragraph par = new Paragraph("History", HeaderFont);
                                                //Chunk chunk = new Chunk(textItem.ContentElement.PlainText.Trim(), dataFont);
                                                //par.Add("\n");
                                                //par.Add(chunk);
                                                document.Add(par);
                                                StyleSheet styles = new StyleSheet();
                                                ArrayList objects = HTMLWorker.ParseToList(new StringReader(textItem.ContentElement.Text), styles);
                                                for (int k = 0; k < objects.Count; ++k)
                                                {
                                                    document.Add((IElement)objects[k]);
                                                }
                                                break;
                                            }



                                        case "Discussion":
                                            {
                                                Paragraph par = new Paragraph("Discussion", HeaderFont);
                                                //par.Add("\n");
                                                //Chunk chunk = new Chunk(textItem.ContentElement.PlainText.Trim(), dataFont);
                                                //par.Add(chunk);
                                                document.Add(par);
                                                StyleSheet styles = new StyleSheet();
                                                ArrayList objects = HTMLWorker.ParseToList(new StringReader(textItem.ContentElement.Text), styles);
                                                for (int k = 0; k < objects.Count; ++k)
                                                {
                                                    document.Add((IElement)objects[k]);
                                                }
                                                break;
                                            }


                                        case "CasePoints":
                                            {
                                                Paragraph par = new Paragraph("Case Points", HeaderFont);
                                                //Chunk chunk = new Chunk(textItem.ContentElement.PlainText.Trim(), dataFont);
                                                //par.Add("\n");
                                                //par.Add(chunk);
                                                document.Add(par);
                                                StyleSheet styles = new StyleSheet();
                                                ArrayList objects = HTMLWorker.ParseToList(new StringReader(textItem.ContentElement.Text), styles);
                                                for (int k = 0; k < objects.Count; ++k)
                                                {
                                                    document.Add((IElement)objects[k]);
                                                }
                                                break;
                                            }



                                    }
                                }

                                break;
                            }

                    }
                }
                document.Close();
                Response.AddHeader("content-disposition", "attachment;filename=" + getCaseName(caseid) + ".pdf");
                Response.ContentType = "application/pdf";
                Response.End();

            }

            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }


        }

    }

问题是某些时候讨论数据没有在pdf中显示某些历史数据没有在pdf中显示有时案例点数据没有以PDF格式显示而所有数据都存在于表中

我不知道为什么会发生这种情况

1 个答案:

答案 0 :(得分:1)

替换

document.Add(par);
StyleSheet styles = new StyleSheet();
ArrayList objects = HTMLWorker.ParseToList(new StringReader(textItem.ContentElement.Text), styles);
for (int k = 0; k < objects.Count; ++k)
{
  document.Add((IElement)objects[k]);
}

通过

//实施新守则:

document.Add(par);
StyleSheet styles = new StyleSheet();
string tempText = textItem.ContentElement.Text;
tempText = tempText.Replace("\"", "&#34;");
ArrayList objects = HTMLWorker.ParseToList(new StringReader(tempText), styles);
for (int k = 0; k < objects.Count; ++k)
{
    document.Add((IElement)objects[k]);
}

这样可以正常工作......