2019-01-09Unity利用"itextsharp创建PDF文档

Unity制作PDF文档

一、效果图

二、软件环境

Unity5.6.0链接:https://pan.baidu.com/s/1fLj1phwY8YO4wdyf4FBe6w 密码:eicq

三、实现步骤

1.下载我们创建PDF需要使用的”itextsharp.dll“库文件,以及”GraphMaker1.5.7“绘制图表的插件

2.创建一个空工程,建立如下文件夹,导入itextsharp.dll,创建新scene

3.创建pdf生成脚本

4.创建脚本,加入以下引用

using System;

using UnityEngine;

using UnityEngine.UI;

using System.IO;

using iTextSharp.text;

using iTextSharp.text.pdf;

using System.Collections.Generic;

5.添加一些基本的使用变量

 #region 基本信息

  public Camera cam_height;

  public Camera cam;

  private Rect CutRect = new Rect(0, 0, 1, 1);

  private int resWidth = 710;

  private int resHeight = 512;

  private string testContent = "专注力测试";//测试内容0.

  public Button testBtn;

  public string testTime = "2018年7月20日10:00";//测试时间

  string fileTime;

  string fileName;

  string pdfPath = "D://心理报告/";

  #endregion

 #region 测试人员信息

  private string teacher = "王某某";

  private string id = "10010";

  private string name = "李某某";

  private string sex = "男";

  private string age = "26";

  #endregion

  #region 航行信息

  private string model = "F22"; //机型

  private string airport = "哈尔滨太平机场"; //机场

  private string weather = "暴风雪"; //天气

  private string time = "早晨";  //时间

  private string launch_speed = "0";//起飞速度

  private string land_speed = "0"; //降落速度

  private string distance = "0";  //飞行距离

  private string max_height = "0"; //最大飞行高度

  private string land_pitch = "0"; //降落俯仰角

  private string launch_pitch = "0";//起飞俯仰角

  private string flight_time = "0"; //留空时间

  private string ground_time = "0"; //地面时间

  private string luanch_sldtm = "0";//起飞滑行时间

  private string land_sldtm = "0"; //降落滑行时间

  #endregion

  void Start () {

    testBtn.onClick.AddListener(btnClicked);

    DateTime dt = DateTime.Now;

    fileName = dt.Year + "-" + dt.Month + "-" + dt.Day + "_" + dt.Hour + ":" + dt.Minute + ":" +       dt.Second + ".pdf";

    fileTime = dt.Year + "年" + dt.Month + "月" + dt.Day + "日" + dt.Hour + ":" + dt.Minute;

  }

6.创建button点击事件,具体行代码的含义都有注释在,看不懂的可以留下评论,如果不看文档创建PDF还是有一些坑的,比如文字居中对齐,中文字体加入到表格中,设置行间距等。

  //点击事件,创建PDF文档

  public void btnClicked()

  {

    try

    {

      //实用TTF字体

      BaseFont heiBaseFont = BaseFont.CreateFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

      //创建PDF文档

      Document doc = new Document(PageSize.A4, 40, 40, 40, 40);

      //创建写入实例,PDF文档保存位置

      PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(pdfPath + fileName, FileMode.Create));

      //创建标题字体

      iTextSharp.text.Font titleFont = new iTextSharp.text.Font(heiBaseFont, 20);

      //创建二级标题字体

      iTextSharp.text.Font titleFont2 = new iTextSharp.text.Font(heiBaseFont, 16);

      //创建三级标题

      iTextSharp.text.Font titleFont3 = new iTextSharp.text.Font(heiBaseFont, 14);

      //创建正文字体

      iTextSharp.text.Font textFont = new iTextSharp.text.Font(heiBaseFont, 12);

      //页眉页脚

      HeaderFooter footer = new HeaderFooter(new Phrase(" ", titleFont), true);

      footer.Border = Rectangle.ALIGN_CENTER;

      footer.Alignment = Element.ALIGN_CENTER;

      doc.Footer = footer;

      //打开文档

      doc.Open();

      //创建摘要

      doc.AddTitle("");

      doc.AddAuthor("Addis Chen");

      doc.AddSubject("PDF Test");

      doc.AddCreator("PDF Test");

      //创建一个标题

      Paragraph pdf_title = new Paragraph("飞行模拟运动仿真心理测试评估报告", titleFont);

      pdf_title.Alignment = iTextSharp.text.Element.ALIGN_CENTER;

      doc.Add(pdf_title);

      //创建第一段落    

      Paragraph pdf_first1 = new Paragraph("一、基本信息", titleFont2);

      pdf_first1.SetLeading(0, 2);

      doc.Add(pdf_first1);

      Paragraph pdf_first2 = new Paragraph("   教员:" + teacher + "  姓名:" + name + "   ID:" + id, textFont);

      pdf_first2.SetLeading(0, 2.5f);

      doc.Add(pdf_first2);

      Paragraph pdf_first3 = new Paragraph("   性别: " + sex + "   年龄: " + age + "    测试时间:" + testTime, textFont);

      pdf_first3.SetLeading(0, 2);

      doc.Add(pdf_first3);

      //创建第二段落

      Paragraph pdf_second1 = new Paragraph("二、航程数据", titleFont2);

      pdf_second1.SetLeading(0, 2);

      doc.Add(pdf_second1);

      #region 创建表格

      //设置表格有几列

      Table table = new Table(4);

      //table.Alignment = Element.ALIGN_CENTER;

      //表格边框宽度

      table.BorderWidth = 1;

      //边框颜色

      table.BorderColor = new iTextSharp.text.Color(0, 0, 0);

      //表格的填距,就是单元格边界和内容间一定数量的空间,在前面的示例中,我们看到文本紧贴边界,通过使用用特定的填距,就可以避免。

      table.Cellpadding = 2;

      //表格的间距,就是单元格和表格边界间的一定数量的空间,不同的单元格间使用了半数空间。

      table.Cellspacing = 1;

      Cell cell1_1 = new Cell(new Phrase(""));

      cell1_1.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pmodel = new Paragraph("机型", textFont);

      cell1_1.AddElement(Pmodel);

      table.AddCell(cell1_1);

      Cell cell1_2 = new Cell(new Phrase(""));

      cell1_2.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pmodel1 = new Paragraph(model, textFont);

      cell1_2.AddElement(Pmodel1);

      table.AddCell(cell1_2);

      Cell cell1_3 = new Cell(new Phrase(""));

      cell1_3.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pairport = new Paragraph("机场", textFont);

      cell1_3.AddElement(Pairport);

      table.AddCell(cell1_3);

      Cell cell1_4 = new Cell(new Phrase(""));

      cell1_4.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph PPairport = new Paragraph(airport, textFont);

      cell1_4.AddElement(PPairport);

      table.AddCell(cell1_4);

      Cell cell2_1 = new Cell(new Phrase(""));

      cell2_1.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pweather = new Paragraph("天气", textFont);

      cell2_1.AddElement(Pweather);

      table.AddCell(cell2_1);

      Cell cell2_2 = new Cell(new Phrase(""));

      cell2_2.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph PPweather = new Paragraph(weather, textFont);

      cell2_2.AddElement(Pweather);

      table.AddCell(cell2_2);

      Cell cell2_3 = new Cell(new Phrase(""));

      cell2_3.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Ptime = new Paragraph("时间", textFont);

      cell2_3.AddElement(Ptime);

      table.AddCell(cell2_3);

      Cell cell2_4 = new Cell(new Phrase(""));

      cell2_4.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph PPtime = new Paragraph(time, textFont);

      cell2_4.AddElement(PPtime);

      table.AddCell(cell2_4);

      Cell cell3_1 = new Cell(new Phrase(""));

      cell3_1.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Plaunch_speed = new Paragraph("起飞速度", textFont);

      cell3_1.AddElement(Plaunch_speed);

      table.AddCell(cell3_1);

      Cell cell3_2 = new Cell(new Phrase(""));

      cell3_2.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Plaunch_speed2 = new Paragraph(launch_speed, textFont);

      cell3_2.AddElement(Plaunch_speed2);

      table.AddCell(cell3_2);

      Cell cell3_3 = new Cell(new Phrase(""));

      cell3_3.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pland_speed = new Paragraph("降落速度", textFont);

      cell3_3.AddElement(Pland_speed);

      table.AddCell(cell3_3);

      Cell cell3_4 = new Cell(new Phrase(""));

      cell3_4.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pland_speed2 = new Paragraph(land_speed, textFont);

      cell3_4.AddElement(Pland_speed2);

      table.AddCell(cell3_4);

      Cell cell4_1 = new Cell(new Phrase(""));

      cell4_1.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pdistance = new Paragraph("飞行距离", textFont);

      cell4_1.AddElement(Pdistance);

      table.AddCell(cell4_1);

      Cell cell4_2 = new Cell(new Phrase(""));

      cell4_2.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pdistance2 = new Paragraph(distance, textFont);

      cell4_2.AddElement(Pdistance2);

      table.AddCell(cell4_2);

      Cell cell4_3 = new Cell(new Phrase(""));

      cell4_3.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pmax_height = new Paragraph("最大飞行高度", textFont);

      cell4_3.AddElement(Pmax_height);

      table.AddCell(cell4_3);

      Cell cell4_4 = new Cell(new Phrase(""));

      cell4_4.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pmax_height2 = new Paragraph(max_height, textFont);

      cell4_4.AddElement(Pmax_height2);

      table.AddCell(cell4_4);

      Cell cell5_1 = new Cell(new Phrase(""));

      cell5_1.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pland_pitch = new Paragraph("降落俯仰角", textFont);

      cell5_1.AddElement(Pland_pitch);

      table.AddCell(cell5_1);

      Cell cell5_2 = new Cell(new Phrase(""));

      cell5_2.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pland_pitch2 = new Paragraph(land_pitch, textFont);

      cell5_2.AddElement(Pland_pitch2);

      table.AddCell(cell5_2);

      Cell cell5_3 = new Cell(new Phrase(""));

      cell5_3.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Plaunch_pitch = new Paragraph("起飞俯仰角", textFont);

      cell5_3.AddElement(Plaunch_pitch);

      table.AddCell(cell5_3);

      Cell cell5_4 = new Cell(new Phrase(""));

      cell5_4.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Plaunch_pitch2 = new Paragraph(launch_pitch, textFont);

      cell5_4.AddElement(Plaunch_pitch2);

      table.AddCell(cell5_4);

      Cell cell6_1 = new Cell(new Phrase(""));

      cell6_1.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pflight_time = new Paragraph("留空时间", textFont);

      cell6_1.AddElement(Pflight_time);

      table.AddCell(cell6_1);

      Cell cell6_2 = new Cell(new Phrase(""));

      cell6_2.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pflight_time2 = new Paragraph(flight_time, textFont);

      cell6_2.AddElement(Pflight_time2);

      table.AddCell(cell6_2);

      Cell cell6_3 = new Cell(new Phrase(""));

      cell6_3.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pground_time = new Paragraph("地面时间", textFont);

      cell6_3.AddElement(Pground_time);

      table.AddCell(cell6_3);

      Cell cell6_4 = new Cell(new Phrase(""));

      cell6_4.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pground_time2 = new Paragraph(ground_time, textFont);

      cell6_4.AddElement(Pground_time2);

      table.AddCell(cell6_4);

      Cell cell7_1 = new Cell(new Phrase(""));

      cell7_1.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pluanch_sldtm = new Paragraph("降落滑行时间", textFont);

      cell7_1.AddElement(Pluanch_sldtm);

      table.AddCell(cell7_1);

      Cell cell7_2 = new Cell(new Phrase(""));

      cell7_2.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pluanch_sldtm2 = new Paragraph(luanch_sldtm, textFont);

      cell7_2.AddElement(Pluanch_sldtm2);

      table.AddCell(cell7_2);

      Cell cell7_3 = new Cell(new Phrase(""));

      cell7_3.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pland_sldtm = new Paragraph("起飞滑行时间", textFont);

      cell7_3.AddElement(Pland_sldtm);

      table.AddCell(cell7_3);

      Cell cell7_4 = new Cell(new Phrase(""));

      cell7_4.HorizontalAlignment = Element.ALIGN_CENTER;

      Paragraph Pland_sldtm2 = new Paragraph(land_sldtm, textFont);

      cell7_4.AddElement(Pland_sldtm2);

      table.AddCell(cell7_4);

      doc.Add(table);

      #endregion

      //关闭文档

      doc.Close();

      //打开pdf文件

      System.Diagnostics.Process.Start(pdfPath + fileName);

    }

    catch (Exception e)

    {

      // label2.text = e.Message;

    }

  }

7.目前为止,出这折线图,其他的就都创建完毕了,下面我们着重讲一下折线图的创建

(1) 导入GraphMaker1.5.7插件

(2)将图中预制体拖拽至canvas下,如图

(3)设置X、Y轴最大值,最小值和分段数

(4)图表一些主要参数做一下简单介绍,如下图,LineGraph是整个图表,它下面的WMG_Axis_Graph属性是对整个图表的设置,如上一步中X、Y轴的参数设定,Background对背景的设置,Anchored这个参数在一个图表显示多条曲线的时候可用,graphTittle设置图表标题,Series下具体对Y轴点坐标赋值。Axis Type设置表格类型。Series 的size设置一个图表中画几条线。其他不常用的我就不一一赘述了。

(5)代码中加入向PDF中存储图片的代码,加入到 关闭文档之前,如下

      //存图片 

      iTextSharp.text.Image imge;

      imge = MakeCameraImg(cam_height, 1920, 1080); //此处第二和第三个参数控制图片输出大小

      iTextSharp.text.Image mark = imge;

      mark.ScaleAbsolute(450, 240);//将图像缩放至

      mark.Alignment = iTextSharp.text.Image.ALIGN_CENTER;

      doc.Add(mark);

      //关闭文档

      doc.Close();

      //打开pdf文件

      System.Diagnostics.Process.Start(pdfPath + fileName);

(6)添加图片存储的事件void

 private iTextSharp.text.Image MakeCameraImg(Camera mCam, int width, int height)

  {

    iTextSharp.text.Image mImage;

    RenderTexture rt = new RenderTexture(width, height, 2);

    mCam.pixelRect = new Rect(0, 0, Screen.width, Screen.height);

    mCam.targetTexture = rt;

    Texture2D screenShot = new Texture2D((int)(width * CutRect.width), (int)(height * CutRect.height),

                        TextureFormat.RGB24, false);

    mCam.Render();

    RenderTexture.active = rt;

    screenShot.ReadPixels(new Rect(width * CutRect.x, width * CutRect.y, width * CutRect.width, height *

      CutRect.height), 0, 0);

    mCam.targetTexture = null;

    RenderTexture.active = null;

    UnityEngine.Object.Destroy(rt);

    byte[] bytes = screenShot.EncodeToPNG();

    mImage = iTextSharp.text.Image.GetInstance(bytes);

    return mImage;

  }

(7)此方法其实是截取某个摄像头下的图片,所有我们要新建一个摄像头,新建一个canvas(screen space-camera),制定到新摄像头,二者主要负责输出表格图片,调整表格大小至相机填满整个相机视角,具体如下

(8)将图表相机拖拽至脚本上

(9)OK大功告成,具体细节自己再慢慢调把

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,029评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,395评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,570评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,535评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,650评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,850评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,006评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,747评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,207评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,536评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,683评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,342评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,964评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,772评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,004评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,401评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,566评论 2 349

推荐阅读更多精彩内容