unity3d导出pdf




unity生成pdf格式,首先需要导入iTextSharp.dll ,下面是我写的一些方法,可以直接用.直接贴代码,

[csharp]  view plain  copy
 print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.IO;  
  5. using iTextSharp.text.pdf;  
  6. using iTextSharp.text;  
  7. using System.Text.RegularExpressions;  
  8. using System.Data.OracleClient;  
  9. using System;  
  10.   
  11. //需要用到的数据结构  
  12. public class PdfUserInformation   
  13. {  
  14.     public string goodsName;     
  15.     public string youzhanType;   
  16.     public string cvsLevel;      
  17.     public string writeTime;      
  18.     public string creater;        
  19.     public string creatTime;     
  20. };  
  21. //数据结构  
  22. public  class PdfInformation  
  23. {  
  24.     public string mID;  
  25.     public string mName;  
  26.     public string mReserve;   
  27.     //  ....  
  28. };  
  29.   
  30. public class PDFMakerManager  {  
  31.       
  32.     public static PDFMakerManager PDFInstance;  
  33.     public static PDFMakerManager createPDFMakerManager()  
  34.     {  
  35.         if(PDFInstance==null)  
  36.         {  
  37.             PDFInstance = new PDFMakerManager ();  
  38.         }  
  39.           
  40.         return PDFInstance;  
  41.     }  
  42.       
  43.     //获取物品的子物体名字 通过子物体的名字获取物体  返回物体的链表  
  44.     public List<PdfInformation> getObjChildsName(Transform mParentObj)   
  45.     {  
  46.          
  47.         if(mParentObj!=null)  
  48.         {  
  49.             List<string> mChildObjs = new List<string>();  
  50.             List<PdfInformation> mInfoList = new List<PdfInformation>();  
  51.             foreach (Transform mChildTransform in mParentObj)  
  52.             {  
  53.                 mChildObjs.Add(mChildTransform.gameObject.name);  
  54.                 Debug.Log(mChildTransform.gameObject.name);  
  55.             }  
  56.             //获取  数据库  信息  
  57.             getPdfDatasFromDB();  
  58.             //通过子物体的名字 获取物体信息  
  59.             for (int i = 0; i < mChildObjs.Count; i++)  
  60.             {  
  61.                 mInfoList.Add(mDirectoryData[mChildObjs[i]]);  
  62.             }  
  63.             return mInfoList;  
  64.         }  
  65.         return null;  
  66.     }  
  67.     //每行五个  还有问题,由于需求以及时间关系,没时间去管了   谁看到了可以帮忙解决下,.thanks  
  68.     List<PdfInformation> mGoodsInfoList = new List<PdfInformation>();  
  69.     public List<List<PdfInformation>> getAllGoods(List<PdfInformation> mList)   
  70.     {  
  71.         List<List<PdfInformation>> mGoodsAll = new List<List<PdfInformation>>();  
  72.         List<List<PdfInformation>> mGoods = new List<List<PdfInformation>>();  
  73.   
  74.         for (int i = 1; i <= mList.Count;i++ )  
  75.         {  
  76.             mGoodsInfoList.Add(mList[i-1]);  
  77.              
  78.             if(i%5==0)  
  79.             {  
  80.                  
  81.                 mGoodsAll.Add(mGoodsInfoList);  
  82.                 Debug.Log("xx"+mGoodsInfoList.Count);  
  83.                 mGoodsInfoList.Clear();  
  84.                   
  85.                 for (int j = 0; j < mGoodsAll.Count; j++)  
  86.                 {  
  87.                     Debug.Log("InfoList [[[" + j + "]]]is:" + mGoodsAll[j].Count);  
  88.                 }  
  89.                  
  90.             }else if(i%5!=0&&i==mList.Count)  
  91.             {  
  92.                  mGoodsAll.Add(mGoodsInfoList);  
  93.                  mGoodsInfoList.Clear();  
  94.             }  
  95.         }  
  96.         Debug.Log("InfoList [[[is:" + mGoodsAll.Count);  
  97.         //for (int j = 0; j < mGoodsAll.Count;j++ )  
  98.         //{  
  99.         //    Debug.Log("InfoList [[[" + j + "]]]is:" + mGoodsAll[j].Count);  
  100.         //}  
  101.          
  102.         return mGoodsAll;     
  103.     }  
  104.     //从数据库  获取  PDF 信息  
  105.     Dictionary<string, PdfInformation> mDirectoryData = new Dictionary<string, PdfInformation>();  
  106.     public List<PdfInformation> getPdfDatasFromDB()   
  107.     {  
  108.         List<PdfInformation> mInfoList = new List<PdfInformation>();  
  109.         
  110.         Oracle   mDB = new Oracle ();  
  111.         try   
  112.         {  
  113.             string mSearch = "select * from mdb";  
  114.             mInfoList = mDB.Select(mSearch);  
  115.             mDirectoryData = mDB.getPdfDictionary();  
  116.         }catch(Exception e)  
  117.         {  
  118.             mDB.Close();  
  119.             Debug.Log("sql error");  
  120.         }  
  121.        return mInfoList;  
  122.     }  
  123.   
  124. //  / pdf 行  
  125.     public PdfPTable CreateRankGoodsInforMationTable(List<List<PdfInformation>> mList)  
  126.     {  
  127.          PdfPTable mTable=new PdfPTable(1);  
  128.           
  129.          mTable.SetTotalWidth(new float[]{300});  
  130.            
  131.           
  132.          mTable.HorizontalAlignment = PdfPTable.LINECANVAS;  
  133.           
  134.          if(mList.Count>0)  
  135.          {  
  136.              for(int i=0;i<mList.Count;i++)  
  137.              {  
  138.                  Debug.Log("xx"+i+"xxxxxx:"+mList[i].Count);  
  139.                 // mTable.AddCell(CreateCell(CreateRowGoodsInforMationTable2(mList[i]),-1,0));  
  140.              }  
  141.               
  142.          }  
  143.   
  144.          mTable.LockedWidth = true;  
  145.          return mTable;  
  146.     }  
  147. //  
  148.     //pdf 列   
  149.      PdfPTable CreateRowGoodsInforMationTable2(System.Collections.Generic.List<PdfInformation> mList)  
  150.     {  
  151.         PdfPTable mTable =new PdfPTable (mList.Count);  
  152.         System.Collections.Generic.List<float> Width = new System.Collections.Generic.List<float> ();  
  153.         if(mList.Count>0)  
  154.         {  
  155.             for(int i=0;i<mList.Count;i++)  
  156.             {  
  157.                 Width.Add(80f);  
  158.             }  
  159.         }  
  160.         float []mWidth = Width.ToArray();  
  161.         mTable.HorizontalAlignment = PdfPTable.BACKGROUNDCANVAS;  
  162.           
  163.         if (mWidth[0] != -1)  
  164.         {  
  165.             mTable.SetTotalWidth(mWidth);  
  166.         }  
  167.         mTable.LockedWidth = true;  
  168.         for(int i=0;i<mList.Count;i++)  
  169.         {  
  170.               
  171.             mTable.AddCell(CreateGoodsInformationCell(mList[i]));  
  172.         }  
  173.         mList.Clear();  
  174.         return mTable;  
  175.     }  
  176.   
  177.     //pdf  图 暂时没用  
  178.      byte[] GoodsImgPrint(Camera SmallCamera,GameObject TakeHuoJia)   
  179.     {  
  180.         int swidth = (int)(SmallCamera.WorldToScreenPoint(TakeHuoJia.transform.position + TakeHuoJia.renderer.bounds.size).x - SmallCamera.WorldToScreenPoint(TakeHuoJia.transform.position - TakeHuoJia.renderer.bounds.size).x);  
  181.   
  182.         RenderTexture rt = new RenderTexture(Screen.width, resHeight, 72);  
  183.         SmallCamera.pixelRect = new Rect(0, 0, Screen.width, Screen.height);  
  184.         SmallCamera.targetTexture = rt;  
  185.         Texture2D screenShot = new Texture2D(swidth + 100, resHeight, TextureFormat.ARGB32, false);  
  186.         SmallCamera.Render();  
  187.         RenderTexture.active = rt;  
  188.   
  189.         screenShot.ReadPixels(new Rect(SmallCamera.WorldToScreenPoint(TakeHuoJia.transform.position - TakeHuoJia.renderer.bounds.size).x, 0, swidth + 100, resHeight), 0, 0);  
  190.         screenShot.Apply();  
  191.         SmallCamera.targetTexture = null;  
  192.         RenderTexture.active = null//  
  193.         UnityEngine.Object.Destroy(rt);  
  194.         byte[] bytes = screenShot.EncodeToPNG();  
  195.   
  196.         return bytes;  
  197.     }  
  198.      //打印物品详细信息 图 (左边字  右边图 分开的  ,写法一:pdf字和摄像头图)   
  199.      public void PrintGoodsInformations2(string filePath, Camera mCam, GameObject mObj, PdfUserInformation mInfo)  
  200.      {  
  201.          Document document = new Document(new Rectangle(PageSize.A4.Height, PageSize.A4.Width));  
  202.          PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));  
  203.          document.Open();  
  204.   
  205.          PdfPTable tableAll = tableAll = CreateTable(2, PdfPTable.BACKGROUNDCANVAS, new float[] { document.PageSize.Width / 2 - 50, document.PageSize.Width / 2 - 50 }, true);  
  206.          //  
  207.          PdfPTable tableLeft = PrintGoodsInformations_Left(mInfo);  
  208.   
  209.          Image tableRight = MakeCameraImg(mCam, 512, 512);//Image.GetInstance( GoodsImgPrint(mCam,mObj));  
  210.         
  211.          tableAll.AddCell(CreateCell(tableLeft, -1, 0));  
  212.          tableAll.AddCell(CreateCell(tableRight, -1, 0));  
  213.          document.Add(tableAll);  
  214.          document.Close();  
  215.          ShowTips.showTips("  打印完成!文件保存在:" + filePath);  
  216.      }  
  217.      //打印物品详细信息 图   (左边字  右边图 分开的  ,写法2:摄像头全图,包括字和图)   
  218.     public void PrintGoodsInformations(string filePath, Camera mCam,GameObject mObj,PdfUserInformation mInfo)   
  219.     {  
  220.         Document document = new Document(new Rectangle(PageSize.A4.Height, PageSize.A4.Width));  
  221.         PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));  
  222.         document.Open();  
  223.   
  224.         PdfPTable tableAll = CreateTable(1, PdfPTable.BACKGROUNDCANVAS, new float[] {document.PageSize.Width -100 }, true);  
  225.         //  
  226.         PdfPTable tableLeft = PrintGoodsInformations_Left(mInfo);  
  227.   
  228.         Image tableRight = MakeCameraImg(mCam,680,512);//Image.GetInstance( GoodsImgPrint(mCam,mObj));  
  229.       
  230.        // tableAll.AddCell(CreateCell(tableLeft, -1, 0));  
  231.         tableAll.AddCell(CreateCell(tableRight, -1, 0));  
  232.         document.Add(tableAll);  
  233.         document.Close();  
  234.         ShowTips.showTips("  打印完成!文件保存在:" + filePath);  
  235.     }  
  236.   
  237.   
  238.     //打印物品详细信息  左边字 右边字  
  239.     public void PrintGoodsInformations(string filePath,PdfUserInformation mInfo)   
  240.     {  
  241.         Document document = new Document(new Rectangle(PageSize.A4.Height,PageSize.A4.Width));  
  242.         PdfWriter.GetInstance(document,new FileStream(filePath,FileMode.Create));  
  243.         document.Open();  
  244.   
  245.         PdfPTable tableAll = CreateTable(2, PdfPTable.BACKGROUNDCANVAS, new float[] { document.PageSize.Width / 2 - 50, document.PageSize.Width / 2 - 50 }, true);  
  246.         //  
  247.         PdfPTable tableLeft = PrintGoodsInformations_Left(mInfo);  
  248.   
  249.         //测试数据  ....  
  250.         PdfInformation mInfo1 = new PdfInformation();  
  251.         mInfo1.mName = "aaaa";  
  252.         PdfInformation mInfo2 = new PdfInformation();  
  253.         mInfo2.mName = "bbbbb";  
  254.   
  255.         List<PdfInformation> mInfoList = new List<PdfInformation>();  
  256.         mInfoList.Add(mInfo1);  
  257.         mInfoList.Add(mInfo2);  
  258.   
  259.         PdfInformation mInfo3 = new PdfInformation();  
  260.         mInfo3.mName = "ccccc";  
  261.         PdfInformation mInfo4 = new PdfInformation();  
  262.         mInfo4.mName = "ddddd";  
  263.   
  264.         List<PdfInformation> mInfoList2 = new List<PdfInformation>();  
  265.         mInfoList2.Add(mInfo3);  
  266.         mInfoList2.Add(mInfo4);  
  267.   
  268.         List<List<PdfInformation>> mLLInfo = new List<List<PdfInformation>>();  
  269.         mLLInfo.Add(mInfoList);  
  270.         mLLInfo.Add(mInfoList2);  
  271.   
  272.         //.......  
  273.   
  274.   
  275.         //测试数据  数据库数据  
  276.        // List<List<PdfInformation>> mDBTestList = new List<List<PdfInformation>>();  
  277.         //mDBTestList = getAllGoods(getPdfDatasFromDB());  
  278.   
  279.         //Debug.Log("DBList is:"+mDBTestList.Count);  
  280.         //......  
  281.   
  282.   
  283.         PdfPTable tableRight = CreateRankGoodsInforMationTable(mDBTestList);  
  284.         tableRight.HorizontalAlignment = PdfPTable.BACKGROUNDCANVAS;  
  285.         tableAll.AddCell(CreateCell(tableLeft, -1, 0));  
  286.         tableAll.AddCell(CreateCell(tableRight,-1,0));  
  287.         document.Add(tableAll);  
  288.         document.Close();  
  289.         ShowTips.showTips("  打印完成!文件保存在:" + filePath);  
  290.   
  291.     }  
  292.     //详细信息的左半部分  
  293.     PdfPTable PrintGoodsInformations_Left(PdfUserInformation mInfo)   
  294.     {  
  295.         PdfPTable tableHeaderR = CreateTable(1, PdfPTable.BACKGROUNDCANVAS, new float[] { -1 }, false);  
  296.         PdfPTable tableHeaderminiR = CreateTable(2, PdfPTable.BACKGROUNDCANVAS, new float[] { 1, 3 }, false);  
  297.   
  298.         Image mark = Image.GetInstance(Application.dataPath + "/Icon.png");  
  299.         mark.ScaleAbsolute(250, 250);  
  300.         mark.Alignment = Image.LEFT_BORDER;  
  301.         tableHeaderminiR.AddCell(CreateCell(mark, 2, 0));//<<<<<<<<<  
  302.   
  303.         //tableHeaderminiR.AddCell(CreateCell("商品", -1, 0, CreateFont(32, iTextSharp.text.Font.NORMAL)));//<<<<<<<<  
  304.         tableHeaderminiR.AddCell(CreateCell("标题", -1, 0, CreateFont(32, iTextSharp.text.Font.NORMAL)));//<<<<<<<<  
  305.         tableHeaderminiR.AddCell(CreateCell("xxx", -1, 0, CreateFont(22, iTextSharp.text.Font.NORMAL)));//<<<<<<<<  
  306.   
  307.         PdfPTable tableDataR = CreateTable(1, PdfPTable.BACKGROUNDCANVAS, new float[] { 350 }, true);  
  308.         tableDataR.AddCell(CreateCell("\n", -1, 0, CreateFont(12, iTextSharp.text.Font.NORMAL)));  
  309.         tableDataR.AddCell(CreateCell("名称:\n", -1, -1, CreateFont(22, iTextSharp.text.Font.NORMAL)));  
  310.         tableDataR.AddCell(CreateCell(mInfo.goodsName, -1, -1, CreateFont(20, iTextSharp.text.Font.NORMAL)));  
  311.         tableDataR.AddCell(CreateCell("类型:\n", -1, -1, CreateFont(22, iTextSharp.text.Font.NORMAL)));  
  312.         tableDataR.AddCell(CreateCell(mInfo.youzhanType, -1, -1, CreateFont(20, iTextSharp.text.Font.NORMAL)));  
  313.         tableDataR.AddCell(CreateCell("级别:\n", -1, -1, CreateFont(22, iTextSharp.text.Font.NORMAL)));  
  314.         tableDataR.AddCell(CreateCell(mInfo.cvsLevel, -1, -1, CreateFont(20, iTextSharp.text.Font.NORMAL)));  
  315.         tableDataR.AddCell(CreateCell("时间:\n", -1, -1, CreateFont(22, iTextSharp.text.Font.NORMAL)));  
  316.         tableDataR.AddCell(CreateCell(mInfo.writeTime, -1, -1, CreateFont(20, iTextSharp.text.Font.NORMAL)));  
  317.         tableDataR.AddCell(CreateCell("创建者:", -1, -1, CreateFont(22, iTextSharp.text.Font.NORMAL)));  
  318.         tableDataR.AddCell(CreateCell(mInfo.creater, -1, -1, CreateFont(20, iTextSharp.text.Font.NORMAL)));  
  319.         tableDataR.AddCell(CreateCell("创建时间:\n", -1, -1, CreateFont(22, iTextSharp.text.Font.NORMAL)));  
  320.         tableDataR.AddCell(CreateCell(mInfo.creatTime, -1, -1, CreateFont(20, iTextSharp.text.Font.NORMAL)));  
  321.         tableHeaderR.AddCell(CreateCell(tableHeaderminiR, -1, 0));//<<<<<<<<  
  322.         tableHeaderR.AddCell(CreateCell(tableDataR, -1, 0));  
  323.   
  324.   
  325.         return tableHeaderR;  
  326.     }  
  327.   
  328.   
  329.     //把摄像头视野 制作成png图片  
  330.     private Rect CutRect = new Rect(0, 0, 1, 1);  
  331.     private int resWidth = 710;  
  332.     private int resHeight = 512;  
  333.     private  Image MakeCameraImg(Camera mCam,int width,int height)   
  334.     {  
  335.         Image mImage;  
  336.         RenderTexture rt = new RenderTexture(width, height, 2);  
  337.         mCam.pixelRect = new Rect(0, 0, Screen.width, Screen.height);  
  338.         mCam.targetTexture = rt;  
  339.         Texture2D screenShot = new Texture2D((int)(width * CutRect.width), (int)(height * CutRect.height),  
  340.                                                  TextureFormat.RGB24, false);  
  341.         mCam.Render();  
  342.         RenderTexture.active = rt;  
  343.         screenShot.ReadPixels(new Rect(width * CutRect.x, width * CutRect.y, width * CutRect.width, height * CutRect.height), 0, 0);  
  344.         mCam.targetTexture = null;  
  345.         RenderTexture.active = null;  
  346.         UnityEngine.Object.Destroy(rt);  
  347.         byte[] bytes = screenShot.EncodeToPNG();  
  348.         //string filename = Application.dataPath + "/Print/3D"  
  349.           //                  + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png";  
  350.         //System.IO.File.WriteAllBytes(filename, bytes);  
  351.   
  352.         mImage = Image.GetInstance(bytes);  
  353.         return mImage;  
  354.   
  355.     }  
  356.     //打印图片 (摄像头视野看到的/生成的图片) 成PDF  
  357.     public  void PrintImgPdf(string filePath, Camera mCam)  
  358.     {  
  359.         Document document = new Document(new Rectangle(PageSize.A4.Height, PageSize.A4.Width));  
  360.         PdfWriter.GetInstance(  
  361.          document,  
  362.           new FileStream(filePath, FileMode.Create)  
  363.         );  
  364.           
  365.         document.Open();  
  366.         Image imge;  
  367.         imge = MakeCameraImg(mCam,700,512);  
  368.         PdfPTable tableAll = CreateTable(1, PdfPTable.BACKGROUNDCANVAS, new float[] { document.PageSize.Width -50}, true);  
  369.         PdfPTable tableHeaderR = CreateTable(1, PdfPTable.BACKGROUNDCANVAS, new float[] { -1 }, false);  
  370.         PdfPTable tableHeaderminiR = CreateTable(1, PdfPTable.BACKGROUNDCANVAS, new float[] { 1 }, false);  
  371.         //  
  372.         Image mark = imge;  
  373.         mark.ScaleAbsolute(mark.Width, mark.Height);  
  374.         mark.Alignment = Image.LEFT_BORDER;  
  375.         tableHeaderminiR.AddCell(CreateCell(mark, 2, 0));//<<<<<<<<<  
  376.   
  377.         tableHeaderR.AddCell(CreateCell(tableHeaderminiR, -1, 0));//<<<<<<<<  
  378.         tableAll.AddCell(CreateCell(tableHeaderR, -1, 0));//<<<<<<<<  
  379.   
  380.         document.Add(tableAll);  
  381.         document.Close();  
  382.         ShowTips.showTips("  打印完成!文件保存在:" + filePath);  
  383.   
  384.     }  
  385.     public PdfPTable CreateTable(int numCol, int HorizontalAlignmentStyle, float[] Width, bool isLockedWidth)   
  386.     {  
  387.         PdfPTable tableAll = new PdfPTable(numCol);  
  388.         tableAll.HorizontalAlignment = HorizontalAlignmentStyle;  
  389.         if (Width[0] != -1)  
  390.         {  
  391.             tableAll.SetTotalWidth(Width);  
  392.         }  
  393.         tableAll.LockedWidth = isLockedWidth;  
  394.         return tableAll;  
  395.     }  
  396.   
  397.     public PdfPCell CreateCell(PdfPTable table, int rowspan, int borderStyle)  
  398.     {  
  399.         PdfPCell cell = new PdfPCell(table);  
  400.         if (borderStyle != -1 && borderStyle >= 0)  
  401.         {  
  402.             cell.Border = borderStyle;  
  403.         }  
  404.         if (rowspan != -1)   
  405.         {  
  406.             cell.Rowspan = rowspan;  
  407.         }  
  408.          
  409.         cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;  
  410.         cell.VerticalAlignment = PdfPCell.ALIGN_LEFT | PdfPCell.ALIGN_CENTER;  
  411.         return cell;  
  412.     }  
  413.   
  414.     public  PdfPCell CreateCell(Image image, int rowspan, int borderStyle)  
  415.     {  
  416.         PdfPCell cell = new PdfPCell(image);  
  417.         if (borderStyle != -1 && borderStyle >= 0)  
  418.         {  
  419.             cell.Border = borderStyle;  
  420.         }  
  421.         if (rowspan != -1)  
  422.         {  
  423.             cell.Rowspan = rowspan;  
  424.         }  
  425.         cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;  
  426.         cell.VerticalAlignment =  PdfPCell.ALIGN_BASELINE;  
  427.         return cell;  
  428.     }  
  429.   
  430.     public PdfPCell CreateCell(string text, int rowspan, int borderStyle, iTextSharp.text.Font cellFont)  
  431.     {  
  432.         PdfPCell cell = new PdfPCell(new Phrase(text, cellFont));  
  433.         if (borderStyle != -1 && borderStyle>=0)   
  434.         {  
  435.             cell.Border = borderStyle;  
  436.         }  
  437.         if (rowspan != -1)  
  438.         {  
  439.             cell.Rowspan = rowspan;  
  440.         }  
  441.          
  442.         cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;  
  443.         cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;  
  444.         return cell;  
  445.     }  
  446.     /// <summary>  只在详细信息的时候用  
  447.      PdfPCell CreateGoodsInformationCell(PdfInformation mInfo)  
  448.     {  
  449.   
  450.         string text = mInfo.mName +"\n";  
  451.         iTextSharp.text.Font font =CreateFont(20, iTextSharp.text.Font.NORMAL);  
  452.         PdfPCell cell = new PdfPCell(new Phrase(text,font));  
  453.         cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;  
  454.         cell.VerticalAlignment = PdfPCell.ALIGN_LEFT;  
  455.           
  456.         return cell;  
  457.           
  458.     }  
  459.     /// </param>/  
  460.       
  461.     public  void CellAddTable(PdfPCell cell,PdfPTable table)  
  462.     {  
  463.         cell.AddElement(table);  
  464.     }  
  465.     public iTextSharp.text.Font CreateFont(float fontSize, int fontStyle)  
  466.     {  
  467.           BaseFont bfChinese = BaseFont.CreateFont(  
  468.   
  469.                Application.dataPath + "/font/MSYH.TTF",  
  470.                BaseFont.IDENTITY_H,  
  471.                BaseFont.NOT_EMBEDDED  
  472.   
  473.             );  
  474.   
  475.             return new iTextSharp.text.Font(bfChinese, fontSize, fontStyle);   
  476.     }  
  477.       
  478.       
  479.       
  480. }  
下面是调用:

[csharp]  view plain  copy
 print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class PrintPdf : MonoBehaviour {  
  5.   
  6.   
  7.     //需要将这几个摄像头绑定对应的  
  8.     PDFMakerManager mPdfManager;  
  9.     public Camera m3DCamera;  
  10.     public Camera mHightCam;  
  11.     public GameObject mObj;  
  12.     public Camera mInfoCamera;  
  13.     string mPdfPath;  
  14.   
  15.     public GameObject[] Texts = new GameObject[6];  
  16.     public string[] Textstext;  
  17.     PdfUserInformation mPDFUserInfo;  
  18.     // Use this for initialization  
  19.     void Start () {  
  20.         
  21.         Textstext = new string[6];  
  22.         mPdfManager = new PDFMakerManager();  
  23.         mPDFUserInfo = new PdfUserInformation();  
  24.          
  25.     }  
  26.       
  27.     // Update is called once per frame  
  28.     void Update () {  
  29.       
  30.     }  
  31.      
  32.   
  33.   
  34.     void OnGUI()   
  35.     {  
  36.   
  37.         if (GUI.Button(new Rect(100, 100, 100, 40), "PdfInfoPrint"))  
  38.         {  
  39.             
  40.             PrintInfoPdf();  
  41.         }  
  42.         if (GUI.Button(new Rect(300, 100, 100, 40), "PdfInfoImgPrint"))  
  43.         {  
  44.            
  45.             PrintInfoImgPdf();  
  46.         }  
  47.         if (GUI.Button(new Rect(400, 100, 100, 40), "PdfHightPrint"))  
  48.         {  
  49.             PrintHightImgPdf();  
  50.         }  
  51.     }  
  52.   
  53.     //  还需要把摄像头绑定对    
  54.     //   把写字的物体绑定对  
  55.     //  在  调用函数  需要 找对 位置  
  56.     public void getText()  
  57.     {  
  58.         for (int i = 0; i < Texts.Length; i++)  
  59.         {  
  60.             Textstext[i] = Texts[i].GetComponent<UILabel>().text;  
  61.         }  
  62.         mPDFUserInfo.goodsName = Textstext[0];  
  63.         mPDFUserInfo.youzhanType = Textstext[1];  
  64.         mPDFUserInfo.cvsLevel = Textstext[2];  
  65.         mPDFUserInfo.writeTime = Textstext[3];  
  66.         mPDFUserInfo.creater = Textstext[4];  
  67.         mPDFUserInfo.creatTime = Textstext[5];  
  68.   
  69.     }  
  70.     //打印  左边字 右边表格  
  71.     public void PrintInfoPdf()   
  72.     {  
  73.         getText();  
  74.         mPdfManager.PrintGoodsInformations(mPdfPath,mPDFUserInfo);    
  75.     }  
  76.     //打印  信息 图片   
  77.     public void PrintInfoImgPdf()   
  78.     {  
  79.   
  80.         getText();  
  81.         mPdfPath = Application.dataPath + "/Print/InfoImg"  
  82.                          + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".pdf";  
  83.           
  84.         mPdfManager.PrintGoodsInformations(mPdfPath, mInfoCamera, mObj, mPDFUserInfo);  
  85.   
  86.           
  87.     }  
  88.      
  89.     //打印 图  
  90.     public void PrintHightImgPdf()   
  91.     {  
  92.         mPdfPath = Application.dataPath + "/Print/Top"  
  93.                          + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".pdf";  
  94.         mPdfManager.PrintImgPdf(mPdfPath, mHightCam);  
  95.           
  96.     }  
  97. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值