iTextSharp 使用笔记

用于导出PDF

 

            iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(1000, 500);
            iTextSharp.text.Document document = new iTextSharp.text.Document(pageSize, 10, 10, 20, 20);
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fName, FileMode.Create));
            document.Open();
            BaseFont baseFont = BaseFont.CreateFont(@"C:\Windows\Fonts\msyh.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            iTextSharp.text.Font fontb = new iTextSharp.text.Font(baseFont);
             // 没起作用 ,可以用无边框单元格实现标题
            document.AddTitle(" 标题 ");

            PdfPTable pdfTable = new PdfPTable(new float[] { 50, 80, 80, 50, 100, 100, 70, 50, 95, 95 });
            pdfTable.TotalWidth = 900; //表格宽度
            pdfTable.LockedWidth = true;

            //将单元格添加到表格中
            AddPdfCell(pdfTable, fontb, "表头1");
            AddPdfCell(pdfTable, fontb, "表头2");
            AddPdfCell(pdfTable, fontb, "表头3");
            AddPdfCell(pdfTable, fontb, "表头4");
            AddPdfCell(pdfTable, fontb, "表头5");
            for (int i = 0; i < dgvUserList.Rows.Count; i++)
            {
                AddPdfCell(pdfTable, fontb, "内容1");
                AddPdfCell(pdfTable, fontb, "内容2");
                AddPdfCell(pdfTable, fontb, "内容3");
                AddPdfCell(pdfTable, fontb, "内容4");
                AddPdfCell(pdfTable, fontb, "内容5");
              
            }

            document.Add(pdfTable);//将表格添加到pdf文档中
            document.Close();
            writer.Close();            

 

 

 

 

用到的AddPdfCell

private static void AddPdfCell(PdfPTable pdfTable, iTextSharp.text.Font fontb, string text)
        {
            PdfPCell pdfCell = new PdfPCell(new Paragraph(text, fontb));
            pdfCell.HorizontalAlignment = 1;
            pdfCell.PaddingBottom = 10;
            pdfCell.PaddingTop = 10;
            pdfCell.BorderColor = BaseColor.BLACK;
            pdfCell.SetLeading(1.2f, 1.2f);
            pdfTable.AddCell(pdfCell);
        }

后期总结的一个 AddPdfCell

private static void AddPdfCell(PdfPTable pdfTable, 
            iTextSharp.text.Font fontb, string text,int rowspan,int colspan,BaseColor color)
        {
            PdfPCell pdfCell = new PdfPCell(new Paragraph(text, fontb));
            pdfCell.HorizontalAlignment = 1;
            pdfCell.PaddingBottom = 10;
            pdfCell.PaddingTop = 10;
            pdfCell.Rowspan = rowspan;
            pdfCell.Colspan = colspan;
            pdfCell.BorderColor = color;  
            pdfCell.SetLeading(1.2f, 1.2f);
            pdfTable.AddCell(pdfCell);
        }

导出图片

private void BeforeChartImg(List<string> chartData)
        {
            if (chartData == null) return;
            foreach (var strChar in chartData)
            {
                string[] arr = strChar.Split(new string[] { "base64," }, StringSplitOptions.RemoveEmptyEntries);
                byte[] byteArray = null;

                if (arr.Length > 1)
                {
                    byteArray = Convert.FromBase64String(arr[1]);
                }
                else
                {
                    MemoryStream tData = new MemoryStream(Encoding.UTF8.GetBytes(strChar));
                    MemoryStream tStream = new MemoryStream();
                    Svg.SvgDocument tSvgObj = SvgDocument.Open(tData);
                    tSvgObj.Draw().Save(tStream, ImageFormat.Png);
                    byteArray = tStream.ToArray();
                }
                Image png = Image.GetInstance(byteArray);
                //图片居中显示
                png.Alignment = 1;
                //缩放图片
                ZoomPicture(png, document.PageSize);
                document.Add(png);
            }
        }

 

posted @ 2020-07-14 15:40  fgnu  阅读(834)  评论(0编辑  收藏  举报