2014-09-10 2 views
0

Windows에서 PDF를 인쇄 할 때 텍스트의 문자에 따라 글꼴을 선택해야합니다.iTextSharp.text.pdf.FoldSelector for Bold text

iTextSharp FontSelector를 사용하고 싶습니다. 올바른 글꼴이 굵은 글꼴과 일반 글꼴에 대해 다를 수 있습니다. 그러나 글꼴 선택자는 굵게에 대한 매개 변수를 가지고 있지 않습니다.

중국어 글꼴을 너무 많이 이해하지 못합니다.하지만 simsun.ttc에 굵은 글꼴이없고 simhei.ttf가 굵은 글꼴을 대신 선택할 수 있습니다 (잘못된 경우 수정하십시오). 내가 굵게 다른 글꼴을 선택해야합니까? 그리고 어떻게?

 const int fontSizeforSelector = 12; 
     static Font Arial; 
     static Font SimplifiedArabic; 
     static Font Andalus; 
     static Font TraditionalArabic; 
     static Font Tahoma; 
     static Font Simpo; 
     static Font Batang; 
     static Font Simsun; 
     static Font ArialUni; 
     static Font Simpbdo; 
     static Font Simhei; 

     static iTextSharp.text.pdf.FontSelector _fontSelector = new iTextSharp.text.pdf.FontSelector(); 

     static MyFontSelector() 
     { 
      string winDir = Environment.GetEnvironmentVariable("WINDIR");//!-! 
      if (winDir.IsNullOrEmpty()) 
       winDir = GetWindowsDirectory(); 

      string fontDir = winDir + "\\Fonts\\";//!-! 

      Arial = TryCreateFont(fontDir, "arial.ttf"); 
      SimplifiedArabic = TryCreateFont(fontDir,"simpo.ttf"); 
      Andalus = TryCreateFont(fontDir,"andlso.ttf"); 
      TraditionalArabic = TryCreateFont(fontDir,"trado.ttf"); 
      Tahoma = TryCreateFontEmbedded(fontDir, "tahoma.ttf"); 
      Simpo = TryCreateFontEmbedded(fontDir, "simpo.ttc");//!-! 
      Batang = TryCreateFontEmbedded(fontDir, "batang.ttc");//!-! 
      Simsun = TryCreateFontEmbedded(fontDir, "simsun.ttc,1");//!-! 
      ArialUni = TryCreateFontEmbedded(fontDir, "arialuni.ttf");//!-! 
      Simpbdo = TryCreateFontEmbedded(fontDir, "simpbdo.ttf");//!-! 
      Simhei = TryCreateFontEmbedded(fontDir, "simhei.ttf");//!-! 

      //http://stackoverflow.com/questions/16415074/detecting-cjk-characters-in-a-string-c/16429065#16429065 


      //http://www.computarat.com/files/java/Test9.java 

      if (Arial != null) 
       _fontSelector.AddFont(Arial); 
      if (SimplifiedArabic != null) 
       _fontSelector.AddFont(SimplifiedArabic); 
      if (Andalus != null) 
       _fontSelector.AddFont(Andalus); 
      if (SimplifiedArabic != null) 
       _fontSelector.AddFont(SimplifiedArabic); 
      if (TraditionalArabic != null) 
       _fontSelector.AddFont(TraditionalArabic); 
      if (Tahoma != null) 
       _fontSelector.AddFont(Tahoma); 
      if (Batang != null) 
       _fontSelector.AddFont(Batang); 
      if (Simsun != null) 
       _fontSelector.AddFont(Simsun); 
      if (ArialUni != null) 
       _fontSelector.AddFont(ArialUni); 
      if (Simpbdo != null) 
       _fontSelector.AddFont(Simpbdo); 
      if (Simhei != null) 
       _fontSelector.AddFont(Simhei); 

     } 

     private static Font TryCreateFont(string fontDir, string file) 
     { 
      try 
      { 
       return FontFactory.GetFont(fontDir + file, BaseFont.IDENTITY_H, fontSizeforSelector); 
      } 
      catch(Exception ex) 
      { 
       Trace.WriteLine(ex);     
      } 
      return null; 
     } 

     private static Font TryCreateFontEmbedded(string fontDir, string file) 
     { 
      try 
      { 
       return FontFactory.GetFont(fontDir + file, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 
      } 
      catch (Exception ex) 
      { 
       Trace.WriteLine(ex); 
      } 
      return null; 
     } 

     public static iTextSharp.text.pdf.FontSelector FontSelector 
     { 
      get 
      { 
       return _fontSelector;     
      } 
     } 

     public static BaseFont GetBaseFont(string text) 
     { 
      Phrase phrase = _fontSelector.Process(text); 
      return phrase.Font.GetCalculatedBaseFont(true); 
     } 

답변

1

내 대답에 두 부분이 있습니다 : 내 대답의

1 부 : 일반적으로이

중국어 글꼴되지 않은 대담한 버전입니다 당신은 중국어 글꼴이 필요합니다 그래서 만약 굵게 표시하려면 Chunk의 렌더링 모드를 굵게 글꼴을 모방 한 중국어 텍스트로 변경해야합니다.

chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE); 

이의 SayPeace 예에서 설명됩니다 : 당신이 일반 폰트의 텍스트와 Chunk 객체 chunk이있는 경우

예를 들어, 당신은 대담 것처럼 대담한 모양을 만들기 위해이 작업을 수행 할 수 있습니다 내 책. 기본적으로 글리프는 색상으로 채워진 모양입니다. 그것들을 굵은 글씨로 보이게하기 위해서 우리는 렌더링 모드를 변경하여 단순히 채워지는 것이 아니라 스트로크되도록합니다. 내 대답의

2 부 :

당신은 FontSelector를 사용하여 Phrase을 만들 수 있습니다. 이 phrase 개체는 글꼴이 다른 Chunk 개체의 모음입니다. phrase 개체에 글꼴을 요청하는 것은 이상한 일입니다. 한 글꼴 만 반환하는 반면 구의 다른 청크는 다른 글꼴을 가질 수 있습니다.

내가 너라면 phrase의 다른 청크를 얻고 각 청크에 사용 된 글꼴을 확인하고 필요한 경우 텍스트 렌더링 모드를 변경 한 다음이 청크로 새 구를 만듭니다.

관련 문제