Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Sep 29, 2020 11:02 am

hello

I tried to change the format of the selected text,
When I save to pdf or docx,
The font size is changed, but the text format has not changed.


Code: Select all
                Spire.Doc.Document docX = new Spire.Doc.Document("test.docx");
                TextSelection[] selections = docX.FindAllString("うえお", true, false);
                if (selections == null) throw new Exception("can not find text");
                foreach (TextSelection selection in selections)
                {
                    TextRange range = selection.GetAsOneRange();
                    range.CharacterFormat.FontName = "ABCDE";
                    range.CharacterFormat.FontSize = 36;
                }
                string fontPath = Path.Combine(Application.StartupPath, "KTEGAKI.ttf");
                if (!System.IO.File.Exists(fontPath)) throw new Exception("can not find font file!");
                docX.EmbedFontsInFile = true;
                docX.PrivateFontList.Add(new PrivateFontPath("ABCDE", fontPath));

                docX.SaveToFile("text.pdf", Spire.Doc.FileFormat.PDF);
                docX.SaveToFile("text2.docx", Spire.Doc.FileFormat.Docx);

hidamari
 
Posts: 5
Joined: Fri May 15, 2020 10:48 am

Wed Sep 30, 2020 6:27 am

Hello,

Thanks for your inquiry.
Please refer to the following modified code, it works fine on my end. I have also attached my output files for your reference. If there is any question, please feel free to write back.
Code: Select all
 Document docX = new Document();
            docX.LoadFromFile("test.docx");

            TextSelection[] selections = docX.FindAllString("うえお", true, false);
            if (selections == null) throw new Exception("can not find text");
            foreach (TextSelection selection in selections)
            {
                TextRange range = selection.GetAsOneRange();
                range.CharacterFormat.FontName = "KAWAIITEGAKIMOJI";
                range.CharacterFormat.FontSize = 36;
            }

            docX.EmbedFontsInFile = true;
            docX.PrivateFontList.Add(new PrivateFontPath("KAWAIITEGAKIMOJI", @"KTEGAKI.ttf"));

            string result = "doc.docx";
            docX.SaveToFile(result, Spire.Doc.FileFormat.Docx);

            docX.LoadFromFile(result);
            ToPdfParameterList parms = new ToPdfParameterList();
            List<PrivateFontPath> fonts = new List<PrivateFontPath>();
            fonts.Add(new PrivateFontPath("KAWAIITEGAKIMOJI", @"KTEGAKI.ttf"));
            parms.PrivateFontPaths = fonts;
            string pdf = "doc2pdf.pdf";
            docX.SaveToFile(pdf, parms);
            System.Diagnostics.Process.Start(pdf);


Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Wed Sep 30, 2020 9:00 am

Hello

Thanks for your reply
It can work normally
Thank you :lol:

hidamari
 
Posts: 5
Joined: Fri May 15, 2020 10:48 am

Wed Sep 30, 2020 9:40 am

Hello,

Glad to hear that you solve this issue.
If you encounter any issues related our product in the future, just feel free to contact us.
Have a nice day!

Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Return to Spire.Doc