loudixvmuniu/Assets/_Scripts/Framework/Utilities/PDFGreadeManagerTest.cs

229 lines
7.0 KiB
C#
Raw Normal View History

2025-01-02 12:15:45 +08:00
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<PDFGreadeManagerTest>
{
public void OnClick(string filePath, string modelName, Queue<string[]> 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();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
string FontPath = Application.streamingAssetsPath + "/SourceHanSansSC-Medium.otf";
BaseFont baseFont = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
//<2F><><EFBFBD>ֲ<EFBFBD>ͬ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Font titleFont = new Font(baseFont, 24, Font.BOLD);
//һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Font firstTitleFont = new Font(baseFont, 20, Font.BOLD);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Font secondTitleFont = new Font(baseFont,16, Font.BOLD);
//<2F><><EFBFBD><EFBFBD>
Font contentFont = new Font(baseFont, 12, Font.NORMAL);
//д<><D0B4><EFBFBD><EFBFBD>
Paragraph title = new Paragraph(modelName, titleFont);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
title.Alignment = Rectangle.ALIGN_CENTER;
document.Add(title);
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3>Էֿ<D4B7><D6BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Paragraph nullLine = new Paragraph(" ", secondTitleFont);
nullLine.Leading = 5;
document.Add(nullLine);
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Paragraph contentP = new Paragraph(new Chunk("һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ", titleFont));
contentP.Alignment = 0;
document.Add(contentP);
document.Add(nullLine);
document.Add(nullLine);
// <20>̾<EFBFBD>
Phrase phrase = new Phrase("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"+ studentName+
" "+ <>ţ<EFBFBD>" + studentNumber + " "+
" " + "<22>ܷ֣<DCB7>" + totalScore + " " +
<>ʵ÷֣<C3B7>" + realScore,
contentFont );
// <20><><EFBFBD>ü<EFBFBD><C3BC><EFBFBD>
document.Add(phrase);
//<2F><><EFBFBD>ӵڶ<D3B5><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Paragraph contentP2 = new Paragraph(new Chunk("<22><><EFBFBD><EFBFBD><EFBFBD>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD><EFBFBD>", titleFont));
contentP.Alignment = 0;
document.Add(contentP2);
//<2F><><EFBFBD>ӿ<EFBFBD><D3BF><EFBFBD>
Paragraph nullLine1 = new Paragraph(" ", secondTitleFont);
nullLine1.Leading =20;
document.Add(nullLine1);
PdfPTable table = new PdfPTable(4);
// <20><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6>и<EFBFBD>
table.WidthPercentage = 100;
PdfPCell v1 = new PdfPCell(new Phrase("<22><><EFBFBD>˻<EFBFBD><CBBB><EFBFBD>", secondTitleFont));
PdfPCell v2 = new PdfPCell(new Phrase("<22><><EFBFBD>˵<EFBFBD>", secondTitleFont));
PdfPCell v3 = new PdfPCell(new Phrase("<22>ܷ<EFBFBD>", secondTitleFont));
PdfPCell v4 = new PdfPCell(new Phrase(<>ʵ÷<CAB5>", 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);
// <20><>list<73>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>table
PdfPCell cell1;
List<string[]> 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("<22><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>");
WDebug.Log(list[j][0]+"!!!!!!!");
WDebug.Log(list[Current][0] + "!!!!!!!");
}
//<2F><><EFBFBD>ÿ<EFBFBD><C3BF>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>
// 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<64>ļ<EFBFBD>д<EFBFBD><D0B4><EFBFBD>ɹ<EFBFBD>");
}
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;
}