2

I'm still a beginner in C# and I want to know is there an option of writing slavic letters č,ć,š,ž in PDF using iTextSharp. I was reading other posts about it but I can't apply their solution to my problem, maybe it's a bit to complicate to me as a beginner. This is my code:

SaveFileDialog pdfFile = new SaveFileDialog();
pdfFile.Filter = "PDF|*.pdf";
pdfFile.Title = "Spremi PDF";
pdfFile.FileName = "Ispit";
if (pdfFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    Document document = new Document(iTextSharp.text.PageSize.LETTER, 25, 25, 35, 35);
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfFile.FileName, FileMode.Create));
    document.Open();

    foreach (List<string> question in questions)
    {
          document.NewPage();
          foreach (string field in question)
          {
                 document.Add(new Paragraph(field));
          }
    }

    document.Close();
}

This code is maybe to simple and maybe there's a lots of better ways to do this but this is one of my first codes in C#.

NutCracker
  • 11,485
  • 4
  • 44
  • 68
  • What exactly are you seeing? An err msg when adding the new paragraph, or the wrong characters being added, or...? – B. Clay Shannon-B. Crow Raven Nov 13 '15 at 18:21
  • Text added to the pdf is without these letters – NutCracker Nov 13 '15 at 18:32
  • 4
    Czech this out (no pun intended): http://stackoverflow.com/questions/26631815/cant-get-czech-characters-while-generating-a-pdf Although Czech <> Croation, the solution should be similar. – B. Clay Shannon-B. Crow Raven Nov 13 '15 at 18:35
  • Font f2 = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, true); when i write this, FONT gets red underlined – NutCracker Nov 13 '15 at 18:45
  • What happens when you right click on it - does it give you a chance to resolve a reference, or any other feedback? – B. Clay Shannon-B. Crow Raven Nov 13 '15 at 18:47
  • `FONT` is defined in the code sample: `public static final String FONT = "resources/fonts/FreeSans.ttf";`. Also, it seems you're copy/pasting Java code in your C# solution. This should work: `Font f2 = FontFactory.GetFont("path/to/your/font.ttf", BaseFont.IDENTITY_H, true)` – rhens Nov 13 '15 at 22:56
  • It's not working. It gets underlined saying that font is ambigous reference between itextsharp and system.drawing.font. I also try to set an alias using Font = iTextSharp.Text.Font; but nothing happens. It's showing just š,ž and not č,ć. – NutCracker Nov 14 '15 at 08:22
  • You should also read http://stackoverflow.com/questions/33574106/itextsharp-font-interfering-with-common-font/33578243#33578243 This is typical for C#; it isn't an iTextSharp problem. – Bruno Lowagie Nov 14 '15 at 08:40
  • Yes. I have done this but still letters č and ć are not added to PDF. – NutCracker Nov 14 '15 at 09:00
  • It works for me, but I can imagine that a newbie who doesn't know how to distinguish `System.Drawing.Font` from `iTextSharp.text.Font` also doesn't understand the concept of encoding. Did you ever learn [the basics of encoding](http://www.joelonsoftware.com/articles/Unicode.html)? Don't you have access to a more experienced developer at your company, one that understands the answers that are given? – Bruno Lowagie Nov 14 '15 at 09:04
  • 1
    I'm not working in a company and I'm just looking for help in learning. Thanks for your time – NutCracker Nov 14 '15 at 09:45
  • 1
    In that case, please show your progress: in your current code snippet, you don't define a font. Which font did you choose? Maybe you selected a font that doesn't contain any Croatian glyphs. Also show what `field` is about: how do you read the `questions`? Are you using the correct encoding when you read them. When using `IDENTITY_H`, it should be Unicode. Your most funny remark was *FONT gets red underlined* but `FONT` is a constant you have to define yourself... Although I understand that you want to learn, you are making too many errors at once right now. Please work step by step. – Bruno Lowagie Nov 14 '15 at 12:19

1 Answers1

1

I have solved my problem. This is the code that helped me:

BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false);
Font titleFont = new Font(bf,20);
Font infoFont = new Font(bf,16);

Thank you all

NutCracker
  • 11,485
  • 4
  • 44
  • 68
  • @mrisek try with this: https://developer.xamarin.com/guides/android/advanced_topics/localization/ – NutCracker Jul 13 '17 at 10:03
  • Actually, your code works just fine. The problem is that Google Drive PDF viewer doesn't support croatian chars. When I opened the same file with another application like Adobe Acrobat, all symbols were present. – mrisek Aug 03 '17 at 07:51