|
| 1 | +using UnityEngine; |
| 2 | +using System.Collections; |
| 3 | +using System.IO; |
| 4 | +using System; |
| 5 | + |
| 6 | +public class PDFGenerater : MonoBehaviour { |
| 7 | + |
| 8 | + iTextSharp.text.Document pdfdoc; |
| 9 | + iTextSharp.text.Image pdfImg; |
| 10 | + iTextSharp.text.pdf.PdfWriter pdfwriter; |
| 11 | + |
| 12 | + iTextSharp.text.Rectangle currentPageSize = new iTextSharp.text.Rectangle (7440,12360);//iTextSharp.text.PageSize.A0; |
| 13 | + |
| 14 | + |
| 15 | + int gridSideLength = 20;// side length of each small square grid |
| 16 | + int blockRowInterval = 40;//interval between two BlockRow |
| 17 | + int gridCountInBlockRowVertical = 40; |
| 18 | + int gridCountInBlockRowHorizontal;// = currentPageSize.Width / gridSideLength; |
| 19 | + int firstBlockRowOffsetInVertical = 100;//first BlockRow move down to give space for Text Head writing |
| 20 | + |
| 21 | + |
| 22 | + int blockRowHeight; // = gridSideLength * gridCountInBlockRowVertical; |
| 23 | + int blockRowCount; // = currentPageSize.Height / (blockRowHeight + blockRowInterval); means how many BlockRow each page contains |
| 24 | + |
| 25 | + |
| 26 | + int blockRowIndex = 0; |
| 27 | + float currentBaseLine; |
| 28 | + |
| 29 | + // means how many raw data will be contained within a BlockRow |
| 30 | + int rawDataCountInBlockRow = 0;// = gridCountInBlockRowHorizontal * 40ms * 512 / 1000ms; |
| 31 | + //a small square == 40ms in X-axle, 1 second == 512 raw samples |
| 32 | + float rawDataInterval = 0;// = currentPageSize.Width / rawDataCountInBlockRow; |
| 33 | + int rawDataIndex = 0; // if rawDataIndex >= rawDataCountInBlockRow; blockRowIndex++ to change to next BlockRow |
| 34 | + |
| 35 | + |
| 36 | + int maxRawData = 13990;//a small square == 0.1mv in Y-axle, 0.1mv == 699.5; there are 20 small square above/under baseLine. |
| 37 | + float scaledRawData = 0;// = (rawData * (blockRowHeight / 2)) / maxRawData; zoom RawData to small, less that BlockRowHeight / 2; |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + // Use this for initialization |
| 42 | + void Start () { |
| 43 | + |
| 44 | + |
| 45 | + //CreateFromFolder (Application.persistentDataPath); |
| 46 | + CreateFromRawdataFile ("/Volumes/Work/RawData.txt","/Volumes/Work/outpdf.pdf"); |
| 47 | + } |
| 48 | + |
| 49 | + void CreateFromRawdataFile(string sourceTxtPath, string destinationPDFPath){ |
| 50 | + pdfdoc = new iTextSharp.text.Document (); |
| 51 | + pdfdoc.SetPageSize (currentPageSize); |
| 52 | + |
| 53 | + gridCountInBlockRowHorizontal = (int)(currentPageSize.Width / gridSideLength); |
| 54 | + blockRowHeight = gridSideLength * gridCountInBlockRowVertical; |
| 55 | + blockRowCount = (int)(currentPageSize.Height / (blockRowHeight + blockRowInterval)); |
| 56 | + rawDataCountInBlockRow = (int)(gridCountInBlockRowHorizontal * 40 * 512 / 1000); |
| 57 | + rawDataInterval = currentPageSize.Width / rawDataCountInBlockRow; |
| 58 | + |
| 59 | + Debug.Log ("gridCountInBlockRowHorizontal:" + gridCountInBlockRowHorizontal); |
| 60 | + Debug.Log ("rawDataCountInBlockRow" + rawDataCountInBlockRow); |
| 61 | + |
| 62 | + if (File.Exists (destinationPDFPath)) { |
| 63 | + File.Delete(destinationPDFPath); |
| 64 | + } |
| 65 | + |
| 66 | + pdfwriter = iTextSharp.text.pdf.PdfWriter.GetInstance (pdfdoc,new FileStream(destinationPDFPath,FileMode.CreateNew)); |
| 67 | + |
| 68 | + pdfdoc.Open (); |
| 69 | + |
| 70 | + iTextSharp.text.pdf.PdfContentByte cb = pdfwriter.DirectContent; |
| 71 | + |
| 72 | + pdfdoc.NewPage (); |
| 73 | + blockRowIndex = 0; |
| 74 | + rawDataIndex = 0; |
| 75 | + |
| 76 | + iTextSharp.text.pdf.BaseFont font = iTextSharp.text.pdf.BaseFont.CreateFont(); |
| 77 | + cb.SetFontAndSize (font,60); |
| 78 | + cb.BeginText (); |
| 79 | + cb.SetTextMatrix (100,currentPageSize.Height - 50); |
| 80 | + cb.ShowText ("Test String ......."); |
| 81 | + cb.EndText (); |
| 82 | + |
| 83 | + System.IO.StreamReader reader = new StreamReader (sourceTxtPath); |
| 84 | + string theLine; |
| 85 | + int rawData; |
| 86 | + |
| 87 | + while(!reader.EndOfStream){ |
| 88 | + theLine = reader.ReadLine(); |
| 89 | + try{ |
| 90 | + rawData = int.Parse(theLine); |
| 91 | + }catch(Exception e){ |
| 92 | + Debug.Log("Exception"); |
| 93 | + continue; |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + rawData = rawData > maxRawData ? maxRawData : rawData; |
| 98 | + rawData = rawData < -maxRawData ? -maxRawData : rawData; |
| 99 | + scaledRawData = (rawData * (blockRowHeight / 2)) / maxRawData; |
| 100 | + |
| 101 | + if(rawDataIndex == 0){ |
| 102 | + currentBaseLine = currentPageSize.Height - (firstBlockRowOffsetInVertical + blockRowIndex * (blockRowHeight + blockRowInterval) + (blockRowHeight / 2)); |
| 103 | + |
| 104 | + cb.SetColorStroke (iTextSharp.text.BaseColor.RED); |
| 105 | + |
| 106 | + //draw horizontal lines |
| 107 | + for(int i = 0; i <= gridCountInBlockRowVertical; i++){ |
| 108 | + if(i % 5 == 0){ |
| 109 | + cb.SetLineWidth (2.5f); |
| 110 | + }else{ |
| 111 | + cb.SetLineWidth (0.5f); |
| 112 | + } |
| 113 | + cb.MoveTo(0,currentPageSize.Height - (firstBlockRowOffsetInVertical + blockRowIndex * (blockRowHeight + blockRowInterval) + i * gridSideLength)); |
| 114 | + cb.LineTo(currentPageSize.Width,currentPageSize.Height - (firstBlockRowOffsetInVertical + blockRowIndex * (blockRowHeight + blockRowInterval) + i * gridSideLength)); |
| 115 | + cb.Stroke(); |
| 116 | + } |
| 117 | + |
| 118 | + //draw vertical lines |
| 119 | + for(int j = 0; j <= gridCountInBlockRowHorizontal; j++){ |
| 120 | + if(j % 5 == 0){ |
| 121 | + cb.SetLineWidth (2.5f); |
| 122 | + }else{ |
| 123 | + cb.SetLineWidth (0.5f); |
| 124 | + } |
| 125 | + cb.MoveTo(j * gridSideLength,currentPageSize.Height - (firstBlockRowOffsetInVertical + blockRowIndex * (blockRowHeight + blockRowInterval))); |
| 126 | + cb.LineTo(j * gridSideLength,currentPageSize.Height - (firstBlockRowOffsetInVertical + blockRowIndex * (blockRowHeight + blockRowInterval) + blockRowHeight)); |
| 127 | + cb.Stroke(); |
| 128 | + } |
| 129 | + //prepare to draw ECG |
| 130 | + cb.SetLineWidth (1.5f); |
| 131 | + cb.SetColorStroke (iTextSharp.text.BaseColor.BLACK); |
| 132 | + |
| 133 | + cb.MoveTo (0,currentBaseLine); |
| 134 | + } |
| 135 | + |
| 136 | + cb.LineTo (rawDataIndex * rawDataInterval,currentBaseLine + scaledRawData); |
| 137 | + rawDataIndex++; |
| 138 | + if(rawDataIndex >= rawDataCountInBlockRow){ |
| 139 | + cb.Stroke(); |
| 140 | + rawDataIndex = 0; |
| 141 | + blockRowIndex++; |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + cb.Stroke(); |
| 146 | + reader.Close (); |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + pdfdoc.Dispose(); |
| 156 | + System.Diagnostics.Process.Start (destinationPDFPath); |
| 157 | + } |
| 158 | + |
| 159 | + void CreateFromFolder(string folderPath){ |
| 160 | + pdfdoc = new iTextSharp.text.Document (); |
| 161 | + pdfdoc.SetPageSize (iTextSharp.text.PageSize.A0); |
| 162 | + |
| 163 | + Debug.Log ("000000"); |
| 164 | + |
| 165 | + string pdfPath = Path.Combine (folderPath,"output.pdf"); |
| 166 | + if (File.Exists (pdfPath)) { |
| 167 | + |
| 168 | + File.Delete(pdfPath); |
| 169 | + } |
| 170 | + Debug.Log ("1111111"); |
| 171 | + |
| 172 | + pdfwriter = iTextSharp.text.pdf.PdfWriter.GetInstance (pdfdoc,new FileStream(pdfPath,FileMode.CreateNew)); |
| 173 | + |
| 174 | + Debug.Log ("22222"); |
| 175 | + pdfdoc.Open (); |
| 176 | + |
| 177 | + pdfdoc.NewPage (); |
| 178 | + |
| 179 | + string[] paths = Directory.GetFiles (folderPath); |
| 180 | + |
| 181 | + int rows; |
| 182 | + int columns; |
| 183 | + |
| 184 | + int rowIndex = 1; |
| 185 | + int pageIndex = 1; |
| 186 | + |
| 187 | + float pageWidth = iTextSharp.text.PageSize.A0.Width; |
| 188 | + float pageHeight = iTextSharp.text.PageSize.A0.Height; |
| 189 | + |
| 190 | + pdfImg = iTextSharp.text.Image.GetInstance(paths[0]); |
| 191 | + |
| 192 | + rows = (int)(pageHeight/pdfImg.Height); |
| 193 | + columns = (int)(pageWidth/pdfImg.Width); |
| 194 | + Debug.Log ("Rows:" + rows); |
| 195 | + Debug.Log ("Columns:" + columns); |
| 196 | + |
| 197 | + for(int i = 0; i < paths.Length; i++){ |
| 198 | + if(paths[i].EndsWith(".png")){ |
| 199 | + Debug.Log(paths[i]); |
| 200 | + pdfImg = iTextSharp.text.Image.GetInstance(paths[i]); |
| 201 | + |
| 202 | + if(i == columns * rows * pageIndex){ |
| 203 | + pageIndex++; |
| 204 | + Debug.Log("pageIndex:" + pageIndex); |
| 205 | + |
| 206 | + pdfdoc.NewPage(); |
| 207 | + rowIndex = 1; |
| 208 | + Debug.Log("rowIndex:" + rowIndex); |
| 209 | + }else if(i != 0 && i % columns == 0){ |
| 210 | + rowIndex++; |
| 211 | + Debug.Log("rowIndex:" + rowIndex); |
| 212 | + } |
| 213 | + pdfImg.SetAbsolutePosition(pdfImg.Width * (i % columns),pageHeight - pdfImg.Height * rowIndex); |
| 214 | + |
| 215 | + pdfdoc.Add(pdfImg); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + pdfdoc.Dispose(); |
| 220 | + } |
| 221 | + |
| 222 | + // Update is called once per frame |
| 223 | + void Update () { |
| 224 | + |
| 225 | + } |
| 226 | + |
| 227 | +} |
0 commit comments