2012-04-02 5 views
0

Я пытаюсь отобразить символ «✔» в PDF, используя iTextSharp. Однако персонаж не будет отображаться в созданном PDF-файле. Пожалуйста помоги мне с этим.Как показать ✔ в PDF с помощью iTextSharp?

+2

Можете ли вы показать нам код, который вы используете, чтобы добавить этот символ в свой PDF документ с помощью iTextSharp? – 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 шрифтов, а Zipf Dingbats - один из них. Это решение, вероятно, лучше всего, потому что не требует дополнительного встраивания шрифтов. Единственная причина фактически внедрить шрифт будет, если вам не нравится, как это делает. –

1

Шрифт Wingdings печатает этот символ вместо «o». Вам необходимо подключить этот шрифт к вашему приложению, а затем применить этот шрифт к букве и вставить шрифт в формат 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

Я никогда не видел так много вызовов GC.Collect() в рамках одного метода. – lurkerbelow

Смежные вопросы