首页 >> 大全

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

2023-09-06 大全 25 作者:考证青年

封装一个创建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;/// /// 创建报告的初始化/// 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();}/// /// 打开文档/// void StartPDF(){document.Open();}/// /// 增加表格/// /// 列数/// 内容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);}/// /// 实训一 列表数据/// /// /// 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);}/// /// 实训三 列表数据/// /// /// 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);}/// /// 实训三 列表数据/// /// /// 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);}/// /// 空格/// public void Space(){Paragraph content = new Paragraph(new Chunk(" ", secondTitleFont));document.Add(content);}/// /// 插入文字内容/// /// 内容/// 字体/// 格式,1为居中public void AddContent(string content, Font font, int type = 0){Paragraph contentP = new Paragraph(new Chunk(content, font));contentP.Alignment = type;document.Add(contentP);}/// /// 插入图片/// /// /// 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(imageName);//这里都是图片最原始的宽度与高度float resizedWidht = image.Width;float resizedHeight = image.Height;image.ScaleToFit(475, 325);image.Alignment = Element.ALIGN_JUSTIFIED;document.Add(image);}/// /// 新的一页/// public void NewPage(){document.NewPage();}/// /// 关闭文档/// public void ClosePDF(){document.Close();}
}

实例PDF

再创建一个类 吧下面的代码粘贴进去 然后调用 就可以创建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);}/// /// 实训任务/// /// void TaskThree(Report report){report.AddContent(@"22222222222", report.contentFont);}/// /// 操作规程/// /// 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();}

_java动态生成html文件_动态生成xml文件

下面这个链接是一个博主详细介绍了这个插件的使用

插件下载 百度网盘

链接:

提取码:j1kb

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了