【Unity 3D】【C#】创建PDF文件且在PDF中创建表格

    因为项目需求,要实现用C#创建PDF文件的功能,在这里需要引入iTextSharp库。引用方法如下:

    1、打开visual studio,打开“工具”菜单栏下的“Nu Get包管理器”下的“管理解决方案的Nu Get程序包”;

【Unity 3D】【C#】创建PDF文件且在PDF中创建表格_第1张图片

    2、搜索“iTextSharp”并安装第一个。

【Unity 3D】【C#】创建PDF文件且在PDF中创建表格_第2张图片

3、将下载好的包移动到unity包的Assets目录下。

4.脚本中添加引用:

5.编写脚本代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

public class NewBehaviourScript : MonoBehaviour {

    public void OnClick() {

        Document document = new Document();
        try
        {
            PdfWriter.GetInstance(document,new FileStream(@"D://生成pdf.pdf",FileMode.Create));
            document.Open();
            //标题字体
            BaseFont bftitle = BaseFont.CreateFont(@"c:\windows\fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font fonttitle = new iTextSharp.text.Font(bftitle, 20);

            BaseFont bf1 = BaseFont.CreateFont(@"c:\windows\fonts\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font font1 = new iTextSharp.text.Font(bf1, 12);
            iTextSharp.text.Font fonttitle10 = new iTextSharp.text.Font(bf1, 12);

            //写标题
            Paragraph title = new Paragraph("××××××考核成绩单", fonttitle);
            //标题居中
            title.Alignment = Rectangle.ALIGN_CENTER;
            document.Add(title);

            //输入一个空行,以分开标题与表格
            Paragraph nullp = new Paragraph(" ", fonttitle);
            nullp.Leading = 10;
           document.Add(nullp);
            //表格一共6列
            PdfPTable table = new PdfPTable(6);
            PdfPCell cellname = new PdfPCell(new Phrase ("姓名",fonttitle10));
            PdfPCell celltxtname = new PdfPCell(new Phrase ("lq", fonttitle10));
            PdfPCell cellno = new PdfPCell(new Phrase ("学号", fonttitle10));
            PdfPCell celltxtno = new PdfPCell(new Phrase ("3117393033", fonttitle10));
            PdfPCell celltime = new PdfPCell(new Phrase ("考核时间", fonttitle10));
            PdfPCell celltxttime = new PdfPCell(new Phrase ("2019-1-3", fonttitle10));
            PdfPCell cellgrade = new PdfPCell(new Phrase ("成绩", fonttitle10));
            PdfPCell celltxtgrade = new PdfPCell(new Phrase ("0",fonttitle10));
            PdfPCell celltotaltime = new PdfPCell(new Phrase ("时间", fonttitle10));
            PdfPCell celltxttotaltime = new PdfPCell(new Phrase ("1467.88s", fonttitle10));
            PdfPCell cellcollider = new PdfPCell(new Phrase ("非法碰撞次数", fonttitle10));
            PdfPCell celltxtcollider = new PdfPCell(new Phrase ("3",fonttitle10));
            PdfPCell celltask = new PdfPCell(new Phrase ("任务", fonttitle10));
            PdfPCell celleachtime = new PdfPCell(new Phrase ("时间", fonttitle10));
            PdfPCell celleachcollider = new PdfPCell(new Phrase ("碰撞次数", fonttitle10));
            PdfPCell cellerror = new PdfPCell(new Phrase ("关键点误差", fonttitle10));
            PdfPCell cellpic = new PdfPCell(new Phrase ("考核路径分析", fonttitle10));
            cellpic.Colspan = 2;
            
            table.AddCell(cellname);
            table.AddCell(celltxtname);
            table.AddCell(cellno);
            table.AddCell(celltxtno);
            table.AddCell(celltime);
            table.AddCell(celltxttime);
            table.AddCell(cellgrade);
            table.AddCell(celltxtgrade);
            table.AddCell(celltotaltime);
            table.AddCell(celltxttotaltime);
            table.AddCell(cellcollider);
            table.AddCell(celltxtcollider);
            table.AddCell(celltask);
            table.AddCell(celleachtime);
            table.AddCell(celleachcollider);
            table.AddCell(cellerror);
            table.AddCell(cellpic);

            
            for (int i = 0;i < 14;i++) {
                
                table.AddCell(new PdfPCell (new Phrase ("第"+i+"个任务节点",fonttitle10)));
                table.AddCell(new PdfPCell (new Phrase (0.ToString(), fonttitle10)));
                table.AddCell(new PdfPCell(new Phrase(0.ToString(), fonttitle10)));
                table.AddCell(new PdfPCell(new Phrase(0.ToString(), fonttitle10)));
                PdfPCell pic = new PdfPCell(new Phrase ("第"+i+"个任务节点的截图", fonttitle10));
                pic.Colspan = 2;
                table.AddCell(pic);
            }
            document.Add(table);
            document.Close();
            Debug.Log("pdf文件写入成功");
        }
        catch (DocumentException de) { Debug.Log(de.Message); }
        catch (IOException io) { Debug.Log(io.Message); }

        
    } 
}

6、存储的文件

【Unity 3D】【C#】创建PDF文件且在PDF中创建表格_第3张图片

你可能感兴趣的:(Unity)