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大功告成,具体细节自己再慢慢调把

你可能感兴趣的:(2019-01-09Unity利用"itextsharp创建PDF文档)