Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I am trying to make a invoce with HTML to PDF but have troubles when i have unicode characters like: ПРОБАСДАСасда or in the code this one> КОСТАДИН СТОЈЧЕВ and this characters arent shown in the pdf doc. here is the code so pls gude me how to fix this.

XML
try
        {

            var document = new Document(PageSize.A4, 50, 50, 25, 25);
            PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\Invoice_Statement.pdf", FileMode.Create));
            document.Open();
            document.AddAuthor("");
            document.AddCreator("Creator of the Doc");
            document.AddSubject("Subject of the Doc");
            document.AddCreationDate();
            document.AddTitle("This is the title");
            //SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            //SAXmyHtmlHandler shh = new SAXmyHtmlHandler(document);
            HTMLWorker htmlWorker = new HTMLWorker(document);
            String str = "<html><head><meta http-equiv='Content-Type' content='text/html;charset=utf-8'><title><img src='C:/Users/Kosta/Desktop/Vmedia Own Projects/Prodavnicata/eMall/Dodatoci za Master/images/logo.png' alt='Smiley face' height='42' width='42'></title></head><body><table><tr><td><p style='font-size: 10pt; font-family: Times'>" +
            "Cher Monsieur,</p><br><p align='justify' style='text-indent: 2em; font-size: 10pt; font-family: Times'>" +
            "КОСТАДИН СТОЈЧЕВ<br></p><p align='justify' style='text-indent: 2em; font-size: 10pt; font-family: Times'>" +
            "En vous remerciant &agrave; nouveau de la confiance que vous nous t&eacute;moignez,</p>" +
            "<br><p style='font-size: 10pt; font-family: Times'>Bien Cordialement,<br>" +
            "<br>ADMINISTRATEUR ADMINISTRATEUR<br>Ligne directe : 04 42 91 52 10<br>Acadomia&reg; – " +
            "37 BD Aristide Briand  – 13100 Aix en Provence  </p></td></tr></table></body></html>";



            htmlWorker.Parse(new StringReader(str));
            document.Close();
        }
        catch (DocumentException err)
        {

        }
        catch (FileNotFoundException ex)
        {

        }

        catch (IOException exx)
        {
        }
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jun-13 12:11pm    
Just a note: string reader has nothing to do with UTFs, it just holds strings. Internal string representation in memory uses UTF-16LE, but you should never use this fact. For you, this is abstract Unicode, abstracted from bitwise representation.
—SA
thekoko89 18-Jun-13 12:15pm    
mhm tnx for clearing that to me :) do you have some solution how to make this working?
Sergey Alexandrovich Kryukov 18-Jun-13 12:21pm    
Sorry, I don't know what PFD required. From what I know, its Unicode support is quite awkward.
But where is PDF in your code? I can see only HTML, which is of course not a problem at all.
—SA
thekoko89 18-Jun-13 12:28pm    
The PDF documet is created from the HTML. It would be great to use that HTML. In the HTML have Cyrillic letters and if that shows on the pdf then i can make the rest of the changes. Tnx
Sergey Alexandrovich Kryukov 18-Jun-13 12:30pm    
OK, did you obtain correct HTML? You could test it if you, say, saved your HTML string to file in UTF-8. (It should be the encoding of your StreamReader to use.)
—SA

Here are a couple samples. Example A is a simple class that will store the cyrillic output and return the string in the ToString() using override. If you want to use StringReader class you could always inherit from StringReader as shown in Example B.

Example A:
C#
public class MyStringReader
    {
        private String str;
        private Encoding encoding = Encoding.UTF8;

        public Encoding Encoding
        {
            get
            {
                return this.encoding;
            }
            set
            {
                if (value != null)
                {
                    this.encoding = value;
                }
            }
        }
        public MyStringReader(string str)
        {
            this.str = this.encoding.GetString(this.encoding.GetBytes(str));            
        }
        public override string ToString()
        {
            return this.str;
        }
    }

Example B:
C#
public sealed class CyrillicTextReader : StringReader
    {
        public CyrillicTextReader(string text)
            : base(LoadCyrillicText(text))
        {
        }
        private static string LoadCyrillicText(string text)
        {
            return Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(text));
        }
    }
 
Share this answer
 
v3
I fix it :))))) finally the code below works perfectly. The main problem was that it needed a font

XML
string html = @"<html><body><img src='C:/Users/Kosta/Desktop/Vmedia Own Projects/Prodavnicata/eMall/Dodatoci za Master/images/logo.png' alt='Smiley face' height='42' width='42'><table><tr><td><p style='font-size: 10pt; font-family: Times'>" +
           "Cher Monsieur,</p><br><p>" +
           "КОСТА<br></p><p align='justify' style='text-indent: 2em; font-size: 10pt; font-family: Times'>" +
           "En vous remerciant &agrave; nouveau de la confiance que vous nous t&eacute;moignez,</p>" +
           "<br><p style='font-size: 10pt; font-family: Times'>Bien Cordialement,<br>" +
           "<br>ADMINISTRATEUR ADMINISTRATEUR<br>Ligne directe : 04 42 91 52 10<br>Acadomia&reg; – " +
           "37 BD Aristide Briand  – 13100 Aix en Provence  </p></td></tr></table></body></html>";


       FontFactory.Register("c:/windows/fonts/ARIALUNI.TTF");
       StyleSheet style = new StyleSheet();
       style.LoadTagStyle("body", "face", "Arial Unicode MS");
       style.LoadTagStyle("body", "encoding", BaseFont.IDENTITY_H);
       using (Document document = new Document())
       {
           PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\Invoice_Statement.pdf", FileMode.Create));
           document.Open();
           foreach (IElement element in HTMLWorker.ParseToList(
               new StringReader(html.ToString()), style))
           {
               document.Add(element);
           }
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900