2012-04-02 20 views
0

iTextSharpを使用してPDFに「✔」文字を表示しようとしています。ただし、作成されたPDFに文字は表示されません。これで私を助けてください。iTextSharpを使って✔をPDFで表示するには?

+2

あなたは私たちにあなたがiTextSharpを使用してPDF文書にその文字を追加するために使用しているコードを表示することができますか? – user7116

+2

✔文字のあるフォントを使用していますか? –

答えて

10
Phrase phrase = new Phrase("A check mark: "); 
Font zapfdingbats = new Font(Font.FontFamily.ZAPFDINGBATS); 
phrase.Add(new Chunk("\u0033", zapfdingbats)); 
phrase.Add(" and more text"); 
document.Add(phrase); 
+0

PDF仕様に準拠PDFレンダラーは14個のフォントからなる共通のセットをサポートしなければならず、Zapf Dingbatsもその1つです。このソリューションは、フォントを埋め込む必要がないため、おそらく最適です。実際にフォントを埋め込む唯一の理由は、このレンダリング方法が気に入らない場合です。 –

1

フォントWingdingsは "o"の代わりにこの文字を印刷します。 このフォントをアプリケーションに接続し、このフォントをletterに適用し、互換性のためpdfにフォントを埋め込む必要があります。

これは私が自分のプロジェクトの中でしばらく前から使っていた機能です(クリーニングされていません)。 クリーンアップしてください。ただし、必要な機能がいくつかあります。 (私は私のカスタムフォント(font1.ttfとfont2.ttf)をプロジェクトディレクトリにコピーしました)

私はあなたに役立つことを望んでいます。

public void StartConvert(String originalFile, String newFile) 
    { 
     Document myDocument = new Document(PageSize.LETTER); 
     PdfWriter.GetInstance(myDocument, new FileStream(newFile, FileMode.Create)); 
     myDocument.Open(); 

     int totalfonts = FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts"); 
     iTextSharp.text.Font content = FontFactory.GetFont("Pea Heather's Handwriting", 13);//13 
     iTextSharp.text.Font header = FontFactory.GetFont("assign", 16); //16 

     BaseFont customfont = BaseFont.CreateFont(@"font1.ttf", BaseFont.CP1252, BaseFont.EMBEDDED); 
     Font font = new Font(customfont, 13); 
     string s = " "; 
     myDocument.Add(new Paragraph(s, font)); 

     BaseFont customfont2 = BaseFont.CreateFont(@"font2.ttf", BaseFont.CP1252, BaseFont.EMBEDDED); 
     Font font2 = new Font(customfont2, 16); 
     string s2 = " "; 
     myDocument.Add(new Paragraph(s2, font2)); 

     try 
     { 
      try 
      {     
       using (StreamReader sr = new StreamReader(originalFile)) 
       { 
        // Read and display lines from the file until the end of 
        // the file is reached. 
        String line; 
        while ((line = sr.ReadLine()) != null) 
        { 
         String newTempLine = ""; 
         String[] textArray; 
         textArray = line.Split(' '); 
         newTempLine = returnSpaces(RandomNumber(0, 6)) + newTempLine; 

         int counterMax = RandomNumber(8, 12); 
         int counter = 0; 
         foreach (String S in textArray) 
         { 
          if (counter == counterMax) 
          { 
           Paragraph P = new Paragraph(newTempLine + Environment.NewLine, font); 
           P.Alignment = Element.ALIGN_LEFT; 
           myDocument.Add(P); 
           newTempLine = ""; 
           newTempLine = returnSpaces(RandomNumber(0, 6)) + newTempLine; 
          } 
          newTempLine = newTempLine + returnSpaces(RandomNumber(1, 5)) + S; 
          counter++; 
         } 
         Paragraph T = new Paragraph(newTempLine, font2); 
         T.Alignment = Element.ALIGN_LEFT; 

         myDocument.Add(T); 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("The file could not be read:"); 
       Console.WriteLine(e.Message); 
      } 
     } 
     catch (DocumentException de) 
     { 
      Console.Error.WriteLine(de.Message); 
     } 
     catch (IOException ioe) 
     { 
      Console.Error.WriteLine(ioe.Message); 
     } 

     try 
     { 
      myDocument.Close(); 
     } 
     catch { } 
    } 
+6

1つのメソッド内でGC.Collect()呼び出しが非常に多く見たことはありません。 – lurkerbelow

関連する問題