using UnityEngine; using iTextSharp.text.pdf; using iTextSharp.text; using System.IO; using CG.Framework; using System.Collections.Generic; using System; using Font = iTextSharp.text.Font; using System.Linq; public class PDFGreadeManagerTest: ClassSingleton { public void OnClick(string filePath, string modelName, Queue gradeInfo, string studentName, string studentNumber, Action greateOverCall) { Document document = new Document(); try { document = new Document(PageSize.A4); document.PageCount = 2; PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.OpenOrCreate)); document.Open(); //设置字体 string FontPath = Application.streamingAssetsPath + "/SourceHanSansSC-Medium.otf"; BaseFont baseFont = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //设置字体,字体大小 //几种不同的标题字体大小 //报告标题 Font titleFont = new Font(baseFont, 24, Font.BOLD); //一级标题 Font firstTitleFont = new Font(baseFont, 20, Font.BOLD); //二级标题 Font secondTitleFont = new Font(baseFont,16, Font.BOLD); //内容 Font contentFont = new Font(baseFont, 12, Font.NORMAL); //写标题 Paragraph title = new Paragraph(modelName, titleFont); //标题居中 title.Alignment = Rectangle.ALIGN_CENTER; document.Add(title); //输入一个空行,以分开标题与表格 Paragraph nullLine = new Paragraph(" ", secondTitleFont); nullLine.Leading = 5; document.Add(nullLine); //添加一级标题 Paragraph contentP = new Paragraph(new Chunk("一、基础信息", titleFont)); contentP.Alignment = 0; document.Add(contentP); document.Add(nullLine); document.Add(nullLine); // 短句 Phrase phrase = new Phrase("姓名:"+ studentName+ " "+ "学号:" + studentNumber + " "+ " " + "总分:" + totalScore + " " + "实际得分:" + realScore, contentFont ); // 设置间距 document.Add(phrase); //添加第二个标题 //添加一级标题 Paragraph contentP2 = new Paragraph(new Chunk("二、成绩详情", titleFont)); contentP.Alignment = 0; document.Add(contentP2); //添加空行 Paragraph nullLine1 = new Paragraph(" ", secondTitleFont); nullLine1.Leading =20; document.Add(nullLine1); PdfPTable table = new PdfPTable(4); // 设置表格自动行高 table.WidthPercentage = 100; PdfPCell v1 = new PdfPCell(new Phrase("考核环节", secondTitleFont)); PdfPCell v2 = new PdfPCell(new Phrase("考核点", secondTitleFont)); PdfPCell v3 = new PdfPCell(new Phrase("总分", secondTitleFont)); PdfPCell v4 = new PdfPCell(new Phrase("实际得分", secondTitleFont)); v1.HorizontalAlignment = Element.ALIGN_CENTER; v1.VerticalAlignment = Element.ALIGN_CENTER; v2.HorizontalAlignment = Element.ALIGN_CENTER; v2.VerticalAlignment = Element.ALIGN_CENTER; v3.HorizontalAlignment = Element.ALIGN_CENTER; v3.VerticalAlignment = Element.ALIGN_CENTER; v4.HorizontalAlignment = Element.ALIGN_CENTER; v4.VerticalAlignment = Element.ALIGN_CENTER; v1.MinimumHeight = 30; v2.MinimumHeight = 30; v3.MinimumHeight = 30; v4.MinimumHeight = 30; table.AddCell(v1); table.AddCell(v2); table.AddCell(v3); table.AddCell(v4); // 将list中的数据添加到table PdfPCell cell1; List list = gradeInfo.ToList(); for (int j = 0; j < list.Count; j++) { // WDebug.Log(list[j]+"!!!!!!!!!!"); for (int i = 0; i < list[j].Length; i++) { //list[j][i] WDebug.Log(list[j][i]+"!!!!!!!!!!!!"); cell1 = new PdfPCell(new Phrase(list[j][i], contentFont)); cell1.PaddingBottom = 5f; cell1.PaddingTop = 5f; cell1.HorizontalAlignment = 1; cell1.VerticalAlignment = 1; cell1.HorizontalAlignment = Element.ALIGN_CENTER; cell1.VerticalAlignment = Element.ALIGN_MIDDLE; if (Current < list[j].Length) { Current = j + 1; } else { Current = list[j].Length; } if (list[j][0]== list[Current][0]) { WDebug.Log("相等一个"); WDebug.Log(list[j][0]+"!!!!!!!"); WDebug.Log(list[Current][0] + "!!!!!!!"); } //设置跨行。跨列 // cell1.Rowspan = 5; // cell1.Colspan = 6; table.AddCell(cell1); } } //foreach (string[] data in gradeInfo) //{ // for (int i = 0; i < data.Length; i++) // { // cell1 = new PdfPCell(new Phrase(data[i], contentFont)); // if (Current< data.Length) // { // Current= i + 1; // } // else // { // Current = data.Length; // } // cell1.PaddingBottom = 5f; // cell1.PaddingTop = 5f; // cell1.HorizontalAlignment = 1; // cell1.VerticalAlignment = 1; // cell1.HorizontalAlignment = Element.ALIGN_CENTER; // cell1.VerticalAlignment = Element.ALIGN_MIDDLE; // table.AddCell(cell1); // } //} document.Add(table); document.Close(); WDebug.Log("pdf文件写入成功"); } catch (DocumentException de) { WDebug.Log(de.Message); } catch (IOException io) { WDebug.Log(io.Message); } greateOverCall?.Invoke(); } int totalScore ; int realScore ; int Current; int Rowspan; int Colspan; }