当前位置:编程学习 > C#/ASP.NET >>

[求助]关于C#生成PDF文件

1.在win7下面生成成功!

2.在XP服务器下生成报错:The document has no pages. 


   public bool CreateReport(string file_path)
        {
            bool returnValue = false;
            Document doc = null;
            PdfWriter writer = null;

            string filename = file_path + "/" + CurrentReport.CurrentClass.SchoolInfo.SchoolName + CurrentReport.CurrentClass.ClassName + Configure.ExamGrade[CurrentReport.CurrentExam.ExamName1] + Configure.ExamSemester[CurrentReport.CurrentExam.ExamName2-1] + Configure.ExamType[CurrentReport.CurrentExam.Type-1] +  "-成绩分析报告.pdf";
            if (false == Directory.Exists(file_path))
                Directory.CreateDirectory(file_path);
            if (File.Exists(filename))
                File.Delete(filename);

            try
            {
                doc = new Document(iTextSharp.text.PageSize.A4, 25, 25, 50, 40);//定义pdf大小,设置上下左右边距
                writer = PdfWriter.GetInstance(doc, new FileStream(filename, FileMode.Create));//生成pdf路径,创建文件流

                doc.Open();
                writer.PageEvent = new HeaderAndFooterEvent();

                string title = "aasswwe";
                doc.Add(ExamAnalysiseReportFormat.InsertTitleContent(title));

            }
            catch (Exception ex)
            {
                LogHelper.Error("CreateReport", ex);
            }
            finally
            {
                try
                {
                    if (doc.IsOpen())
                    {
                        if (writer != null)
                        {
                            writer.Flush();
                            writer.CloseStream = true;
                        }
                        doc.Close();
                    }
                    PAGE_NUMBER = 1;
                }
                catch (Exception ex)
                {
                    LogHelper.Error("CreateReport", ex);
                    returnValue = false;
                }
            }
            return returnValue;
        }
--------------------编程问答--------------------

 public static Paragraph InsertTitleContent(string text)
        {

            iTextSharp.text.Font font = BaseFontAndSize("华文中宋", 16, Font.BOLD);

            Paragraph paragraph = new Paragraph(text, font);

            paragraph.Alignment = Element.ALIGN_CENTER;

            paragraph.SpacingBefore = 5;

            paragraph.SpacingAfter = 5;
            paragraph.SetLeading(1, 2);
            return paragraph;
        }
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,