示例#1
0
        /// <summary>
        /// 打印主内容
        /// </summary>
        private void PrintContent()
        {
            PdfPTable pdfTable = new PdfPTable(Content.ColumnsCount);

            pdfTable.WidthPercentage = 110;
            Font font = new Font(BaseFont, 9);

            for (int i = 0; i < Content.RowsCount; i++)
            {
                for (int j = 0; j < Content.ColumnsCount; j++)
                {
                    Paragraph par = new Paragraph(string.Format("{0}", Content.GetValue(i, j)), font);
                    pdfTable.addCell(par);
                }
            }
            PdfDoc.Add(pdfTable);
        }
示例#2
0
        public static void CreatePDF()
        {
            //设置添加内容
            string pdfName  = "阿苏卫2017年8月报表";//文件名
            string name     = "阿苏卫填埋场";
            string location = "116.3454,40.15811,90";
            string year     = "2009.9.9";
            string note     = "  阿苏卫垃圾填埋场从1986年开始修建,1994年投入运营,占地26公顷,位于北京市西北郊。阿苏卫填埋场原来只负责处理北京市东城区和西城区的垃圾,每天处理1200吨。但后来每天处理垃圾量达到3500吨,这些垃圾包括来自朝阳区、顺义区和昌平区的商业垃圾。" +
                              "\n  垃圾填埋场紧邻二德庄、阿苏卫、百善、牛房圈四村。" +
                              "\n  2011年12月28日,昌平区举行“阿苏卫循环经济园”周边村庄搬迁安置房项目奠基仪式,此举标志着“阿苏卫循环经济园”项目正式启动。";

            iTextSharp.text.Image img1 = iTextSharp.text.Image.getInstance(@"reportimg/asw.jpg");
            img1.Alignment = iTextSharp.text.Image.MIDDLE;
            iTextSharp.text.Image img21 = iTextSharp.text.Image.getInstance(@"reportimg/chart.jpg");
            img21.scaleAbsolute(400, 270);//图片固定400*270保证排版不乱!!!
            img21.Alignment = iTextSharp.text.Image.MIDDLE;
            iTextSharp.text.Image img22 = iTextSharp.text.Image.getInstance(@"reportimg/chart1.jpg");
            img22.scaleAbsolute(400, 270);
            img22.Alignment = iTextSharp.text.Image.MIDDLE;
            iTextSharp.text.Image img8 = iTextSharp.text.Image.getInstance(@"reportimg/2d.jpg");
            img8.scaleAbsolute(400, 400);
            img8.Alignment = iTextSharp.text.Image.MIDDLE;
            iTextSharp.text.Image img9 = iTextSharp.text.Image.getInstance(@"reportimg/3d.jpg");
            img9.scaleAbsolute(400, 400);
            img9.Alignment = iTextSharp.text.Image.MIDDLE;

            // step 1: creation of a document-object
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);

            try
            {
                PdfWriter writer = PdfWriter.getInstance(document, new FileStream(pdfName + ".pdf", FileMode.Create));
                document.Open();
                //设置字体--添加字体库
                BaseFont             bfHei       = BaseFont.createFont(@"simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);   //黑体
                BaseFont             bfSun       = BaseFont.createFont(@"simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //新宋体
                iTextSharp.text.Font chapterFont = new iTextSharp.text.Font(bfHei, 18);                                              //FontFactory.getFont(FontFactory.HELVETICA, 24, iTextSharp.text.Font.NORMAL, new iTextSharp.text.Color(255, 0, 0));
                iTextSharp.text.Font sectionFont = new iTextSharp.text.Font(bfSun, 16);                                              //FontFactory.getFont(FontFactory.HELVETICA, 20, iTextSharp.text.Font.NORMAL, new iTextSharp.text.Color(0, 0, 255));
                iTextSharp.text.Font contentFont = new iTextSharp.text.Font(bfSun, 12);
                //--------------------1填埋场简介-----------------------
                Paragraph cTitle  = new Paragraph("填埋场简介", chapterFont);
                Chapter   chapter = new Chapter(cTitle, 1);
                document.Add(chapter);
                //--------------------1填埋场简介--表格
                Table table = new Table(2);
                table.BorderWidth = 1;
                table.BorderColor = new iTextSharp.text.Color(0, 0, 255);
                table.Padding     = 5;
                table.Spacing     = 5;
                //table.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
                Cell cell = new Cell("", contentFont);//图片
                cell.Rowspan           = 3;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.Add(img1);
                //cell.BorderColor = new iTextSharp.text.Color(255, 0, 0);
                table.addCell(cell);
                table.addCell(new Cell("名称:\n" + name, contentFont));         //名称
                table.addCell(new Cell("位置:\n" + location, contentFont));     //位置
                table.addCell(new Cell("建成年份:\n" + year, contentFont));       //建成年份
                cell                 = new Cell("简介:\n" + note, contentFont); //简介
                cell.Colspan         = 2;
                cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);
                table.addCell(cell);
                document.Add(table);
                //--------------------2基本信息-------------------------
                cTitle  = new Paragraph("基本信息", chapterFont);
                chapter = new Chapter(cTitle, 2);
                //--------------------2基本信息--img21
                Paragraph sTitle  = new Paragraph("填埋场数量按行政区划分", sectionFont);
                Section   section = chapter.addSection(sTitle, 2);//2表示处于那一章节
                Paragraph paraImg = new Paragraph();
                paraImg.Add(img21);
                section.Add(paraImg);
                //--------------------2基本信息--img22
                sTitle  = new Paragraph("填埋场数量按管理单位分", sectionFont);
                section = chapter.addSection(sTitle, 2);
                paraImg = new Paragraph();
                paraImg.Add(img22);
                section.Add(paraImg);
                document.Add(chapter);
                //--------------------3投资信息-------------------------
                cTitle  = new Paragraph("投资信息", chapterFont);
                chapter = new Chapter(cTitle, 3);
                //--------------------3投资信息--img31
                sTitle  = new Paragraph("库容统计", sectionFont);
                section = chapter.addSection(sTitle, 2);
                paraImg = new Paragraph();
                paraImg.Add(img21);
                section.Add(paraImg);
                //--------------------3投资信息--img32
                sTitle  = new Paragraph("当前投资情况", sectionFont);
                section = chapter.addSection(sTitle, 2);
                paraImg = new Paragraph();
                paraImg.Add(img22);
                section.Add(paraImg);
                //--------------------3投资信息--img33
                sTitle  = new Paragraph("垃圾处理量", sectionFont);
                section = chapter.addSection(sTitle, 2);
                paraImg = new Paragraph();
                paraImg.Add(img21);
                section.Add(paraImg);
                //--------------------3投资信息--img34
                sTitle  = new Paragraph("当前库容利用率", sectionFont);
                section = chapter.addSection(sTitle, 2);
                paraImg = new Paragraph();
                paraImg.Add(img22);
                section.Add(paraImg);
                document.Add(chapter);
                //--------------------4运营信息-------------------------
                cTitle  = new Paragraph("运营信息(当月)", chapterFont);
                chapter = new Chapter(cTitle, 4);
                document.Add(chapter);
                int       NumColumnsYY = 6;
                PdfPTable datatableYY  = new PdfPTable(NumColumnsYY);
                datatableYY.DefaultCell.Padding = 3;
                float[] headerwidthsYY = { 9, 8, 8, 10, 8, 11 }; // percentage
                datatableYY.setWidths(headerwidthsYY);
                //datatable.WidthPercentage = 100; // percentage
                datatableYY.DefaultCell.BorderWidth         = 2;
                datatableYY.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
                datatableYY.addCell(new Phrase("序号", sectionFont));
                datatableYY.addCell(new Phrase("数据1", sectionFont));
                datatableYY.addCell(new Phrase("数据2", sectionFont));
                datatableYY.addCell(new Phrase("数据3", sectionFont));
                datatableYY.addCell(new Phrase("数据4", sectionFont));
                datatableYY.addCell(new Phrase("数据5", sectionFont));
                datatableYY.HeaderRows = 1;  // this is the end of the table header
                datatableYY.DefaultCell.BorderWidth = 1;
                int maxYY = 26;
                for (int i = 1; i < maxYY; i++)
                {
                    if (i % 2 == 1)
                    {
                        datatableYY.DefaultCell.GrayFill = 0.9f;
                    }
                    for (int x = 0; x < NumColumnsYY; x++)
                    {
                        datatableYY.addCell(i + "-" + x);
                    }
                    if (i % 2 == 1)
                    {
                        datatableYY.DefaultCell.GrayFill = 0.0f;
                    }
                }
                document.Add(datatableYY);
                //--------------------5当前库容-------------------------
                cTitle  = new Paragraph("当前库容(当前)", chapterFont);
                chapter = new Chapter(cTitle, 5);
                document.Add(chapter);
                document.Add(img21);
                //--------------------6库容利用率-----------------------
                cTitle  = new Paragraph("库容利用率", chapterFont);
                chapter = new Chapter(cTitle, 6);
                document.Add(chapter);
                document.Add(img21);
                //--------------------7堆体安全预警---------------------
                cTitle  = new Paragraph("堆体安全预警", chapterFont);
                chapter = new Chapter(cTitle, 7);
                document.Add(chapter);
                int       NumColumnsYJ = 6;
                PdfPTable datatableYJ  = new PdfPTable(NumColumnsYJ);
                datatableYJ.DefaultCell.Padding = 3;
                float[] headerwidthsYJ = { 9, 8, 8, 10, 8, 11 }; // percentage
                datatableYJ.setWidths(headerwidthsYJ);
                //datatable.WidthPercentage = 100; // percentage
                datatableYJ.DefaultCell.BorderWidth         = 2;
                datatableYJ.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
                datatableYJ.addCell(new Phrase("序号", sectionFont));
                datatableYJ.addCell(new Phrase("数据1", sectionFont));
                datatableYJ.addCell(new Phrase("数据2", sectionFont));
                datatableYJ.addCell(new Phrase("数据3", sectionFont));
                datatableYJ.addCell(new Phrase("数据4", sectionFont));
                datatableYJ.addCell(new Phrase("数据5", sectionFont));
                datatableYJ.HeaderRows = 1;  // this is the end of the table header
                datatableYJ.DefaultCell.BorderWidth = 1;
                int maxYJ = 56;
                for (int i = 1; i < maxYJ; i++)
                {
                    if (i % 2 == 1)
                    {
                        datatableYJ.DefaultCell.GrayFill = 0.9f;
                    }
                    for (int x = 0; x < NumColumnsYJ; x++)
                    {
                        datatableYJ.addCell(i + "-" + x);
                    }
                    if (i % 2 == 1)
                    {
                        datatableYJ.DefaultCell.GrayFill = 0.0f;
                    }
                }
                document.Add(datatableYJ);
                //--------------------8二维设计图-----------------------
                cTitle  = new Paragraph("二维设计图", chapterFont);
                chapter = new Chapter(cTitle, 8);
                document.Add(chapter);
                document.Add(img8);
                //--------------------9三维设计图-----------------------
                cTitle  = new Paragraph("三维设计图", chapterFont);
                chapter = new Chapter(cTitle, 9);
                document.Add(chapter);
                document.Add(img9);
            }
            catch (Exception de)
            {
                Console.Error.WriteLine(de.StackTrace);
            }
            document.Close();
            Console.WriteLine("create success!");
            Console.Read();
        }
示例#3
0
文件: clsExport.cs 项目: 100009/hberp
        private bool ExportToPdf()
        {
            Document dc = new Document();

            try
            {
                FileStream fs     = new FileStream(_filename, FileMode.Create);
                PdfWriter  writer = PdfWriter.getInstance(dc, fs);
                dc.Open();

                dc.Add(new Paragraph(DateTime.Now.ToString()));
                dc.Add(new Paragraph(_title));
                dc.Add(new Paragraph(""));

                PdfPTable table;
                if (_gridType == 0)
                {
                    table = new PdfPTable(_exportGrid.Rows.Count);
                    for (int i = 0; i < _exportGrid.Columns.Count; i++)
                    {
                        table.addCell(new Phrase(_exportGrid.Columns[i].HeaderText));
                    }
                    for (int i = 0; i < _exportGrid.Rows.Count; i++)
                    {
                        for (int j = 0; j < _exportGrid.Columns.Count; j++)
                        {
                            if (_exportGrid.Rows[j].Cells[j].Value != null)
                            {
                                table.addCell(new Phrase(_exportGrid.Rows[i].Cells[j].Value.ToString()));
                            }
                            else
                            {
                                table.addCell(new Phrase(string.Empty));
                            }
                        }
                    }
                }
                else if (_gridType == 1)
                {
                    table = new PdfPTable(_flexGrid.Rows);
                    for (int i = 0; i < _flexGrid.Rows; i++)
                    {
                        for (int j = 0; j < _flexGrid.Cols; j++)
                        {
                            table.addCell(new Phrase(_flexGrid.get_TextMatrix(i, j)));
                        }
                    }
                }
                else
                {
                    float[] wids = new float[_listview.Columns.Count];
                    for (int i = 0; i < _listview.Columns.Count; i++)
                    {
                        wids[i] = _listview.Columns[i].Width;
                    }
                    table = new PdfPTable(wids);
                    for (int i = 0; i < _listview.Items.Count; i++)
                    {
                        table.addCell(new Phrase(_listview.Items[i].Text));
                        for (int j = 0; j < _flexGrid.Cols; j++)
                        {
                            table.addCell(new Phrase(_listview.Items[i].SubItems[j].Text));
                        }
                    }
                }
                dc.Add(table);
                dc.Close();
                return(true);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, clsTranslate.TranslateString("Information"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }
            finally
            {
                dc.Close();
                //_exportGrid.Dispose();
            }
        }
示例#4
0
        /// <summary>
        /// DataTable导出到PDF
        /// </summary>
        /// <param name="datatable">DataTable</param>
        /// <param name="PDFFilePath">导出的PDF存储路径</param>
        /// <param name="PdfSaveTitle">导出的文件名</param>
        /// <param name="FontPath">字体路径</param>
        /// <param name="FontSize">字体大小</param>
        /// <param name="widthmz">每一列宽度</param>
        /// <returns>布尔值</returns>
        public static string ConvertDataTableToPDF(DataTable datatable, string PDFFilePath, string PdfSaveTitle, string FontPath, float FontSize, float[] widthmz)
        {
            string strReturnSaveFileName = "";          //用于返回的文件名

            if (widthmz == null || widthmz.Length == 0) //如果每一列宽度未指定
            {
                widthmz = new float[datatable.Columns.Count];
                for (int i = 0; i < datatable.Columns.Count; i++)
                {
                    widthmz[i] = 11f;
                }
            }
            //初始化一个目标文档类
            Document document = new Document();
            //调用PDF的写入方法流

            //注意FileMode-Create表示如果目标文件不存在,则创建,如果已存在,则覆盖。
            PdfWriter writer = PdfWriter.getInstance(document, new FileStream(PDFFilePath + "\\" + PdfSaveTitle + ".pdf", FileMode.Create));

            strReturnSaveFileName = PDFFilePath + "\\" + PdfSaveTitle + ".pdf";
            try
            {
                //打开目标文档对象
                document.Open();

                // 添加页眉
                HeaderFooter header = new HeaderFooter(new Phrase(PdfSaveTitle), false);
                document.Header = header;
                // 添加页脚
                HeaderFooter footer = new HeaderFooter(new Phrase(PdfSaveTitle), true);
                footer.Border   = Rectangle.NO_BORDER;
                document.Footer = footer;

                //创建PDF文档中的字体
                BaseFont baseFont = BaseFont.createFont(FontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                //根据字体路径和字体大小属性创建字体
                Font font = new Font(baseFont, FontSize);

                Paragraph pTitle = new Paragraph(new Chunk(PdfSaveTitle, FontFactory.getFont(FontFactory.HELVETICA, 12)));
                document.Add(pTitle);

                //根据数据表内容创建一个PDF格式的表
                PdfPTable table = null;
                table = new PdfPTable(widthmz);


                //打印列名
                for (int j = 0; j < datatable.Columns.Count; j++)
                {
                    table.addCell(new Phrase(datatable.Columns[j].ColumnName.ToString(), font));
                }


                //遍历原table的内容
                for (int i = 0; i < datatable.Rows.Count; i++)
                {
                    for (int j = 0; j < datatable.Columns.Count; j++)
                    {
                        table.addCell(new Phrase(datatable.Rows[i][j].ToString(), font));
                    }
                }
                //在目标文档中添加转化后的表数据
                document.Add(table);
            }
            catch (Exception ec)
            {
                strReturnSaveFileName = "";
            }
            finally
            {
                //关闭目标文件
                document.Close();
                //关闭写入流
                writer.Close();
            }
            return(strReturnSaveFileName);
        }
示例#5
0
        //导出PDF文件方法
        /// <summary>
        /// DataTable导出到PDF
        /// </summary>
        /// <param name="datatable">DataTable</param>
        /// <param name="PDFFilePath">导出的PDF存储路径</param>
        /// <param name="PdfSaveTitle">导出的文件名</param>
        /// <param name="FontPath">字体路径</param>
        /// <param name="FontSize">字体大小</param>
        /// <param name="widthmz">每一列宽度</param>
        /// <returns>布尔值</returns>
        public static bool ConvertDataTableToPDF(DataTable datatable, string PDFFilePath, string PdfSaveTitle, Brush brush, float FontSize, float[] widthmz)
        {
            if (widthmz == null || widthmz.Length == 0)//如果每一列宽度未指定
            {
                widthmz = new float[datatable.Columns.Count];
                for (int i = 0; i < datatable.Columns.Count; i++)
                {
                    widthmz[i] = 30f;
                }
            }
            //初始化一个目标文档类
            Document document = new Document(PageSize.A4, 10, 10, 25, 25);
            //调用PDF的写入方法流
            //注意FileMode-Create表示如果目标文件不存在,则创建,如果已存在,则覆盖。
            PdfWriter writer = PdfWriter.getInstance(document, new FileStream(PDFFilePath, FileMode.Create));

            try
            {
                System.Windows.Media.Color color = (System.Windows.Media.Color)ColorConverter.ConvertFromString(brush.ToString());

                //打开目标文档对象
                document.Open();

                // 添加页眉
                HeaderFooter header = new HeaderFooter(new Phrase(PdfSaveTitle), false);
                document.Header = header;
                // 添加页脚
                HeaderFooter footer = new HeaderFooter(new Phrase(PdfSaveTitle), true);
                footer.Border   = Rectangle.NO_BORDER;
                document.Footer = footer;

                //创建PDF文档中的字体
                BaseFont baseFont = BaseFont.createFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                //根据字体路径和字体大小属性创建字体
                Font font = new Font(baseFont, FontSize);
                font.setColor(color.R, color.G, color.B);

                Paragraph pTitle = new Paragraph(new Chunk(PdfSaveTitle, FontFactory.getFont(FontFactory.HELVETICA, 12)));
                document.Add(pTitle);

                //根据数据表内容创建一个PDF格式的表
                PdfPTable table = null;
                table = new PdfPTable(widthmz);

                //打印列名
                for (int j = 0; j < datatable.Columns.Count; j++)
                {
                    string ColumnName = datatable.Columns[j].ColumnName.ToString();
                    table.addCell(new Phrase(ColumnName, font));
                }

                //遍历原table的内容
                for (int i = 0; i < datatable.Rows.Count; i++)
                {
                    for (int j = 0; j < datatable.Columns.Count; j++)
                    {
                        table.addCell(new Phrase(datatable.Rows[i][j].ToString(), font));
                    }
                }
                //在目标文档中添加转化后的表数据

                document.Add(table);
            }
            catch (Exception ec)
            {
                ec.GetBaseException();
                return(false);
            }
            finally
            {
                //关闭目标文件
                document.Close();
                //关闭写入流
                writer.Close();
            }
            return(true);
        }