概述
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MakePDF
{
public enum TextAlign
{
单元格文字对齐方式
//const int horizontalAlignment = Element.ALIGN_CENTER;
//const int verticalAlignment = Element.ALIGN_MIDDLE;
//const int bottomAlignment = Element.ALIGN_BOTTOM;
//const int leftHAlignment = Element.ALIGN_LEFT;
//const int rightHAlignment = Element.ALIGN_RIGHT;
/// <summary>
/// 水平居中
/// </summary>
HA = Element.ALIGN_CENTER,
/// <summary>
/// 垂直居中
/// </summary>
VA = Element.ALIGN_MIDDLE,
/// <summary>
/// 下对齐
/// </summary>
BA = Element.ALIGN_BOTTOM,
/// <summary>
/// 左对齐
/// </summary>
LA = Element.ALIGN_LEFT,
/// <summary>
/// 右对齐
/// </summary>
RA = Element.ALIGN_RIGHT
}
public class MPDF
{
public Document doc = null;
MemoryStream pdfMS = null;
//不要私自改这个变量的值
public float top = 0;
public float maxTop = 0;
string path = null;
int pageNum = 1;
public iTextSharp.text.Rectangle size = null;
/// <summary>
///
/// </summary>
/// <param name="size">例如:PageSize.A4</param>
/// <param name="size">pdf文件保存路径</param>
public MPDF(iTextSharp.text.Rectangle size, string path)
{
this.path = path;
this.size = size;
doc = new Document(size);//定义一个Document,并设置页面大小为A4,横向
pdfMS = new MemoryStream();
PdfWriter.GetInstance(doc, pdfMS);//PDF内容放入到流中
doc.Open();
maxTop = size.Height - doc.BottomMargin - doc.TopMargin;
}
/// <summary>
/// 只是给MPDF_Table调用的,不需额外调用
/// </summary>
public void Add(PdfPTable ptable)
{
doc.Add(ptable);
top += ptable.TotalHeight;
pageNum = (int)(top / maxTop);
float more = top - maxTop * pageNum;
if (more > 0)
pageNum++;
}
public string save()
{
try
{
doc.NewPage();//分页
doc.Close();
byte[] fileByte = pdfMS.GetBuffer();
FileStream fs = new FileStream(path, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(fileByte);
bw.Close();
fs.Close();
return null;
}
catch (Exception e)
{
return e.ToString();
}
}
}
public class MPDF_Table
{
//字体集合
BaseFont[] bfs = new BaseFont[] {
BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED),//宋体
BaseFont.CreateFont("C://WINDOWS//Fonts//simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED)//黑体
};
iTextSharp.text.Font font = null;
PdfPTable pdfTableInfo = null;
MPDF pdf = null;
int fontIndex = 0;
int fontSize = 0;
float cellHeight = 0;
int hasUseCellNum = 0;
public int maxCellNum = 0;//每行必须填满,否则不会显示
/// <summary>
/// 表格,注意:当添加一个表格到pdf后,下一个添加的表格的初始y值会在上一个表格的y和高度和的基础上叠加
/// </summary>
/// <param name="cellWidths">表格的列宽集合</param>
/// <param name="pdf">该表格将写入的pdf文件</param>
/// <param name="fontSize">表格默认字体大小</param>
/// <param name="cellHeight">表格默认行高</param>
/// <param name="fontIndex">表格默认字体,即文件C:/WINDOWS/Fonts/下的字体文件名</param>
/// <param name="align">表格默对齐方式,水平居中,只能调整水平方向</param>
///
public MPDF_Table(float[] cellWidths, MPDF pdf, int fontSize, float cellHeight, int fontIndex = 0, TextAlign align = TextAlign.HA)
{
this.fontSize = fontSize;
maxCellNum = cellWidths.Length;
this.cellHeight = cellHeight;
font = new iTextSharp.text.Font(bfs[fontIndex], fontSize);
this.fontIndex = fontIndex;
this.pdf = pdf;
pdfTableInfo = new PdfPTable(cellWidths.Length);
pdfTableInfo.SetWidthPercentage(cellWidths, pdf.size);
pdfTableInfo.NormalizeHeadersFooters();
pdfTableInfo.SplitLate = false;
pdfTableInfo.HorizontalAlignment = (int)align;
//pdfTableInfo.TotalWidth = 605;
}
/// <summary>
/// 添加单元格,规则:叠加式,第一行堆满自动换行到第二行,特殊情况:例如,当某行只剩一个单元格的时候,你添加一个占两个单元格位置的单元格,那么它实际占用的只有一个单元格。
/// </summary>
/// <param name="txt">单元格内容</param>
/// <param name="Colspan">单元格占据多少个格子</param>
/// <param name="fontIndex">字体设置,-1代表使用表格所设字体</param>
/// <param name="fontSize">字体设置,-1代表使用表格所设字体大小</param>
/// <param name="BorderWidthTop">单元格边框线条宽度,默认0,即无线条</param>
/// <param name="BorderWidthLeft">单元格边框线条宽度,默认0,即无线条</param>
/// <param name="BorderWidthRight">单元格边框线条宽度,默认0,即无线条</param>
/// <param name="BorderWidthBottom">单元格边框线条宽度,默认0,即无线条</param>
/// <param name="cellHeight">单元格高度默认0,即表格初始化所用高度</param>
/// <param name="HorizontalAlignment">单元格文本水平对齐方式</param>
/// <param name="VerticalAlignment">单元格文本垂直对齐方式</param>
public void AddTextCell(string txt,
int Colspan = 1,
int fontIndex = -1,
int fontSize = -1,
int BorderWidthTop = 0,
int BorderWidthLeft = 0,
int BorderWidthRight = 0,
int BorderWidthBottom = 0,
float cellHeight = 0,
TextAlign HorizontalAlignment = TextAlign.LA,
TextAlign VerticalAlignment = TextAlign.HA)
{
iTextSharp.text.Font font = null;
if (fontSize == -1)
fontSize = this.fontSize;
if (fontIndex == -1)
font = this.font;
else
font = new iTextSharp.text.Font(bfs[fontIndex], fontSize);
if (cellHeight == 0)
cellHeight = this.cellHeight;
PdfPCell pdfPInfoCell = new PdfPCell(new Phrase(txt, font));
pdfPInfoCell.MinimumHeight = cellHeight;
pdfPInfoCell.BorderWidthTop = BorderWidthTop;
pdfPInfoCell.BorderWidthLeft = BorderWidthLeft;
pdfPInfoCell.BorderWidthRight = BorderWidthRight;
pdfPInfoCell.BorderWidthBottom = BorderWidthBottom;
pdfPInfoCell.Colspan = Colspan;
pdfPInfoCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPInfoCell.HorizontalAlignment = (int)HorizontalAlignment;
pdfPInfoCell.VerticalAlignment = (int)VerticalAlignment;
pdfTableInfo.AddCell(pdfPInfoCell);
hasUseCellNum += Colspan;
if (hasUseCellNum >= maxCellNum)
hasUseCellNum = 0;
}
/// <summary>
/// 将一张图片填在单元格
/// </summary>
/// <param name="ImagePath">图片路径</param>
/// <param name="Colspan">单元格占用空间,默认占用一个基础格子</param>
/// <param name="Border">边框线条宽度,默认0,无边框</param>
/// <param name="HorizontalAlignment">单元格内容水平对齐方式</param>
/// <param name="VerticalAlignment">单元格内容垂直对齐方式</param>
public void AddImageCell(string ImagePath,
int Colspan = 1,
int Border = 0,
TextAlign HorizontalAlignment = TextAlign.LA,
TextAlign VerticalAlignment = TextAlign.HA)
{
PdfPCell pdfPInfoCell = new PdfPCell();
pdfPInfoCell.Border = Border;
pdfPInfoCell.Colspan = Colspan;
pdfPInfoCell.HorizontalAlignment = (int)HorizontalAlignment;
pdfPInfoCell.VerticalAlignment = (int)VerticalAlignment;
pdfPInfoCell.PaddingLeft = 10;
//System.IO.Directory.GetCurrentDirectory() + @"title2.png"
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ImagePath);
pdfPInfoCell.Image = image; //图片
pdfTableInfo.AddCell(pdfPInfoCell);
hasUseCellNum += Colspan;
if (hasUseCellNum >= maxCellNum)
hasUseCellNum = 0;
}
/// <summary>
/// 添加一个没有边框的占据表格的一行的单元格
/// </summary>
/// <param name="height"></param>
public void AddEmptyLine(float height)
{
AddTextCell("", maxCellNum, -1, -1, 0, 0, 0, 0, height);
}
public void AddEmptyLine()
{
AddTextCell("", maxCellNum, -1, -1, 0, 0, 0, 0, cellHeight);
}
//填满最后一行
public void FullEndLine()
{
if (hasUseCellNum < maxCellNum && hasUseCellNum != 0)
{
AddTextCell("", maxCellNum - hasUseCellNum);
hasUseCellNum = 0;
}
}
public void AddTableToPDF()
{
//补齐
FullEndLine();
pdf.Add(pdfTableInfo);
}
/// <summary>
/// 把表格添加到末尾,执行了这一个方法后,这一页就不能再添加内容了!后来添加的将放到下一页,当然,如果末尾装不下,会换到下一页的末尾,这个有点坑
/// </summary>
public void AddTableToPDF_InEnd()
{
int pageNum = (int)(pdf.top / pdf.maxTop);
float more = pdf.top - pdf.maxTop * pageNum;
more = pdf.maxTop - more;
if (pdfTableInfo.TotalHeight > more)
{
MPDF_Table table = new MPDF_Table(new float[] { 10, 10 }, pdf, 1, more);
table.AddEmptyLine();
table.AddTableToPDF();
}
else
{
MPDF_Table table = new MPDF_Table(new float[] { 10, 10 }, pdf, 1, more - pdfTableInfo.TotalHeight);
table.AddEmptyLine();
table.AddTableToPDF();
}
AddTableToPDF();
}
}
//测试例子
public class MPDF_TEST
{
/// <summary>
/// 测试例子
/// </summary>
/// <param name="path">必须是完整的路径,并且要保证路径所在文件夹存在</param>
public static void GetYangWFZ_PDF(string path)
{
MPDF pdf = new MPDF(PageSize.A4, path);
MPDF_Table table = new MPDF_Table(new float[] { 50, 50, 81, 81, 172, 15, 157 }, pdf, 10, 20, 1);
//==============logo==================
string imagePath = System.IO.Directory.GetCurrentDirectory() + @"title2.png";//这时一个图片的路径
table.AddTextCell("");
table.AddImageCell(imagePath, 4);
table.AddTextCell("");
table.AddTextCell("XXXXXX区长征路24号n0000-000000", 1, 0, -1, 0, 0, 0, 0, 0, TextAlign.HA, TextAlign.BA);
table.AddEmptyLine(5);
//================基本信息===========================
table.AddTextCell("体液检验 MZFUS200", 3);
table.AddTextCell("", 2);
table.AddTextCell("标本编号:20192232322", 2);
table.AddTextCell("姓名:XXXXX", 2);
table.AddTextCell("病人类别:XXXX", 2);
table.AddTextCell("病员编号:502936");
table.AddTextCell("标本种类:XXXX", 2);
table.AddTextCell("性别:男", 2);
table.AddTextCell("病区科室:XXXXX", 2);
table.AddTextCell("临床诊断:");
table.AddTextCell("病员床号:1111123", 2);
table.AddTextCell("年龄:20", 2, -1, -1, 0, 0, 0, 1);
table.AddTextCell("申请时间:2019/9/12 20:34", 2, -1, -1, 0, 0, 0, 1);
table.AddTextCell("送检时间:2019/9/12 20:35", 1, -1, -1, 0, 0, 0, 1);
table.AddTextCell("采样时间:2019/9/12 20:34", 2, -1, -1, 0, 0, 0, 1);
//==================培养结果==========================
table.AddTextCell("培养结果:", 4, 0);
table.AddTextCell(" 菌落计数:", 3, 0);
table.AddTextCell(" " + "支原体", 4, 1);//培养结果
table.AddTextCell(" " + ">10万cfu/ml", 3, 0);
//==================药敏试验==========================
table.AddEmptyLine();
table.AddEmptyLine();
table.AddTextCell(" 药敏试验:", 4, 0);
table.FullEndLine();
table.AddTableToPDF();
table = new MPDF_Table(new float[] { 101, 101, 90, 11, 11, 90, 101, 101 }, pdf, 10, 13, 0);
table.AddTextCell("支原体", table.maxCellNum, 1, -1, 1, 0, 0, 0, 20, TextAlign.LA, TextAlign.BA);
table.AddTextCell("", 8, -1, -1, 0, 0, 0, 0, 5);
table.AddTextCell("", 4, -1, -1, 0, 0, 1, 1, 5);
table.AddTextCell("", 4, -1, -1, 0, 0, 0, 1, 5);
table.AddTextCell("药物名称", 1, -1, -1, 0, 0, 0, 1, 22);
table.AddTextCell("DISK(mm)", 1, -1, -1, 0, 0, 0, 1, 22, TextAlign.HA);
table.AddTextCell("敏感度", 1, -1, -1, 0, 0, 0, 1, 22, TextAlign.RA);
table.AddTextCell("", 1, -1, -1, 0, 0, 1, 1, 22, TextAlign.RA);
table.AddTextCell("", 1, -1, -1, 0, 0, 0, 1, 22, TextAlign.RA);
table.AddTextCell("药物名称", 1, -1, -1, 0, 0, 0, 1, 22);
table.AddTextCell("DISK(mm)", 1, -1, -1, 0, 0, 0, 1, 22, TextAlign.HA);
table.AddTextCell("敏感度 ", 1, -1, -1, 0, 0, 0, 1, 22, TextAlign.RA);
//模拟内容====
table.AddTextCell("头孢");
table.AddTextCell("");
table.AddTextCell("耐药", 1, -1, -1, 0, 0, 0, 0, 0, TextAlign.RA);
table.AddTextCell("", 1, -1, -1, 0, 0, 1);
table.AddTextCell("");
table.AddTextCell("头孢");
table.AddTextCell("");
table.AddTextCell("耐药", 1, -1, -1, 0, 0, 0, 0, 0, TextAlign.RA);
table.AddTextCell("头孢");
table.AddTextCell("");
table.AddTextCell("耐药", 1, -1, -1, 0, 0, 0, 0, 0, TextAlign.RA);
table.AddTextCell("", 1, -1, -1, 0, 0, 1);
table.AddTextCell("");
table.AddTextCell("头孢");
table.AddTextCell("");
table.AddTextCell("耐药", 1, -1, -1, 0, 0, 0, 0, 0, TextAlign.RA);
table.AddTextCell("头孢");
table.AddTextCell("");
table.AddTextCell("耐药", 1, -1, -1, 0, 0, 0, 0, 0, TextAlign.RA);
table.AddTextCell("", 1, -1, -1, 0, 0, 1);
table.AddTextCell("");
table.AddTextCell("头孢");
table.AddTextCell("");
table.AddTextCell("耐药", 1, -1, -1, 0, 0, 0, 0, 0, TextAlign.RA);
//========
table.AddTextCell("", 4, -1, -1, 1, 0, 1);
table.AddTextCell("", 4, -1, -1, 1);
table.AddTableToPDF();
MPDF_Table table2 = new MPDF_Table(new float[] { 100, 170, 170, 160 }, pdf, 9, 14);
table2.AddTextCell("备注:", 4, -1, -1, 1);
table2.AddTextCell("送检医生:XXXX", 1, -1, -1, 1);
table2.AddTextCell("报告时间:2019/9/12 20:36", 1, -1, -1, 1);
table2.AddTextCell("检验:XXXX", 1, -1, -1, 1);
table2.AddTextCell("审核:系统管理员", 1, -1, -1, 1);
table2.AddEmptyLine();
table2.AddTextCell("注:本次实验报告仅对本次标本负责,如有疑问,请于24小时内提出咨询", 3);
table2.AddTextCell("第1页/共1页", 1, -1, -1, 0, 0, 0, 0, 0, TextAlign.RA);
table2.AddTableToPDF_InEnd();
pdf.save();
}
}
}
最后
以上就是纯真学姐为你收集整理的C# 生成PDF文件(itextsharp二次封装)的全部内容,希望文章能够帮你解决C# 生成PDF文件(itextsharp二次封装)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复