Ejemplo n.º 1
0
        /// <summary>
        /// 导出为PDF
        /// Pdf导出导出示例
        /// </summary>
        /// <param name="fileName">导出的pdf文件</param>
        /// <param name="list">需要导出的数据</param>
        /// <returns></returns>
        public static bool ToPdf(string fileName, List <string> list)
        {
            if (fileName == null || list == null)
            {
                return(false);
            }
            PdfPrinter pdf = null;

            try
            {
                var header = new PdfHeaderContent();
                //header.HeaderImage = Resources.title;
                pdf = new PdfPrinter(fileName, PageSize.A4, header);
                pdf.Init();
                var document = pdf.Doc;
                var font     = pdf.DefaultFont;

                //标题居中
                var p1 = new Paragraph("Title", font)
                {
                    Alignment = Element.ALIGN_CENTER
                };
                document.Add(p1);

                //添加一个表格
                var table = new PdfPTable(6)
                {
                    TotalWidth = document.Right - document.Left
                };
                float[] widths = { 100f, 420f };
                table.SetWidths(widths);
                //应用
                table.LockedWidth = true;

                //增加表头
                table.AddCell(new PdfPCell(new Phrase("image", font)));
                table.AddCell(new PdfPCell(new Phrase("des", font)));

                //增加详细数据
                table.AddCell(Image.GetInstance(null, BaseColor.WHITE));      //图片
                table.AddCell(new PdfPCell(new Phrase("description", font))); //文字

                document.Add(table);

                return(true);
            }
            finally
            {
                //注意一定要关闭,否则PDF中的内容将得不到保存
                if (pdf != null)
                {
                    pdf.Close();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="printPath">指定PDF文件导出路径</param>
        /// <param name="paperSize">纸张大小</param>
        /// <param name="headerContent">PDF文件页眉内容</param>
        public PdfPrinter(string printPath, Rectangle paperSize, PdfHeaderContent headerContent)
        {
            var rootPath = Environment.GetEnvironmentVariable("SystemRoot");

            rootPath = Path.Combine(rootPath, "Fonts", "simsun.ttc,1");
            var bfChinese = BaseFont.CreateFont(rootPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            DefaultFont     = new Font(bfChinese, 12, Font.NORMAL, new BaseColor(0, 0, 0));
            PrintPath       = printPath;
            DefaultPageSize = paperSize;
            HeaderContent   = headerContent;
        }