how to create pdf by itextsharp

  1using System;
  2using System.IO;
  3using System.Drawing;
  4using System.Collections;
  5using iTextSharp.text;
  6using iTextSharp.text.pdf;
  7
  8namespace BluePoint.ERM.BPViewerCore
  9{
 10    public class PDFHelper
 11    {
 12        private static Int64 _traceLevel;
 13        private static string m_FontSrc ;
 14        private iTextSharp.text.Document _document;
 15        private PdfWriter _writer;
 16        // closed
 17        //private string _PDFFullFileName;
 18
 19        public void SavePDFDocument()
 20        {
 21            if (_document != null)
 22            {
 23                _document.Close();
 24            }

 25        }

 26        public PDFHelper(string pdfFullFileName)
 27        {
 28             _document = new iTextSharp.text.Document();
 29            try
 30            {
 31                if(File.Exists(pdfFullFileName))
 32                {
 33                    File.Delete(pdfFullFileName);
 34                }

 35                _writer = PdfWriter.GetInstance(_document, new FileStream(pdfFullFileName,FileMode.Create));
 36            }

 37            catch(Exception e)
 38            {
 39                throw new Exception("can't create iTextSharp document object" + e.ToString ());
 40            }

 41
 42        }

 43        public static string FontSrc
 44        {
 45            get
 46            {
 47                if(PDFHelper.m_FontSrc == null)
 48                {
 49                    PDFHelper.m_FontSrc = "c:\\windows\\fonts\\COUR.TTF";
 50                }

 51                return PDFHelper.m_FontSrc;
 52            }

 53            set
 54            {
 55                PDFHelper.m_FontSrc = value;
 56            }

 57        }

 58
 59        public PDFHelper()
 60        {
 61            //
 62            // TODO: Add constructor logic here
 63            //
 64        }

 65
 66        public static void SetTraceLevel(Int64 val) 
 67        {
 68            _traceLevel = val;
 69        }

 70
 71        public static void Log(string s) 
 72        {
 73            if((_traceLevel & 8> 0
 74            {
 75                System.Diagnostics.Trace.WriteLine("BPVR: " + s);
 76            }

 77        }

 78
 79        public void CreateSinglePagePDF(PageInfo pageInfo)
 80        {
 81            StopWatch sw = new StopWatch();
 82            try
 83            {
 84                sw.Reset();
 85                _document.SetPageSize (new iTextSharp.text.Rectangle(pageInfo.Setting.PageSizeWidth,
 86                    pageInfo.Setting.PageSizeHeight));
 87                _document.SetMargins(pageInfo.Setting.PageMarginLeft,pageInfo.Setting.PageMarginRight,
 88                    pageInfo.Setting.PageMarginTop,pageInfo.Setting.PageMarginBottom);
 89            
 90                Log("BPVR: time elapsed to setup document parameters: " + sw.ToMilliSeconds());
 91
 92                if(pageInfo.ImgFileName != "")
 93                {
 94                    sw.Reset();
 95                    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(pageInfo.ImgFileName);
 96                    Log("BPVR: time elapsed to load image into iTextSharp: " + sw.ToMilliSeconds());
 97
 98                    sw.Reset();
 99                    img.ScaleAbsolute(pageInfo.Setting.ImgWidht,pageInfo.Setting.ImgHeight);
100
101                    Watermark watermark = new Watermark(img,pageInfo.Setting.WatermarkOffsetX ,
102                        pageInfo.Setting.WatermarkOffsetY);
103
104            
105                    _document.Add(watermark);
106                    Log("BPVR: time elapsed to add image to document: " + sw.ToMilliSeconds());
107                }

108                else
109                {
110                    _document.RemoveWatermark();
111                }

112
113                sw.Reset();
114                _document.NewPage();
115                Log("BPVR: time elapsed to create new page: " + sw.ToMilliSeconds());
116
117                sw.Reset();
118                if(!_document.IsOpen())
119                {
120                    _document.Open();                    
121                }

122                Log("BPVR: time elapsed to open document: " + sw.ToMilliSeconds());
123
124                sw.Reset();
125                System.Drawing.Font testFont = new System.Drawing.Font("courier new"8);
126                Log("BPVR: time elapsed to create font in dotNet: " + sw.ToMilliSeconds());
127                //
128
129                sw.Reset();
130                BaseFont bf = BaseFont.CreateFont(FontSrc,"",true);
131                Log("BPVR: time elapsed to create font from ttf file: " + sw.ToMilliSeconds());
132
133                _writer.DirectContent.SetLeading( (float)((testFont.GetHeight()/pageInfo.Setting.DPI*72)));
134            
135                sw.Reset();
136                _writer.DirectContent.BeginText();
137                _writer.DirectContent.SetTextMatrix(pageInfo.Setting.PageMarginLeft, 
138                    pageInfo.Setting.PageSizeHeight - pageInfo.Setting.PageMarginTop);
139                _writer.DirectContent.SetFontAndSize(bf, pageInfo.Setting.FontSize);
140
141                using(StringReader sr = new StringReader(pageInfo.TextFileName))
142                {
143                    while(sr.Peek() >0)
144                    {
145                        _writer.DirectContent.SetCharacterSpacing(0.15f*pageInfo.Setting.FontSize/8);
146                        _writer.DirectContent.NewlineShowText(sr.ReadLine());
147                    }

148                }

149                
150                
151//                using(StreamReader sr = File.OpenText(pageInfo.TextFileName)) 
152//                {
153//                    while(sr.Peek() > 0) 
154//                    {
155//                        _writer.DirectContent.SetCharacterSpacing(0.15f*pageInfo.Setting.FontSize/8);
156//                    
157//                        _writer.DirectContent.NewlineShowText(sr.ReadLine());
158//                    }
159//                }
160                //_writer.DirectContent.SetCharacterSpacing(0.15f*pageInfo.Setting.FontSize/8);
161                //_writer.DirectContent.NewlineShowText(pageInfo.TextFileName);
162
163                _writer.DirectContent.EndText();
164                Log("BPVR: time elapsed to write text: " + sw.ToMilliSeconds());
165
166            }

167            catch(Exception e)
168            {
169                throw new Exception("can't create single PDF page." + e.ToString());
170            }

171        }

172        
173
174    }

175
176
177    public class PageInfo
178    {
179        public PageInfo()
180        {
181        }

182        public PageInfo(string imgFileName,string textFileName,
183            PageSetting pageSetting )
184        {
185            this.m_ImgFileName = imgFileName;
186            this.m_TextFileName = textFileName;
187            this.Setting = pageSetting;
188        }

189        public static PageInfo GetInstance(string imgFileName,string textFileName,
190            PageSetting pageSetting)
191        {
192            return new PageInfo(imgFileName,textFileName,pageSetting);
193        }

194        private string m_ImgFileName;
195
196        public string ImgFileName
197        {
198            get return m_ImgFileName; }
199            set { m_ImgFileName = value; }
200        }

201        private string m_TextFileName;
202
203        public string TextFileName
204        {
205            get return m_TextFileName; }
206            set { m_TextFileName = value; }
207        }

208
209        private PageSetting m_Setting;
210
211        public PageSetting Setting
212        {
213            get return m_Setting; }
214            set { m_Setting = value; }
215        }

216        
217        
218
219    }

220
221    public class PageSetting
222    {
223        public static PageSetting Create(int pWith, int pHeight,
224            int pMarginL, int pMarginR, int pMarginT, int pMarginB,
225            int imgW, int imgH,
226            int offsetX, int offsetY,
227            int fontSize,
228            int dpi)
229        {
230            return new PageSetting(pWith, pHeight, pMarginL, pMarginR, pMarginT, pMarginB,
231                imgW, imgH,
232                offsetX, offsetY,
233                fontSize, dpi);
234        }

235        public static PageSetting Create(int pWith, int pHeight,
236            int pMarginL, int pMarginR, int pMarginT, int pMarginB,
237            int imgW, int imgH,
238            int offsetX, int offsetY,
239            int fontSize)
240        {
241            return new PageSetting(pWith, pHeight, pMarginL, pMarginR, pMarginT, pMarginB,
242                imgW, imgH,
243                offsetX, offsetY,
244                fontSize);
245        }

246
247        private PageSetting(double pWith,double pHeight,
248            int pMarginL,int pMarginR,int pMarginT,int pMarginB,
249            int imgW,int imgH,
250            int offsetX,int offsetY,
251            int fontSize,
252            int dpi)
253        {
254            m_PageSizeWidth = pWith;
255            m_PageSizeHeight = pHeight;
256            m_PageMarginLeft = pMarginL;
257            m_PageMarginRight = pMarginR;
258            m_PageMarginTop = pMarginT;
259            m_PageMarginBottom = pMarginB;
260            m_ImgWidht = imgW;
261            m_ImgHeight = imgH;
262            m_WatermarkOffsetX = offsetX;
263            m_WatermarkOffsetY = offsetY;
264            m_fontSize = fontSize;
265            m_DPI = (float)dpi;
266            
267        }

268        private PageSetting(double pWith, double pHeight,
269            int pMarginL, int pMarginR, int pMarginT, int pMarginB,
270            int imgW, int imgH,
271            int offsetX, int offsetY,
272            int fontSize)
273        {
274            m_PageSizeWidth = pWith;
275            m_PageSizeHeight = pHeight;
276            m_PageMarginLeft = pMarginL;
277            m_PageMarginRight = pMarginR;
278            m_PageMarginTop = pMarginT;
279            m_PageMarginBottom = pMarginB;
280            m_ImgWidht = imgW;
281            m_ImgHeight = imgH;
282            m_WatermarkOffsetX = offsetX;
283            m_WatermarkOffsetY = offsetY;
284            m_fontSize = fontSize;
285
286        }

287        
288        private float m_DPI = 96.0f;
289        private double m_PageSizeWidth;
290
291        public float PageSizeWidth
292        {
293            get return (float)m_PageSizeWidth * 72; }
294        }

295        private double m_PageSizeHeight;
296
297        public float PageSizeHeight
298        {
299            get return (float)m_PageSizeHeight * 72; }
300            
301        }

302        private int m_PageMarginLeft;
303
304        public float PageMarginLeft
305        {
306            get return m_PageMarginLeft/DPI*72; }
307    
308        }

309        private int m_PageMarginRight;
310
311        public float PageMarginRight
312        {
313            get return m_PageMarginRight/DPI*72; }
314          
315        }

316        private int m_PageMarginTop;
317
318        public float PageMarginTop
319        {
320            get return m_PageMarginTop / DPI * 72; }
321           
322        }

323        private int m_PageMarginBottom;
324
325        public float PageMarginBottom
326        {
327            get return m_PageMarginBottom / DPI * 72; }
328           
329        }

330        private int m_ImgWidht;
331
332        public float ImgWidht
333        {
334            get return m_ImgWidht / DPI * 72; }
335           
336        }

337        private int m_ImgHeight;
338
339        public float ImgHeight
340        {
341            get return m_ImgHeight / DPI * 72; }
342            
343        }

344        private int m_WatermarkOffsetX;
345
346        public float  WatermarkOffsetX
347        {
348            get return (float)(m_WatermarkOffsetX / DPI * 72); }
349           
350        }

351        private int m_WatermarkOffsetY;
352
353        public float WatermarkOffsetY
354        {
355            get
356            
357                return (float)((m_PageSizeHeight * (int)DPI - m_ImgHeight - m_WatermarkOffsetY) / (int)DPI * 72);
358            }

359        }

360        private int m_fontSize;
361
362        public int FontSize
363        {
364            get return m_fontSize; }
365            set { m_fontSize = value; }
366        }

367
368
369        public float DPI
370        {
371            get return m_DPI; }
372            set { m_DPI = value; }
373        }

374
375
376    }

377
378}

379
原文地址:https://www.cnblogs.com/umlchina/p/1125453.html