We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Font names unavailable for Aspose.Pdf.Text.FontRepository.FindFont

FontRepository.FindFont() fails when I feed some CJK font names into it.
How should I fix this?

[code]

Font f;
string[] fontnames = { "HYSMyeongJo-Medium", "STSong-Light", "MSung-Light" };
foreach (string fontname in fontnames)
{
    try
    {
        f = FontRepository.FindFont(fontname);
    }
    catch (Exception ex)
    {
        Console.WriteLine("[" + DateTime.Now.ToString() + "]");
        Console.WriteLine("[message]\r\n " + ex.Message);
        Console.WriteLine("[source]\r\n " + ex.Source);
        Console.WriteLine("[stacktrace]\r\n" + ex.StackTrace);
    }
}

[log]

[6/5/2018 3:20:28 PM]
[message]
 Font HYSMyeongJo-Medium was not found
[source]
 Aspose.PDF
[stacktrace]
   at Aspose.Pdf.Text.FontRepository.FindFont(String fontName)
   at pdf.Program.Main(String[] args) in C:\work\project\pdf\Program.cs:line 23
[6/5/2018 3:20:30 PM]
[message]
 Font STSong-Light was not found
[source]
 Aspose.PDF
[stacktrace]
   at Aspose.Pdf.Text.FontRepository.FindFont(String fontName)
   at pdf.Program.Main(String[] args) in C:\work\project\pdf\Program.cs:line 23
[6/5/2018 3:20:32 PM]
[message]
 Font MSung-Light was not found
[source]
 Aspose.PDF
[stacktrace]
   at Aspose.Pdf.Text.FontRepository.FindFont(String fontName)
   at pdf.Program.Main(String[] args) in C:\work\project\pdf\Program.cs:line 23

The code above is a simplified example.
I want to get the Font like this because I want to translate the PDF.

@KDSSHO,

You need to install the missing fonts in the default path of the Operating System. You can also copy font files to a directory, and then add this directory to the font folder list as follows:
C#

// connect custom font directory
FolderFontSource fs = new FolderFontSource(@"path\to\my\folder");
FontRepository.Sources.Add(fs);

Hi Imran,

Thank you for your instruction. I understand what I need to do for this situation.
Then how about “Batang”, one of the the Korean font which more likely to be installed in any users environment by default?
ja2ko.zip (235.9 KB) includes input/output/log files.
I expected 안녕하세요. is printed on out.pdf but it isn’t.
When I ran this code I confirmed the system had “Batang.” in C:\Windows\Fonts folder.

Document doc = new Document("ja.pdf");

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(".+");
textFragmentAbsorber.TextSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.TextReplaceOptions.ReplaceAdjustmentAction = TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation;
doc.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
foreach (TextFragment textFragment in textFragmentCollection)
{
    string translatedText = "안녕하세요.";
    StreamWriter stream = new StreamWriter("ja2ko.txt", true);
    stream.WriteLine("[{0}][{1}]\r\n", textFragment.Text.ToString(), translatedText);
    stream.Close();
    textFragment.Text = translatedText;
    textFragment.TextState.Font = FontRepository.FindFont("Batang");
}
doc.Save("ko.pdf");

Here the method FontRepository.FindFont(“Batang”) seems to have no problem.
But the output file is not what I expected.

If more detailed explanation needed I’d love to share it.
Thanks in advance.

P.S.
I input this 2017112144212.pdf (105.0 KB) to above code and I got this
sagako.pdf (307.9 KB) as output.
What makes these inputs different from each other?

@KDSSHO,

You can embed fonts with Aspose.PDF API into the PDF document. In that way, the font installation on the System is not required. Please refer to this help topic: Embedding Fonts in an existing PDF file

We managed to replicate the problem of misplaced text items. An investigation has been logged under the ticket ID PDFNET-44835 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

Thank you. I’m testing some in that help topic.

I appreciate it.