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

Free Support Forum - aspose.com

How to change font directory?

I need to make the

textFragment.TextState.Font.FontName
to get the Font from local path on my PC

Any help ?

Hello Mohamed,


Thanks for using our API’s.

Aspose.Pdf for .NET offers the feature to use Custom/TrueType fonts while creating/manipulating PDF file contents, so that file contents are displayed using contents other than default system fonts. Starting release of Aspose.Pdf for .NET 10.3.0, we have provided the support for Open Type Fonts. Please see below code for reference

// The path to the documents directory.
string dataDir = “C:/”;
// Create new document instance
Document pdfDocument = new Document();
// Add page to pages collection of PDF file
Aspose.Pdf.Page page = pdfDocument.Pages.Add();
// Create TextFragment instance with sample text
TextFragment fragment = new TextFragment(“Sample Text in OTF font”);
// Find font inside system font directory
// Fragment.TextState.Font = FontRepository.FindFont(“HelveticaNeueLT Pro 45 Lt”);
// Or you can even specify the path of OTF font in system directory
fragment.TextState.Font = FontRepository.OpenFont(dataDir + “space age.otf”);
// Specify to emend font inside PDF file, so that its displayed properly,
// Even if specific font is not installed/present over target machine
fragment.TextState.Font.IsEmbedded = true;
// Add TextFragment to paragraphs collection of Page instance
page.Paragraphs.Add(fragment);
dataDir = dataDir + “OTFFont_out.pdf”;
// Save resulting PDF document.
pdfDocument.Save(dataDir);

If you still face any issue, please feel free to contact us.

Dear Fahad,


Thanks for your reply, I need to make the font Embedded Subset not as the attached image "Embedded Open Type"

Thanks,



Hello Mohamed,


Thanks for sharing more details. You can use IsSubset = true, instead of IsEmbedded = true in above code as follow

<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>fragment.TextState.Font.IsSubset = <span class=“kwrd” data-trid=“576” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>true<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>;

If you still face any issue, please share your sample code along with input PDF document and the font file. It will help us to further investigate your issue and address it accordingly.

Best Regards,

I tried it and it gives the same error, here is the code


string dataDir = @“D:\folder”;

// Open document
string filename = “Input”;
Document pdfDocument = new Document(dataDir + filename + “.pdf”);

// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(“xxxxxxxxxx”);
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;
// Accept the absorber for all the pages
pdfDocument.Pages.Accept(textFragmentAbsorber);
// Get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
if (textFragmentCollection.Count > 0)
{
// Get first occurance of text and replace
TextFragment textFragment = textFragmentCollection[1];
// Update text and other properties
textFragment.Text = “New Text”;
textFragment.TextState.CharacterSpacing = -0.009F;
textFragment.TextState.Font.IsSubset = true;

dataDir = dataDir + filename + “_out.pdf”;
pdfDocument.Convert(“file.log”, PdfFormat.PDF_A_2A, ConvertErrorAction.Delete);
pdfDocument.Save(dataDir);
Console.WriteLine("\nText replaced successfully.\nFile saved at " + dataDir);
}

Hi Mohamed,


p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Arial; -webkit-text-stroke: #000000} span.s1 {font-kerning: none}

I have tested the scenario and have managed to reproduce the problem that "Embedded Subset" font is not showing in Adobe DC. For the sake of correction, I have logged it as PDFNET-42243 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction.


Please be patient and spare us little time. We are sorry for this inconvenience.


Best Regards,

Thanks, I'll wait your reply