.net core datatable 导出 pdf 支持中文
1、nuget 安装 iTextSharp (V4.1.6) to .NET Core.
2、code
public DataTable ToDataTable<T>(IEnumerable<T> collection, Dictionary<string, string> columnMaps = null)
{
var props = typeof(T).GetProperties();
var dt = new DataTable();
dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
if (collection.Count() > 0)
{
for (int i = 0; i < collection.Count(); i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in props)
{
object obj = pi.GetValue(collection.ElementAt(i), null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
dt.LoadDataRow(array, true);
}
} if (columnMaps != null)
{
//修改列名
foreach (var item in columnMaps)
{
dt.Columns[item.Key].ColumnName = item.Value;
}
} return dt;
} public bool ConvertDataTableToPDF(DataTable Data, string PDFFile, float FontSize)
{
try
{
GenepointLog.Info($"开始导出pdf--{PDFFile}");
//定义页面的大小
Rectangle pageSize = PageSize.A4;
Document document = new Document(pageSize);
//默认页面大小
//Document document = new Document();
var stream = new FileStream(PDFFile, FileMode.OpenOrCreate);
PdfWriter writer = PdfWriter.GetInstance(document, stream);
document.Open();
//设置字体
//BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1257, BaseFont.NOT_EMBEDDED);
BaseFont.AddToResourceSearch("iTextAsian.dll");
BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
BaseFont bf = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//BaseFont bf = BaseFont.CreateFont("STSong-Light,Italic", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, FontSize); PdfPTable table = new PdfPTable(Data.Columns.Count);
table.WidthPercentage = 100; // percentage
table.DefaultCell.Padding = 1;
table.DefaultCell.BorderWidth = 0.2f;
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
//将datatable表头转换成PDFTable的表头
foreach (DataColumn dc in Data.Columns)
{
table.AddCell(new Phrase(dc.ColumnName.ToString(), font));
}
//插入数据
for (int i = 0; i < Data.Rows.Count; i++)
{
for (int j = 0; j < Data.Columns.Count; j++)
{
table.AddCell(new Phrase(Data.Rows[i][j].ToString(), font));
}
}
document.Add(table);
document.Close();
writer.Close();
stream.Dispose();
GenepointLog.Info("结束导出pdf");
return true;
}
catch (Exception ex)
{
GenepointLog.Error($"导出pdf失败{PDFFile}", ex);
return false;
} }
.net core datatable 导出 pdf 支持中文的更多相关文章
- flying-saucer + iText + Freemarker实现pdf的导出, 支持中文、css以及图片
前言 项目中有个需求,需要将合同内容导出成pdf.上网查阅到了 iText , iText 是一个生成PDF文档的开源Java库,能够动态的从XML或者数据库生成PDF,同时还可以对文档进行加密,权限 ...
- iText + Freemarker实现pdf的导出,支持中文、css以及图片,页眉页脚,页眉添加图片
本文为了记录自己的实现过程,具体的实现步骤是参考博文 https://www.cnblogs.com/youzhibing/p/7692366.html 来实现的,只是在他的基础上添加了页眉页脚及页眉 ...
- mpdf导出pdf,中文符号乱码
改源码: 打开vendor/mpdf/mpdf/src/Config/FontVariables.php 在最后一行加入: "gb" => [ 'R' => 'gb.t ...
- Jaspersoft Studio 导出PDF格式中文不显示
1:设置字体 2:应用上面设置的字体
- Magento PDF发票,支持中文,以及修改的办法
Magento PDF发票,支持中文,以及修改的办法. 如果让magento的PDF发票支持中文.Magento生成PDF发票.使用的是zend framework的zend_pdf类. 下面是一 ...
- JS导出PDF插件(支持中文、图片使用路径)
在WEB上想做一个导出PDF的功能,发现jsPDF比较多人推荐,遗憾的是不支持中文,最后找到pdfmake,很好地解决了此问题.它的效果可以先到http://pdfmake.org/playgroun ...
- tcpdf导出pdf数据支持中文的解决方案
步骤如下:1.确保你测试tcpdf能正常输出英文内容的pdf2.测试输入中文内容后显示是?的乱码或者空白分析原因,是因为我们输入的中文,tcpdf字体库并不支持,因此乱码或者空白显示 添加一个合适的字 ...
- Jupyter Notebook 导出PDF与Latex中文支持
Jupyter Notebook 最近搞机器学习用到了Jupyter Notebook. 作为一个实时记事本,有时需要将内容导出为PDF. 但是,Jupyter Notebook自带的File -&g ...
- jupyter notebook 目录配置、导出 tex 和 pdf 及中文支持
环境:macbook pro, mactex, jupyter notebook, brew 安装pandoc从而支持格式转换为tex: brew install pandoc 修改tex artic ...
- 解决html导出pdf中文乱码问题的正确姿势
简介 本文使用jspdf 1.5.3版.GitHub地址:https://github.com/MrRio/jsPDF jspdf是歪果仁开发的,因此在一开始就没想过支持非英文以外的文字,这就导致了非 ...
随机推荐
- 使用 IntelliJ IDEA 构建 Spring Framework 5.3.21 源码问题解决
源码版本 1.下载地址:https://github.com/spring-projects/spring-framework/tags 2.选择要构建的源码版本并下载,例如:5.3.21 相关环境 ...
- Qt通用方法及类库11
函数名 //判断IP地址及端口是否在线 static bool ipLive(const QString &ip, int port, int timeout = 1000); //获取网页所 ...
- Python中的包、模块和源码的组织关系
- golang两个协程交替打印出1-100
基于channel实现的,两个协程交替打印出1-100 package main import ( "fmt" "sync" ) var ( toOdd = m ...
- kubernetes系列(九) - 深入掌握Service
1. Service概念 2. Service的类型 2.1 ClusterIP(默认) 2.1.1 原理 2.1.2 ClusterIP资源清单 2.2 NodePort 2.2.1 NodePor ...
- DVWA靶场File Upload(文件上传) 漏洞所有级别通关教程及源码审计
文件上传 文件上传漏洞是由于对上传文件的内.类型没有做严格的过滤.检查,使得攻击者可以通过上传木马文件获取服务器的webshell文件 low 上传一个php文件,上传成功,并且可以在WWW\DVWA ...
- 基于 Admission Webhook 实现 Pod DNSConfig 自动注入
本文主要分享如何使用 基于 Admission Webhook 实现自动修改 Pod DNSConfig,使其优先使用 NodeLocalDNS . 1.背景 上一篇部署好 NodeLocal DNS ...
- Hadoop 概述(二)
shell定时上传linux日志信息到hdfs 从标题可以分析出来,我们要使用到shell,还要推送日志信息到hdfs上. 定义出上传的路径和临时路径,并配置好上传的log日志信息.这里我使用了上一节 ...
- G1原理—10.如何优化G1中的FGC
大纲 1.G1的FGC可以优化的点 2.一个bug导致的FGC(Kafka发送重试 + subList导致List越来越大) 3.为什么G1的FGC比ParNew + CMS要更严重 4.FGC的一些 ...
- 00.嵌入式笔记——初识make工具和Makefile文件
1. make工具和Makefile文件 make是解决大工程编译的工具,描述哪些文件需要编译.哪些需要重新编译的文件就叫做 Makefile,Makefile 就跟脚本文件一样,Makefile 里 ...