Uniyt动态生成PDF文件 iTextSharp插件使用

先封装一个创建PDF的类

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using UnityEditor;
using UnityEngine;
using Font = iTextSharp.text.Font;
using Image = iTextSharp.text.Image;
using Rectangle = iTextSharp.text.Rectangle;

public class Report
{
     //参数一 文档大标题    参数二 纸张大小
    public Report(string n,Document _document)
    {
        fileName = n;
        document = _document;
        Init();
    }
    
    BaseFont heiBaseFont;//字体
    public Font titleFont;//报告字体样式
    public Font firstTitleFont;//大标题字体样式
    public Font secondTitleFont;//小标题字体样式
    public Font contentFont;//内容字体样式
    Document document;//文档
    PdfPTable table;//表格
    //public QuestionData[] questionBiBei;//思考题
    public QuestionData[] question;
    AskData[] askDatas;//问答题
    string imageName;//插入图片的名字
    int tableColumn = 4;
    string fileName;
    /// <summary>
    /// 创建报告的初始化
    /// </summary>
    public void Init()
    {
        
        //创建字体
        heiBaseFont = BaseFont.CreateFont(Application.streamingAssetsPath + "/ReportRes/SIMHEI.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        titleFont = new Font(heiBaseFont, 26, 1);
        firstTitleFont = new Font(heiBaseFont, 20, 1);
        secondTitleFont = new Font(heiBaseFont, 13, 1);
        contentFont = new Font(heiBaseFont, 11, Font.NORMAL);
        //Document:(文档)生成pdf必备的一个对象,生成一个Document示例
       
        //为该Document创建一个Writer实例: 
        FileStream os = new FileStream(Application.streamingAssetsPath + "/" + fileName + ".pdf", FileMode.Create);
        PdfWriter.GetInstance(document, os);
        StartPDF();
    }
    /// <summary>
    /// 打开文档
    /// </summary>
    void StartPDF()
    {
        document.Open();
    }
    /// <summary>
    /// 增加表格
    /// </summary>
    /// <param name="column">列数</param>
    /// <param name="content">内容</param>
    public void AddTable(int column, string[] content)
    {
        table = new PdfPTable(column);
        table.TotalWidth = 520f;

        table.LockedWidth = true;

        table.HorizontalAlignment = 1;
        for (int i = 0; i < content.Length; i++)
        {
            PdfPCell cell = new PdfPCell(new Phrase(content[i], contentFont));
           // cell.FixedHeight = 20;

            cell.PaddingBottom = 5f;
            cell.PaddingTop = 5f;
            cell.HorizontalAlignment = 1;
            cell.VerticalAlignment = 1;
            table.AddCell(cell);
        }
        document.Add(table);
    }
    /// <summary>
    /// 实训一 列表数据
    /// </summary>
    /// <param name="column"></param>
    /// <param name="content"></param>
    public void AddTableTraining(int column, string[] content )
    {
        table = new PdfPTable(column);
        table.TotalWidth = 520f;

        table.LockedWidth = true;
        
        table.HorizontalAlignment = 1;
         
        for (int i = 0; i < content.Length; i++)
        {
            
            PdfPCell cell = new PdfPCell(new Phrase(content[i], contentFont));

            if (i==0)
            {
                
                cell.Rowspan = 2;//合并行

            }
            if(i==1||i==2)
            {
                 cell.Colspan = 2;//合并列

            }
            if(i==3)
            {
                cell.Colspan = 3;//合并列
            }
            cell.PaddingBottom = 5f;
            cell.PaddingTop = 5f;
            cell.HorizontalAlignment = 1;
            cell.VerticalAlignment = 1;
            table.AddCell(cell);
        }
        document.Add(table);
    }
    /// <summary>
    /// 实训三 列表数据
    /// </summary>
    /// <param name="column"></param>
    /// <param name="content"></param>
    public void AddTableTrainingThree(int column, string[] content)
    {
        table = new PdfPTable(column);
        table.TotalWidth = 800f;

        table.LockedWidth = true;

        table.HorizontalAlignment = 1;

        for (int i = 0; i < content.Length; i++)
        {

            PdfPCell cell = new PdfPCell(new Phrase(content[i], contentFont));


            if (i == 0)
            {

                cell.Colspan = 19;//合并列

            }
            if (i == 1 || i == 2)
            {
                cell.Rowspan = 2;//合并行

            }
            if (i == 3)
            {
                cell.Colspan = 6;//合并列
            }
            if (i == 4 || i == 7)
            {
                cell.Colspan = 3;//合并列
            }
            if (i == 5)
            {
                cell.Rowspan = 2;//合并行
            }
            if (i == 6||i==8)
            {
                cell.Colspan = 2;//合并列
            }
            cell.PaddingBottom = 5f;
            cell.PaddingTop = 5f;
            cell.HorizontalAlignment = 1;
            cell.VerticalAlignment = 1;
            table.AddCell(cell);
        }
        document.Add(table);
    }
    /// <summary>
    /// 实训三 列表数据
    /// </summary>
    /// <param name="column"></param>
    /// <param name="content"></param>
    public void AddTableTrainingThree2(int column, string[] content)
    {
        table = new PdfPTable(column);
        table.TotalWidth = 800f;

        table.LockedWidth = true;

        table.HorizontalAlignment = 1;

        for (int i = 0; i < content.Length; i++)
        {

            PdfPCell cell = new PdfPCell(new Phrase(content[i], contentFont));

            if (i == 0)
            {

                cell.Colspan = 21;//合并列

            }
            if (i == 1 || i == 2)
            {
                cell.Rowspan = 2;//合并行

            }
            if (i == 3)
            {
                cell.Colspan = 2;//合并列
            }
            if (i == 4 ||i==5|| i == 7)
            {
                cell.Colspan = 3;//合并列
            }
            if (i == 6)
            {
                cell.Colspan = 2;//合并列
            }
            if ( i == 8)
            {
                 cell.Rowspan = 2;//合并行
            }
            if (i == 9)
            {
                cell.Colspan = 5;//合并列
            }
            cell.PaddingBottom = 5f;
            cell.PaddingTop = 5f;
            cell.HorizontalAlignment = 1;
            cell.VerticalAlignment = 1;
            table.AddCell(cell);
        }
        document.Add(table);
    }
    public void AddTableTraining1(int column, string[] content)
    {
        table = new PdfPTable(column);
        table.TotalWidth = 520;
        table.LockedWidth = true;
        
        table.HorizontalAlignment = 1;
        
        for (int i = 0; i < content.Length; i++)
        {

            PdfPCell cell = new PdfPCell(new Phrase(content[i], contentFont));
           
            if (i == 0)
            {

                cell.Rowspan = 2;//合并行

            }
            if (i == 1 || i == 2)
            {
                cell.Colspan = 2;//合并列

            }
            if (i == 3)
            {
                cell.Colspan = 3;//合并列
            }
            cell.PaddingBottom = 5f;
            cell.PaddingTop = 5f;
            cell.HorizontalAlignment = 1;
            cell.VerticalAlignment = 1;
            table.AddCell(cell);
        }
        document.Add(table);
    }
    /// <summary>
    /// 空格
    /// </summary>
    public void Space()
    {
        Paragraph content = new Paragraph(new Chunk(" ", secondTitleFont));
        document.Add(content);
    }
    /// <summary>
    /// 插入文字内容
    /// </summary>
    /// <param name="content">内容</param>
    /// <param name="font">字体</param>
    /// <param name="type">格式,1为居中</param>
    public void AddContent(string content, Font font, int type = 0)
    {
        Paragraph contentP = new Paragraph(new Chunk(content, font));
        contentP.Alignment = type;
        document.Add(contentP);
    }
    /// <summary>
    /// 插入图片
    /// </summary>
    /// <param name="imageName"></param>
    /// <param name="scale"></param>
    public void AddImage(string imageName)
    {
        string path = Application.streamingAssetsPath + "/ReportRes/" + imageName;
        if (!File.Exists(path)) return;
        Image image = Image.GetInstance(Application.streamingAssetsPath + "/ReportRes/" + imageName);

        //Image image = Resources.Load<Image>(imageName);
        //这里都是图片最原始的宽度与高度
        float resizedWidht = image.Width;
        float resizedHeight = image.Height;
        image.ScaleToFit(475, 325);
        image.Alignment = Element.ALIGN_JUSTIFIED;
        document.Add(image);
    }
    /// <summary>
    /// 新的一页
    /// </summary>
    public void NewPage()
    {
        document.NewPage();
    }
    /// <summary>
    /// 关闭文档
    /// </summary>
    public void ClosePDF()
    {
        document.Close();
    }
}





实例PDF
再创建一个类 吧下面的代码粘贴进去 然后调用CreateTrainingThree 就可以创建PDF了

//分别传入两个表格的数据
  public void CreateTrainingThree(List<string> tableData1, List<string> tableData2, List<string> tableData3, List<string> tableData4)
    {
        Document document = new Document(PageSize.A3, 30, 30, 5, 5);
        Report report = new Report("大标题", document);
        currentTime = System.DateTime.Now;
        report.AddContent("大标题", report.titleFont, 1);
        report.Space();
        report.AddContent("姓 名:                         	          ", report.contentFont);
        report.Space();
        report.AddContent("一、 ", report.secondTitleFont);
        report.Space();
        PurposeThree(report);
        report.Space();
        report.AddContent("二、 ", report.secondTitleFont);
        report.Space();
        TaskThree(report);
        report.Space();
        report.AddContent("三、", report.secondTitleFont);
        report.Space();
        RegulationThree(report);
        report.Space();
        report.AddContent("四、表格", report.secondTitleFont);
        report.Space();
        SetReportTableThree1(tableData1);
        report.AddTableTrainingThree(19, tabeArray);
        report.Space();
        report.Space();

        SetReportTableThree2(tableData2);
        report.AddTableTrainingThree2(21, tabeArray);
        report.ClosePDF();
    }
    void PurposeThree(Report report)
    {
        report.AddContent(@"1111111111", report.contentFont);
    }

    /// <summary>
    /// 实训任务
    /// </summary>
    /// <param name="report"></param>
    void TaskThree(Report report)
    {
        report.AddContent(@"22222222222", report.contentFont);
    }

    /// <summary>
    /// 操作规程
    /// </summary>
    /// <param name="report"></param>
    void RegulationThree(Report report)
    {
        report.AddContent(@"3333333333333", report.contentFont);
    }


    void SetReportTableThree1(List<string> data)
    {

        List<string> tabeList = new List<string> {
            @"标题 ", 
            "序号","时间","1", "2","3","4","5","6",
            "7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22"};
        List<string> tabeData = AddTabeData(data,18);
        for (int i = 0; i < tabeData.Count; i++)
        {
            //Debug.Log(tabeData[i]);
            tabeList.Add(tabeData[i]);

        }
        tabeArray = tabeList.ToArray();
    }
    void SetReportTableThree2(List<string> data)
    {

        List<string> tabeList = new List<string> {
            @"大标题 ",
            "序号","时间","1", "2","3","4","5","6","7",
            "8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"};
        List<string> tabeData = AddTabeData(data,20);
        for (int i = 0; i < tabeData.Count; i++)
        {
            //Debug.Log(tabeData[i]);
            tabeList.Add(tabeData[i]);

        }
        tabeArray = tabeList.ToArray();
    }
     /// <summary>
    /// 添加列表数据 
    /// </summary>
    /// <param name="data">数据</param>
    /// <param column="culoumn">数据列数 </param>>
    /// <returns></returns>
    public List<string> AddTabeData(List<string> data,int column)
    {
        List<string> list = data;
        int line = 0;//行
        if (data.Count>0)
        {
            line = (data.Count / column) ;//计算数据一共有几行
        }
        Debug.Log($"{data.Count}....行{line}...列{column}");
        if (line < 10)//判断是否够十行数据    一共需要有十行数据,
        {
            int num = 10 - line;
            for (int i = 0; i < num; i++)//补充数据到十行
            {
                for (int j = 0; j < column; j++)
                {
                    list.Add(" ");
                }
            }
            line += num;
        }
     
        for (int i = 0; i < line; i++)//十行数据 每一行添加序号
        {
            list.Insert(i * (column+1), (i + 1).ToString());//插入序号
        }
        return list;
    }

  

在这里插入图片描述
下面这个链接是一个博主详细介绍了这个插件的使用
https://blog.csdn.net/fuhanghang/article/details/109639575

插件下载 百度网盘
链接:https://pan.baidu.com/s/1WahZJs8WJbPf8qF8dOvHXg
提取码:j1kb

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值