首页 > 编程语言 >C#生成PDF格式的合同文件

C#生成PDF格式的合同文件

时间:2023-07-29 19:44:49浏览次数:38  
标签:PdfPTable 表格 C# 格式 new cell Add table PDF

生成PDF格式的合同文件,效果图如下:

 

一、准备工作

首先C#代码操作pdf文件,需要引用一个pdf官方提供的两个dll,去网上下载,并将其添加引用到项目即可。官方下载地址 ,提取码:0jue

在代码中引用

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

二、创建PDF文件

private static void Main() {
    // 创建PDF文件
    FileStream pdfFs = new FileStream("C:\\MyText.pdf", FileMode.Create);//C:\\MyText.pdf为保存路径
    
    // 获取实例
    Document doc = new Document();
    PdfWriter.GetInstance(doc, pdfFs);
    
    // 打开pdf
    doc.Open();
    
    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("C:\\logo.jpg");//C:\\logo.jpg为logo路径
    image.ScaleAbsolute(50, 50);
    image.SetAbsolutePosition(50, PageSize.A4.Height-50);
    doc.Add(image);
    
    Font font = SetFontSizeColor(20, BaseColor.BLACK);
    Paragraph tit = new Paragraph("深圳市双睿鑫科技有限公司", font);
    tit.Alignment = 1;//居中
    doc.Add(tit);
    
    // 合并多个表格
    List<PdfPTable> allTable = new List<PdfPTable>();
    allTable.Add(SetTitlePDf());
    PdfPTable table = MergeMult(allTable);
    doc.Add(table);//添加表格

    font = SetFontSizeColor(14, BaseColor.BLACK);
    tit = new Paragraph("订单详情", font);
    tit.IndentationLeft = 20;//左边距
    doc.Add(tit);

    // 合并多个表格
    allTable = new List<PdfPTable>();
    allTable.Add(SetMessPDF());
    table = MergeMultTable(allTable);
    doc.Add(table);// 添加表格

    font = SetFontSizeColor(14, BaseColor.BLACK);
    tit = new Paragraph("费用明细", font);
    tit.IndentationLeft = 20;//左边距
    doc.Add(tit);

    // 合并多个表格
    allTable = new List<PdfPTable>();
    allTable.Add(SetInfoPDF());
    table = MergeMultTable(allTable);
    doc.Add(table);//添加表格

    font = SetFontSizeColor(14, BaseColor.BLACK);
    tit = new Paragraph("合同条款", font);
    tit.IndentationLeft = 20;//左边距
    doc.Add(tit);
    
    List<string> str = new List<string>();
    str.Add("第一条合同条款。");
    str.Add("第二条合同条款。");
    str.Add("第三条合同条款。");
    str.Add("第四条合同条款。");
    str.Add("第五条合同条款。");
    str.Add("第六条合同条款。");
    str.Add("第七条合同条款。");
    str.Add("第八条合同条款。");
    str.Add("第九条合同条款。");
    str.Add("第十条合同条款。");
    doc = contract(str, doc);
    
    //合并多个表格
    allTable = new List<PdfPTable>();
    allTable.Add(SetBottomPDf());
    table = MergeMultTable(allTable);
    doc.Add(table);//添加表格
    
    image = iTextSharp.text.Image.GetInstance("C://contract.jpg");//C:\\contract.jpg为合同章路径
    image.ScaleAbsolute(150, 150);
    image.SetAbsolutePosition(PageSize.A4.Width-300, 10);
    doc.Add(image);
    
    //关闭
    doc.Close();
    pdfFs.Close();
}

三、写入内容

/// <summary>
/// 合同条款
/// </summary>
/// <param name="contractList">合同条款</param>
/// <param name="doc">pdf内容</param>
/// <returns></returns>
private static Document contract(List<string> contractList, Document doc)
{
    Font font;
    Paragraph tit;
    for (int i = 0; i < contractList.Count; i++)
    {
        font = SetFont(10, BaseColor.BLACK);
        tit = new Paragraph((i + 1) + "、" + contractList[i], font);
        tit.IndentationLeft = 20;//左边距
        tit.IndentationRight = 20;//右边距
        tit.FirstLineIndent = 20;
        doc.Add(tit);
    }
    return doc;
}
/// <summary>
/// 创建一行N列
/// </summary>
/// <param name="columnNameList">列的元素</param>
/// <param name="fontSize">字号大小</param>
/// <returns></returns>
private static PdfPTable CreateOneRow(List<string> columnNameList, int fontSize)
{
    // 定义表格对象,参数是该表格的列数
    PdfPTable table = new PdfPTable(columnNameList.Count);
    // 实现自动换页
    table.SplitLate = false;
    table.SplitRows = true;

    // 获取字号
    Font font = SetFontSizeColor(fontSize, BaseColor.BLACK);
    // 定义单元格对象
    PdfPCell cell;

    int rowCount = columnNameList.Count;
    for (int i = 0; i < rowCount; i++)
    {
        if (columnNameList[i] != null)
        {
            cell = new PdfPCell(new Phrase(columnNameList[i], font));   // 将内容绑定到单元格中
            cell.PaddingTop = 20;
            cell.PaddingBottom = 30;
            cell.HorizontalAlignment = 1;   // 居中
            cell.DisableBorderSide(15);
            table.AddCell(cell);
        }
    }

    return table;
}
/// <summary>
/// 创建一行N列
/// </summary>
/// <param name="columnNameList">列的元素</param>
/// <param name="fontSize">字号大小</param>
/// <returns></returns>
private static PdfPTable CreateBottomRow(List<string> columnNameList, int fontSize)
{
    // 定义表格对象,参数是该表格的列数
    PdfPTable table = new PdfPTable(columnNameList.Count);
    // 实现自动换页
    table.SplitLate = false;
    table.SplitRows = true;

    // 获取字号
    Font font = SetFontSizeColor(fontSize, BaseColor.BLACK);
    // 定义单元格对象
    PdfPCell cell;

    int rowCount = columnNameList.Count;
    for (int i = 0; i < rowCount; i++)
    {
        if (columnNameList[i] != null)
        {
            cell = new PdfPCell(new Phrase(columnNameList[i], font));   // 将内容绑定到单元格中
            cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;   // 右对齐
            cell.DisableBorderSide(15);
            table.AddCell(cell);
        }
    }

    return table;
}
/// <summary>
/// 创建一行N列表格
/// </summary>
/// <param name="columnNameList">列的元素</param>
/// <param name="fontSize">字号大小</param>
/// <returns></returns>
private static PdfPTable CreateOneRowTable(List<string> columnNameList, int fontSize)
{
    // 定义表格对象,参数是该表格的列数
    PdfPTable table = new PdfPTable(columnNameList.Count);
    // 实现自动换页
    table.SplitLate = false;
    table.SplitRows = true;

    // 获取字号
    Font font = SetFontSizeColor(fontSize, BaseColor.BLACK);
    // 定义单元格对象
    PdfPCell cell;

    int rowCount = columnNameList.Count;
    for (int i = 0; i < rowCount; i++)
    {
        if (columnNameList[i] != null)
        {
            cell = new PdfPCell(new Phrase(columnNameList[i], font));   // 将内容绑定到单元格中
            cell.HorizontalAlignment = 1;   // 居中
            table.AddCell(cell);
        }
    }

    return table;
}
/// <summary>
/// 设置字号与字体颜色
/// </summary>
/// <returns></returns>
private static Font SetFontSizeColor(int size, BaseColor color)
{

    BaseFont bfchinese = BaseFont.CreateFont(_WebConfig.GetAppSettingsString("ttf_url"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//simkai.ttf
    Font ChFont = new iTextSharp.text.Font(bfchinese, size, Font.NORMAL, color);//Font.BOLD:加粗
    return ChFont;
}

/// <summary>
/// 设置字号与字体颜色
/// </summary>
/// <returns></returns>
private static Font SetFont(int size, BaseColor color)
{

    BaseFont bfchinese = BaseFont.CreateFont(_WebConfig.GetAppSettingsString("ttf_url"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//simkai.ttf
    Font ChFont = new iTextSharp.text.Font(bfchinese, size, Font.NORMAL, color);
    return ChFont;
}

// 标题部分表格
public static PdfPTable SetTitlePDf()
{
    // 定义一个字符串链表,用于存储填入表格中的数据
    List<string> text = new List<string>();

    text.Add("商品销售合同");
    PdfPTable tableMain = CreateOneRow(text, 14);  // 创建一张仅有一行一列的表格

    text = new List<string>();
    text.Add("需求方(甲方):");
    text.Add("供应商(乙方):深圳市双睿鑫科技有限公司");
    text.Add("联系人:");
    text.Add("客服人员:张先生");
    text.Add("收货地址:");
    text.Add("地址:广东省深圳市龙华区");
    PdfPTable tableMess = CreateMultRowMultColumn(2, text);    // 根据数据创建多行多列表格
    // 将两张表合并成一张表
    PdfPTable table = MergeTwo(tableMain, tableMess);

    return table;

}
// 标题部分表格
public static PdfPTable SetBottomPDf()
{
    // 定义一个字符串链表,用于存储填入表格中的数据
    List<string> text = new List<string>();

    text.Add("合同生成日期:" + DateTime.Now.ToLongDateString().ToString());
    PdfPTable tableMain = CreateBottomRow(text, 10);  // 创建一张仅有一行一列的表格

    text = new List<string>();
    text.Add("甲方代表签字:");
    text.Add("乙方代表签字:张先生");
    text.Add("盖章:");
    text.Add("盖章:");
    PdfPTable tableMess = CreateMultRowMultColumn(2, text);    // 根据数据创建多行多列表格
    // 将两张表合并成一张表
    PdfPTable table = MergeTwo(tableMain, tableMess);

    return table;

}
/// <summary>
/// 合并两个表格
/// </summary>
/// <param name="table1"></param>
/// <param name="table2"></param>
/// <returns></returns>
public static PdfPTable MergeTwoTable(PdfPTable table1, PdfPTable table2)
{
    // 定义表格对象,参数代表是该表格一共由多少列
    PdfPTable table = new PdfPTable(1);
    // 定义单元格对象
    PdfPCell cell;

    // 实现自动换页
    table.SplitLate = false;
    table.SplitRows = true;

    // 这个函数写死是合并两个单元格
    int rowCount = 2;
    for (int i = 0; i < rowCount; i++)
    {
        switch (i)
        {
            case 0:
                // 单元格中添加表格1
                cell = new PdfPCell(table1);
                // 居中
                cell.Padding = 0;
                cell.DisableBorderSide(15);
                // 将单元格添加到表格中
                table.AddCell(cell);
                break;
            case 1:
                cell = new PdfPCell(table2);
                cell.Padding = 0;
                cell.DisableBorderSide(15);
                table.AddCell(cell);
                break;
            default:
                break;
        }
    }

    // 最后将合并好的表格返回
    return table;
}

/// <summary>
/// 合并两个表格
/// </summary>
/// <param name="table1"></param>
/// <param name="table2"></param>
/// <returns></returns>
public static PdfPTable MergeTwo(PdfPTable table1, PdfPTable table2)
{
    // 定义表格对象,参数代表是该表格一共由多少列
    PdfPTable table = new PdfPTable(1);
    // 定义单元格对象
    PdfPCell cell;

    // 实现自动换页
    table.SplitLate = false;
    table.SplitRows = true;

    // 这个函数写死是合并两个单元格
    int rowCount = 2;
    for (int i = 0; i < rowCount; i++)
    {
        switch (i)
        {
            case 0:
                // 单元格中添加表格1
                cell = new PdfPCell(table1);
                // 居中
                cell.Padding = 0;
                cell.DisableBorderSide(15);
                // 将单元格添加到表格中
                table.AddCell(cell);
                break;
            case 1:
                cell = new PdfPCell(table2);
                cell.Padding = 0;
                cell.DisableBorderSide(15);
                table.AddCell(cell);
                break;
            default:
                break;
        }
    }

    // 最后将合并好的表格返回
    return table;
}

// 订单详情部分表格
public static PdfPTable SetMessPDF()
{
    List<string> checkNameList = new List<string>();

    checkNameList.Add("商品");
    checkNameList.Add("单价");
    checkNameList.Add("数量");
    checkNameList.Add("总价");
    checkNameList.Add("周期");
    checkNameList.Add("发票");
    PdfPTable tableTitle = CreateOneRowTable(checkNameList, 12);    // 生成一张表格

    checkNameList = new List<string>();
    checkNameList.Add("iPhone 13");
    checkNameList.Add("¥5399");
    checkNameList.Add("100部");
    checkNameList.Add("¥53990");
    checkNameList.Add("5天");
    checkNameList.Add("增值税发票");

    PdfPTable tableMess = CreateMultRowMultColumnTable(6, checkNameList);       // 创建多行多列表格
    PdfPTable tableOverall = MergeTwoTable(tableTitle, tableMess);  // 两张表格上下合并

    //PdfPTable table = CreateRowTitleRowDataTable("订单详情", tableOverall, 5);  // 合并左边带标题信息的表格

    return tableOverall;
}
/// <summary>
/// 创建带行标题和一组数据的表格
/// </summary>
/// <param name="columnTitleName">行标题</param>
/// <param name="messPDFTable">数据表格</param>
/// <param name="columnCount">数据表的比例</param>
/// <returns></returns>
private static PdfPTable CreateRowTitleRowDataTable(string columnTitleName, PdfPTable messPDFTable, int columnCount)
{
    // 创建两列的表格
    PdfPTable table = new PdfPTable(2);
    // 设置列宽比例
    table.SetWidths(new int[] { 1, columnCount });  // 猜测:1比columnCount的比例
                                                    // 设置自动换页
    table.SplitLate = false;
    table.SplitRows = true;
    // 设置字号字体颜色
    Font font = SetFontSizeColor(15, BaseColor.BLUE);

    PdfPCell cell;
    int rowCount = 2;

    for (int i = 0; i < rowCount; i++)
    {
        switch (i)
        {
            case 0:
                cell = new PdfPCell(new Phrase(columnTitleName, font));     // 新建一个单元格,数据作为参数
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);        // 单元格添加到表格中
                break;
            case 1:
                cell = new PdfPCell(messPDFTable);  // 新建一个单元格,表格作为参数
                cell.Padding = 0;
                table.AddCell(cell);    // 单元格添加到表格中
                break;
            default:
                break;
        }
    }

    return table;
}
/// <summary>
/// 创建多行多列
/// </summary>
/// <param name="column">列数</param>
/// <param name="columnDataNameList">数据</param>
/// <returns></returns>
private static PdfPTable CreateMultRowMultColumn(int column, List<string> columnDataNameList)
{
    // 创建多列表格
    PdfPTable table = new PdfPTable(column);
    // 设置字号与字体颜色
    Font font = SetFontSizeColor(12, BaseColor.BLACK);

    // 设置自动换页
    table.SplitLate = false;
    table.SplitRows = true;

    // 定义单元格
    PdfPCell cell;

    // 这里的意思是:
    // 将链表中的数据一个一个的设置到单元格中,然后单元格添加到表格中
    // 这里需要注意,因为我们定义表格对象是设置了列数,所以设置单元格达到表的列数后,再次往表中添加单元格会自动换行添加!
    int rowColumn = columnDataNameList.Count;
    for (int i = 0; i < rowColumn; i++)
    {
        // 实例化单元格对象,数据与字体设置到单元格中
        cell = new PdfPCell(new Phrase(columnDataNameList[i], font));
        cell.PaddingTop = 5;
        cell.PaddingBottom = 5;
        cell.DisableBorderSide(15);
        // 将单元格添加到表格中
        table.AddCell(cell);
    }

    return table;
}

/// <summary>
/// 创建多行多列表格
/// </summary>
/// <param name="column">表格列数</param>
/// <param name="columnDataNameList">表格中的数据</param>
/// <returns></returns>
private static PdfPTable CreateMultRowMultColumnTable(int column, List<string> columnDataNameList)
{
    // 创建多列表格
    PdfPTable table = new PdfPTable(column);
    // 设置字号与字体颜色
    Font font = SetFontSizeColor(12, BaseColor.RED);

    // 设置自动换页
    table.SplitLate = false;
    table.SplitRows = true;

    // 定义单元格
    PdfPCell cell;

    // 这里的意思是:
    // 将链表中的数据一个一个的设置到单元格中,然后单元格添加到表格中
    // 这里需要注意,因为我们定义表格对象是设置了列数,所以设置单元格达到表的列数后,再次往表中添加单元格会自动换行添加!
    int rowColumn = columnDataNameList.Count;
    for (int i = 0; i < rowColumn; i++)
    {
        // 实例化单元格对象,数据与字体设置到单元格中
        cell = new PdfPCell(new Phrase(columnDataNameList[i], font));
        cell.HorizontalAlignment = 1;
        // 将单元格添加到表格中
        table.AddCell(cell);
    }

    return table;
}

// 费用明细部分表格
public static PdfPTable SetInfoPDF()
{
    List<string> checkNameList = new List<string>();

    checkNameList.Add("订单编号");
    checkNameList.Add("商品金额");
    checkNameList.Add("税费");
    checkNameList.Add("总金额");
    PdfPTable tableTitle = CreateOneRowTable(checkNameList, 12);    // 生成一张标题表格

    checkNameList = new List<string>();
    checkNameList.Add("20220512001");
    checkNameList.Add("¥53990");
    checkNameList.Add("¥7018.7");
    checkNameList.Add("¥61008.7");


    PdfPTable tableMess = CreateMultRowMultColumnTable(4, checkNameList);    // 根据数据创建多行多列表格
    PdfPTable tableOverall = MergeTwoTable(tableTitle, tableMess);              // 将两张表合并成一张表

    // 新建一个表格,一行两列,第一列写入参数一数据,第二列放入参数二表格
    //PdfPTable table = CreateRowTitleRowDataTable("费用明细", tableOverall, 5);

    return tableOverall;
}
/// <summary>
/// 合并多个表格
/// </summary>
/// <param name="pdfList"></param>
/// <returns></returns>
public static PdfPTable MergeMultTable(List<PdfPTable> pdfList)
{
    PdfPTable table = new PdfPTable(1);
    PdfPCell cell;

    table.SplitLate = false;
    table.SplitRows = true;

    // 将表格绑定到单元格中,然后将单元格插入汇总表格
    foreach (PdfPTable pdf in pdfList)
    {
        cell = new PdfPCell(pdf);
        cell.Padding = 0;
        cell.PaddingTop = 10;
        cell.DisableBorderSide(15);//隐藏Table边框
        table.AddCell(cell);
    }

    return table;
}
/// <summary>
/// 合并多行
/// </summary>
/// <param name="pdfList"></param>
/// <returns></returns>
public static PdfPTable MergeMult(List<PdfPTable> pdfList)
{
    PdfPTable table = new PdfPTable(1);
    PdfPCell cell;

    table.SplitLate = false;
    table.SplitRows = true;

    // 将表格绑定到单元格中,然后将单元格插入汇总表格
    foreach (PdfPTable pdf in pdfList)
    {
        cell = new PdfPCell(pdf);
        cell.PaddingLeft = -30;
        cell.PaddingRight = -30;
        cell.DisableBorderSide(15);//隐藏Table边框
        table.AddCell(cell);
    }

    return table;
}

四、总结

C#生成PDF文件无非就是表格类PdfPTable与单元格类PdfPCell互相配合使用而已!根据自己的需求封装函数创建表格返回就行。

标签:PdfPTable,表格,C#,格式,new,cell,Add,table,PDF
From: https://www.cnblogs.com/lgx5/p/17590362.html

相关文章

  • Autofac
    varasms=AppDomain.CurrentDomain.GetAssemblies().Where(x=>!x.GlobalAssemblyCache).ToArray();containerBuilder.RegisterAssemblyTypes(asms).Where(x=>typeof(IBaseView).IsAssignableFrom(x)).PublicOnly().AsSelf();containerBuilder.RegisterAssembl......
  • P9387 [THUPC 2023 决赛] 巧克力 题解
    这篇题解会只讲怎么dp,所以我们这里跳过博弈论的部分。Let'srephrasetheproblemstatementasfollows:给定\(n,m\),设\(x=1\oplus2\oplus\cdots\oplusn\oplusm\)。求有多少个有序三元组\((a,b,c)\)满足:\(a+b+c\len\)或\(a+b+c=m\)(如果都满足需要算两遍)。\((a+b......
  • pycharm运行代码自动跳转到Python控制台窗口
    问题:在使用Pycharm的过程中,会遇到运行代码Pycharm会自动跳转到Python控制台(pythonconsole)窗口,如下图解决方案:​ 造成上述问题的原因是:不经意的设置了使用Pytho控制台运行,我们只需要修改以下运行配置就可以恢复正常,如下图(1)进入编辑配置:(2)取消勾选使用Python控制台运行......
  • solidworks api ch05
    打开一个现存的文件,并选择一个表面,执行下列代码:lettestSelectFace(swApp:ISldWorks)=letswModel=swApp.ActiveDoc|>unbox<ModelDoc2>letswSelMgr=swModel.SelectionManager:?>SelectionMgr//Dosomevalidationbeforerunning......
  • 【VS Code 与 Qt6】QAction 类的一些事
    QAction类表示用户命令的一种抽象,包括命令文本、图标、命令触发后要执行的代码。菜单、工具栏按钮往往存在相同的功能,将这些命令独立抽出来,放到QAction以象上,可避免编写重复的代码。比如“文件”菜单下有“保存”命令,工具栏上也会有“保存”按钮。因此,创建一个表示“保存”的Q......
  • System.NullReferenceException:“未将对象引用设置到对象的实例。”
     c#中操作的变量都是对象对象则有属性和方法   未将对象引用  设置到  对象的实例就是对实例进行操作属性和方法时出错,也就是说该对象没有这个属性或者方法另外当操作的对象为空时,也会出现该错误。我这里就是操作的对象为空出错。因为我没用写入该字段。......
  • pikachu靶场搭建
    资源下载官方下载地址:https://github.com/zhuifengshaonianhanlu/pikachu环境搭建创建一个新的数据库,库名用户和密码自定义需要修改两个文件,第一个:,第二个在配置好后如果有这个在url搜索install.php点击安装/初始化即可......
  • 实用工具 Anaconda
    介绍Anaconda 介绍:Anaconda 是可以便捷获取包且对包能够进行管理,同时对环境可以统一管理的工具。适用Windows、macOS、Linux平台。Anaconda拥有conda包、环境管理器、1000+开源库。Anaconda包含180+的科学包以及其依赖项,如conda、numpy、scipy、ipythonnotebook......
  • Codeforces Round 888 (Div. 3)
    CodeforcesRound888(Div.3)T1​ 思路:直接模拟。T2​思路:首先记录原始数组的奇偶性,然后将奇数、偶数分为不同两组进行排序,然后再根据原数组的奇偶性按顺序填入奇数偶数,最后判断整个数组是否非递减。T3思路:我们已知开始在\(a_1\),最后在\(a_n\)。那么当\(a_1\ne......
  • 在SQL Server中新建了SQL Server authentication用户却连不上
    在SQLServer中新建了一个SQLServerauthentication用户Super,也设置了密码:但是使用SQLServerAuthentication连接的时候,用户Super怎么也连不上:折腾了半天,原来发现是SQLServer服务器的SQLServer Authenticationmode没有开启,在SSMS中选中SQLServer服务器,右键点击Properti......