unity生成pdf格式,首先需要导入iTextSharp.dll ,下面是我写的一些方法,可以直接用.直接贴代码,
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.Text.RegularExpressions;
using System.Data.OracleClient;
using System; //需要用到的数据结构
public class PdfUserInformation
{
public string goodsName;
public string youzhanType;
public string cvsLevel;
public string writeTime;
public string creater;
public string creatTime;
};
//数据结构
public class PdfInformation
{
public string mID;
public string mName;
public string mReserve;
// ....
}; public class PDFMakerManager { public static PDFMakerManager PDFInstance;
public static PDFMakerManager createPDFMakerManager()
{
if(PDFInstance==null)
{
PDFInstance = new PDFMakerManager ();
} return PDFInstance;
} //获取物品的子物体名字 通过子物体的名字获取物体 返回物体的链表
public List<PdfInformation> getObjChildsName(Transform mParentObj)
{ if(mParentObj!=null)
{
List<string> mChildObjs = new List<string>();
List<PdfInformation> mInfoList = new List<PdfInformation>();
foreach (Transform mChildTransform in mParentObj)
{
mChildObjs.Add(mChildTransform.gameObject.name);
Debug.Log(mChildTransform.gameObject.name);
}
//获取 数据库 信息
getPdfDatasFromDB();
//通过子物体的名字 获取物体信息
for (int i = 0; i < mChildObjs.Count; i++)
{
mInfoList.Add(mDirectoryData[mChildObjs[i]]);
}
return mInfoList;
}
return null;
}
//每行五个 还有问题,由于需求以及时间关系,没时间去管了 谁看到了可以帮忙解决下,.thanks
List<PdfInformation> mGoodsInfoList = new List<PdfInformation>();
public List<List<PdfInformation>> getAllGoods(List<PdfInformation> mList)
{
List<List<PdfInformation>> mGoodsAll = new List<List<PdfInformation>>();
List<List<PdfInformation>> mGoods = new List<List<PdfInformation>>(); for (int i = 1; i <= mList.Count;i++ )
{
mGoodsInfoList.Add(mList[i-1]); if(i%5==0)
{ mGoodsAll.Add(mGoodsInfoList);
Debug.Log("xx"+mGoodsInfoList.Count);
mGoodsInfoList.Clear(); for (int j = 0; j < mGoodsAll.Count; j++)
{
Debug.Log("InfoList [[[" + j + "]]]is:" + mGoodsAll[j].Count);
} }else if(i%5!=0&&i==mList.Count)
{
mGoodsAll.Add(mGoodsInfoList);
mGoodsInfoList.Clear();
}
}
Debug.Log("InfoList [[[is:" + mGoodsAll.Count);
//for (int j = 0; j < mGoodsAll.Count;j++ )
//{
// Debug.Log("InfoList [[[" + j + "]]]is:" + mGoodsAll[j].Count);
//} return mGoodsAll;
}
//从数据库 获取 PDF 信息
Dictionary<string, PdfInformation> mDirectoryData = new Dictionary<string, PdfInformation>();
public List<PdfInformation> getPdfDatasFromDB()
{
List<PdfInformation> mInfoList = new List<PdfInformation>(); Oracle mDB = new Oracle ();
try
{
string mSearch = "select * from mdb";
mInfoList = mDB.Select(mSearch);
mDirectoryData = mDB.getPdfDictionary();
}catch(Exception e)
{
mDB.Close();
Debug.Log("sql error");
}
return mInfoList;
} // / pdf 行
public PdfPTable CreateRankGoodsInforMationTable(List<List<PdfInformation>> mList)
{
PdfPTable mTable=new PdfPTable(1); mTable.SetTotalWidth(new float[]{300}); mTable.HorizontalAlignment = PdfPTable.LINECANVAS; if(mList.Count>0)
{
for(int i=0;i<mList.Count;i++)
{
Debug.Log("xx"+i+"xxxxxx:"+mList[i].Count);
// mTable.AddCell(CreateCell(CreateRowGoodsInforMationTable2(mList[i]),-1,0));
} } mTable.LockedWidth = true;
return mTable;
}
//
//pdf 列
PdfPTable CreateRowGoodsInforMationTable2(System.Collections.Generic.List<PdfInformation> mList)
{
PdfPTable mTable =new PdfPTable (mList.Count);
System.Collections.Generic.List<float> Width = new System.Collections.Generic.List<float> ();
if(mList.Count>0)
{
for(int i=0;i<mList.Count;i++)
{
Width.Add(80f);
}
}
float []mWidth = Width.ToArray();
mTable.HorizontalAlignment = PdfPTable.BACKGROUNDCANVAS; if (mWidth[0] != -1)
{
mTable.SetTotalWidth(mWidth);
}
mTable.LockedWidth = true;
for(int i=0;i<mList.Count;i++)
{ mTable.AddCell(CreateGoodsInformationCell(mList[i]));
}
mList.Clear();
return mTable;
} //pdf 图 暂时没用
byte[] GoodsImgPrint(Camera SmallCamera,GameObject TakeHuoJia)
{
int swidth = (int)(SmallCamera.WorldToScreenPoint(TakeHuoJia.transform.position + TakeHuoJia.renderer.bounds.size).x - SmallCamera.WorldToScreenPoint(TakeHuoJia.transform.position - TakeHuoJia.renderer.bounds.size).x); RenderTexture rt = new RenderTexture(Screen.width, resHeight, 72);
SmallCamera.pixelRect = new Rect(0, 0, Screen.width, Screen.height);
SmallCamera.targetTexture = rt;
Texture2D screenShot = new Texture2D(swidth + 100, resHeight, TextureFormat.ARGB32, false);
SmallCamera.Render();
RenderTexture.active = rt; screenShot.ReadPixels(new Rect(SmallCamera.WorldToScreenPoint(TakeHuoJia.transform.position - TakeHuoJia.renderer.bounds.size).x, 0, swidth + 100, resHeight), 0, 0);
screenShot.Apply();
SmallCamera.targetTexture = null;
RenderTexture.active = null; //
UnityEngine.Object.Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG(); return bytes;
}
//打印物品详细信息 图 (左边字 右边图 分开的 ,写法一:pdf字和摄像头图)
public void PrintGoodsInformations2(string filePath, Camera mCam, GameObject mObj, PdfUserInformation mInfo)
{
Document document = new Document(new Rectangle(PageSize.A4.Height, PageSize.A4.Width));
PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
document.Open(); PdfPTable tableAll = tableAll = CreateTable(2, PdfPTable.BACKGROUNDCANVAS, new float[] { document.PageSize.Width / 2 - 50, document.PageSize.Width / 2 - 50 }, true);
//
PdfPTable tableLeft = PrintGoodsInformations_Left(mInfo); Image tableRight = MakeCameraImg(mCam, 512, 512);//Image.GetInstance( GoodsImgPrint(mCam,mObj)); tableAll.AddCell(CreateCell(tableLeft, -1, 0));
tableAll.AddCell(CreateCell(tableRight, -1, 0));
document.Add(tableAll);
document.Close();
ShowTips.showTips(" 打印完成!文件保存在:" + filePath);
}
//打印物品详细信息 图 (左边字 右边图 分开的 ,写法2:摄像头全图,包括字和图)
public void PrintGoodsInformations(string filePath, Camera mCam,GameObject mObj,PdfUserInformation mInfo)
{
Document document = new Document(new Rectangle(PageSize.A4.Height, PageSize.A4.Width));
PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
document.Open(); PdfPTable tableAll = CreateTable(1, PdfPTable.BACKGROUNDCANVAS, new float[] {document.PageSize.Width -100 }, true);
//
PdfPTable tableLeft = PrintGoodsInformations_Left(mInfo); Image tableRight = MakeCameraImg(mCam,680,512);//Image.GetInstance( GoodsImgPrint(mCam,mObj)); // tableAll.AddCell(CreateCell(tableLeft, -1, 0));
tableAll.AddCell(CreateCell(tableRight, -1, 0));
document.Add(tableAll);
document.Close();
ShowTips.showTips(" 打印完成!文件保存在:" + filePath);
} //打印物品详细信息 左边字 右边字
public void PrintGoodsInformations(string filePath,PdfUserInformation mInfo)
{
Document document = new Document(new Rectangle(PageSize.A4.Height,PageSize.A4.Width));
PdfWriter.GetInstance(document,new FileStream(filePath,FileMode.Create));
document.Open(); PdfPTable tableAll = CreateTable(2, PdfPTable.BACKGROUNDCANVAS, new float[] { document.PageSize.Width / 2 - 50, document.PageSize.Width / 2 - 50 }, true);
//
PdfPTable tableLeft = PrintGoodsInformations_Left(mInfo); //测试数据 ....
PdfInformation mInfo1 = new PdfInformation();
mInfo1.mName = "aaaa";
PdfInformation mInfo2 = new PdfInformation();
mInfo2.mName = "bbbbb"; List<PdfInformation> mInfoList = new List<PdfInformation>();
mInfoList.Add(mInfo1);
mInfoList.Add(mInfo2); PdfInformation mInfo3 = new PdfInformation();
mInfo3.mName = "ccccc";
PdfInformation mInfo4 = new PdfInformation();
mInfo4.mName = "ddddd"; List<PdfInformation> mInfoList2 = new List<PdfInformation>();
mInfoList2.Add(mInfo3);
mInfoList2.Add(mInfo4); List<List<PdfInformation>> mLLInfo = new List<List<PdfInformation>>();
mLLInfo.Add(mInfoList);
mLLInfo.Add(mInfoList2); //....... //测试数据 数据库数据
// List<List<PdfInformation>> mDBTestList = new List<List<PdfInformation>>();
//mDBTestList = getAllGoods(getPdfDatasFromDB()); //Debug.Log("DBList is:"+mDBTestList.Count);
//...... PdfPTable tableRight = CreateRankGoodsInforMationTable(mDBTestList);
tableRight.HorizontalAlignment = PdfPTable.BACKGROUNDCANVAS;
tableAll.AddCell(CreateCell(tableLeft, -1, 0));
tableAll.AddCell(CreateCell(tableRight,-1,0));
document.Add(tableAll);
document.Close();
ShowTips.showTips(" 打印完成!文件保存在:" + filePath); }
//详细信息的左半部分
PdfPTable PrintGoodsInformations_Left(PdfUserInformation mInfo)
{
PdfPTable tableHeaderR = CreateTable(1, PdfPTable.BACKGROUNDCANVAS, new float[] { -1 }, false);
PdfPTable tableHeaderminiR = CreateTable(2, PdfPTable.BACKGROUNDCANVAS, new float[] { 1, 3 }, false); Image mark = Image.GetInstance(Application.dataPath + "/Icon.png");
mark.ScaleAbsolute(250, 250);
mark.Alignment = Image.LEFT_BORDER;
tableHeaderminiR.AddCell(CreateCell(mark, 2, 0));//<<<<<<<<< //tableHeaderminiR.AddCell(CreateCell("商品", -1, 0, CreateFont(32, iTextSharp.text.Font.NORMAL)));//<<<<<<<<
tableHeaderminiR.AddCell(CreateCell("标题", -1, 0, CreateFont(32, iTextSharp.text.Font.NORMAL)));//<<<<<<<<
tableHeaderminiR.AddCell(CreateCell("xxx", -1, 0, CreateFont(22, iTextSharp.text.Font.NORMAL)));//<<<<<<<< PdfPTable tableDataR = CreateTable(1, PdfPTable.BACKGROUNDCANVAS, new float[] { 350 }, true);
tableDataR.AddCell(CreateCell("\n", -1, 0, CreateFont(12, iTextSharp.text.Font.NORMAL)));
tableDataR.AddCell(CreateCell("名称:\n", -1, -1, CreateFont(22, iTextSharp.text.Font.NORMAL)));
tableDataR.AddCell(CreateCell(mInfo.goodsName, -1, -1, CreateFont(20, iTextSharp.text.Font.NORMAL)));
tableDataR.AddCell(CreateCell("类型:\n", -1, -1, CreateFont(22, iTextSharp.text.Font.NORMAL)));
tableDataR.AddCell(CreateCell(mInfo.youzhanType, -1, -1, CreateFont(20, iTextSharp.text.Font.NORMAL)));
tableDataR.AddCell(CreateCell("级别:\n", -1, -1, CreateFont(22, iTextSharp.text.Font.NORMAL)));
tableDataR.AddCell(CreateCell(mInfo.cvsLevel, -1, -1, CreateFont(20, iTextSharp.text.Font.NORMAL)));
tableDataR.AddCell(CreateCell("时间:\n", -1, -1, CreateFont(22, iTextSharp.text.Font.NORMAL)));
tableDataR.AddCell(CreateCell(mInfo.writeTime, -1, -1, CreateFont(20, iTextSharp.text.Font.NORMAL)));
tableDataR.AddCell(CreateCell("创建者:", -1, -1, CreateFont(22, iTextSharp.text.Font.NORMAL)));
tableDataR.AddCell(CreateCell(mInfo.creater, -1, -1, CreateFont(20, iTextSharp.text.Font.NORMAL)));
tableDataR.AddCell(CreateCell("创建时间:\n", -1, -1, CreateFont(22, iTextSharp.text.Font.NORMAL)));
tableDataR.AddCell(CreateCell(mInfo.creatTime, -1, -1, CreateFont(20, iTextSharp.text.Font.NORMAL)));
tableHeaderR.AddCell(CreateCell(tableHeaderminiR, -1, 0));//<<<<<<<<
tableHeaderR.AddCell(CreateCell(tableDataR, -1, 0)); return tableHeaderR;
} //把摄像头视野 制作成png图片
private Rect CutRect = new Rect(0, 0, 1, 1);
private int resWidth = 710;
private int resHeight = 512;
private Image MakeCameraImg(Camera mCam,int width,int height)
{
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();
//string filename = Application.dataPath + "/Print/3D"
// + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png";
//System.IO.File.WriteAllBytes(filename, bytes); mImage = Image.GetInstance(bytes);
return mImage; }
//打印图片 (摄像头视野看到的/生成的图片) 成PDF
public void PrintImgPdf(string filePath, Camera mCam)
{
Document document = new Document(new Rectangle(PageSize.A4.Height, PageSize.A4.Width));
PdfWriter.GetInstance(
document,
new FileStream(filePath, FileMode.Create)
); document.Open();
Image imge;
imge = MakeCameraImg(mCam,700,512);
PdfPTable tableAll = CreateTable(1, PdfPTable.BACKGROUNDCANVAS, new float[] { document.PageSize.Width -50}, true);
PdfPTable tableHeaderR = CreateTable(1, PdfPTable.BACKGROUNDCANVAS, new float[] { -1 }, false);
PdfPTable tableHeaderminiR = CreateTable(1, PdfPTable.BACKGROUNDCANVAS, new float[] { 1 }, false);
//
Image mark = imge;
mark.ScaleAbsolute(mark.Width, mark.Height);
mark.Alignment = Image.LEFT_BORDER;
tableHeaderminiR.AddCell(CreateCell(mark, 2, 0));//<<<<<<<<< tableHeaderR.AddCell(CreateCell(tableHeaderminiR, -1, 0));//<<<<<<<<
tableAll.AddCell(CreateCell(tableHeaderR, -1, 0));//<<<<<<<< document.Add(tableAll);
document.Close();
ShowTips.showTips(" 打印完成!文件保存在:" + filePath); }
public PdfPTable CreateTable(int numCol, int HorizontalAlignmentStyle, float[] Width, bool isLockedWidth)
{
PdfPTable tableAll = new PdfPTable(numCol);
tableAll.HorizontalAlignment = HorizontalAlignmentStyle;
if (Width[0] != -1)
{
tableAll.SetTotalWidth(Width);
}
tableAll.LockedWidth = isLockedWidth;
return tableAll;
} public PdfPCell CreateCell(PdfPTable table, int rowspan, int borderStyle)
{
PdfPCell cell = new PdfPCell(table);
if (borderStyle != -1 && borderStyle >= 0)
{
cell.Border = borderStyle;
}
if (rowspan != -1)
{
cell.Rowspan = rowspan;
} cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
cell.VerticalAlignment = PdfPCell.ALIGN_LEFT | PdfPCell.ALIGN_CENTER;
return cell;
} public PdfPCell CreateCell(Image image, int rowspan, int borderStyle)
{
PdfPCell cell = new PdfPCell(image);
if (borderStyle != -1 && borderStyle >= 0)
{
cell.Border = borderStyle;
}
if (rowspan != -1)
{
cell.Rowspan = rowspan;
}
cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
cell.VerticalAlignment = PdfPCell.ALIGN_BASELINE;
return cell;
} public PdfPCell CreateCell(string text, int rowspan, int borderStyle, iTextSharp.text.Font cellFont)
{
PdfPCell cell = new PdfPCell(new Phrase(text, cellFont));
if (borderStyle != -1 && borderStyle>=0)
{
cell.Border = borderStyle;
}
if (rowspan != -1)
{
cell.Rowspan = rowspan;
} cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
return cell;
}
/// <summary> 只在详细信息的时候用
PdfPCell CreateGoodsInformationCell(PdfInformation mInfo)
{ string text = mInfo.mName +"\n";
iTextSharp.text.Font font =CreateFont(20, iTextSharp.text.Font.NORMAL);
PdfPCell cell = new PdfPCell(new Phrase(text,font));
cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
cell.VerticalAlignment = PdfPCell.ALIGN_LEFT; return cell; }
/// </param>/ public void CellAddTable(PdfPCell cell,PdfPTable table)
{
cell.AddElement(table);
}
public iTextSharp.text.Font CreateFont(float fontSize, int fontStyle)
{
BaseFont bfChinese = BaseFont.CreateFont( Application.dataPath + "/font/MSYH.TTF",
BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED ); return new iTextSharp.text.Font(bfChinese, fontSize, fontStyle);
} }
下面是调用:
using UnityEngine;
using System.Collections; public class PrintPdf : MonoBehaviour { //需要将这几个摄像头绑定对应的
PDFMakerManager mPdfManager;
public Camera m3DCamera;
public Camera mHightCam;
public GameObject mObj;
public Camera mInfoCamera;
string mPdfPath; public GameObject[] Texts = new GameObject[6];
public string[] Textstext;
PdfUserInformation mPDFUserInfo;
// Use this for initialization
void Start () { Textstext = new string[6];
mPdfManager = new PDFMakerManager();
mPDFUserInfo = new PdfUserInformation(); } // Update is called once per frame
void Update () { } void OnGUI()
{ if (GUI.Button(new Rect(100, 100, 100, 40), "PdfInfoPrint"))
{ PrintInfoPdf();
}
if (GUI.Button(new Rect(300, 100, 100, 40), "PdfInfoImgPrint"))
{ PrintInfoImgPdf();
}
if (GUI.Button(new Rect(400, 100, 100, 40), "PdfHightPrint"))
{
PrintHightImgPdf();
}
} // 还需要把摄像头绑定对
// 把写字的物体绑定对
// 在 调用函数 需要 找对 位置
public void getText()
{
for (int i = 0; i < Texts.Length; i++)
{
Textstext[i] = Texts[i].GetComponent<UILabel>().text;
}
mPDFUserInfo.goodsName = Textstext[0];
mPDFUserInfo.youzhanType = Textstext[1];
mPDFUserInfo.cvsLevel = Textstext[2];
mPDFUserInfo.writeTime = Textstext[3];
mPDFUserInfo.creater = Textstext[4];
mPDFUserInfo.creatTime = Textstext[5]; }
//打印 左边字 右边表格
public void PrintInfoPdf()
{
getText();
mPdfManager.PrintGoodsInformations(mPdfPath,mPDFUserInfo);
}
//打印 信息 图片
public void PrintInfoImgPdf()
{ getText();
mPdfPath = Application.dataPath + "/Print/InfoImg"
+ System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".pdf"; mPdfManager.PrintGoodsInformations(mPdfPath, mInfoCamera, mObj, mPDFUserInfo); } //打印 图
public void PrintHightImgPdf()
{
mPdfPath = Application.dataPath + "/Print/Top"
+ System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".pdf";
mPdfManager.PrintImgPdf(mPdfPath, mHightCam); }
}
注:如需转载,请注明出处,