• 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:WindowsFontsmsyh.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);
                }
            }
  • 相关阅读:
    oracle数据库使用sys_guid()返回乱码问题
    weblogic 环境改变后,重启应用后方法,报错java.lang.NoSuchMethodError: oracle.i18n.text.converter.CharacterConverterOGS.getInstance
    ORACLE数据库误删表后,没重新建表场景下数据恢复
    Linux环境下weblogic12开启远程debug端口
    关于HOSTS设置不生效的解决小方法
    dubbo调用 Caused by: com.alibaba.dubbo.remoting.RemotingException 异常解决方法
    textkit 研究,mark一下,一个不错的开源库:MLLabel(但是没有文档)
    swift流行UI库(github)
    Android高级第十一讲之不同系统间的区别
    欢快的使用Unity JSON吧
  • 原文地址:https://www.cnblogs.com/wu-xin/p/13299419.html
Copyright © 2020-2023  润新知