画布类实现:
1 public class GrapTool 2 { 3 4 public GrapTool() 5 { 6 } 7 8 private Graphics g; 9 protected Graphics G 10 { 11 get 12 { 13 if (g==null) 14 { 15 Bitmap bm = new Bitmap(800, 15); 16 g= Graphics.FromImage(bm); 17 } 18 return g; 19 } 20 } 21 private Font f; 22 protected Font F 23 { 24 get 25 { 26 if (f==null) 27 { 28 f=new Font("Arial", 8); 29 } 30 return f; 31 } 32 } 33 34 35 public float GetWidth(string src) 36 { 37 SizeF siF= G.MeasureString(src,F); 38 return siF.Width; 39 } 40 }
itextsharp实现:
1 BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA , BaseFont.CP1252 , BaseFont.NOT_EMBEDDED); 2 float pageLen = bf.getWidthPoint("abc 123 abc 123" , 12);