Change Font "Citi-Sans-Text" for PDF File

Hi Team,

I want to change font of type “Citi-Sans-Text” in PDF file. I am able to change font in word document. while coverting output stream to PDF it is not changing the font. Please find attached document word document sample.PAP_PP57229_STL202304009_20230505151924.docx (81.9 KB)

Below is the code:
public async Task GenerateDocFile(string Request_Number, string exportType, string soeid)
{
bool IsPDF = exportType.ToLowerInvariant().Contains(“pdf”);
string reportFile = string.Format(@"{0}PAP_{1}{2}{3}.{4}", ConfigurationManager.AppSettings[“DOWNLOAD_PATH”], soeid, Request_Number, DateTime.Now.ToString(“yyyyMMddHHmmss”), IsPDF ? “pdf” : “docx”);

        try
        {
            Document doc = await GenerateDoc(Request_Number, soeid);

            var Tablenodes = doc.GetChildNodes(NodeType.Table, true);
            if (Tablenodes != null)
            {
                for (int i = 0; i < Tablenodes.Count; i++)
                {
                    Aspose.Words.Tables.Table tab = (Aspose.Words.Tables.Table)doc.GetChildNodes(NodeType.Table, true)[i];
                    //tab.AutoFit(Aspose.Words.Tables.AutoFitBehavior.FixedColumnWidths);
                    if (tab.GetText().Contains("PAP Approval Comments"))
                    {
                        tab.AllowAutoFit = true;
                        tab.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPercent(100);
                    }
                }
            }

            using (MemoryStream outStream = new MemoryStream())
            {
                doc.Save(outStream, IsPDF ? SaveFormat.Pdf : SaveFormat.Docx);
                outStream.Position = 0;

                if (IsPDF)
                {
                    using (MemoryStream pdfOutStream = new MemoryStream())
                    {
                        Aspose.Pdf.Document pdfdoc = PdfFontUpdate(outStream, AppDomain.CurrentDomain.BaseDirectory + "fonts\\Citi-Sans-Text\\Citi-Sans-Text-Regular.otf");
                        pdfdoc.Save(pdfOutStream);
                        pdfOutStream.Position = 0;
                        ePAPS_FileUtility.SaveFile((Stream)pdfOutStream, reportFile);
                    }
                }
                else
                {

                    ePAPS_FileUtility.SaveFile((Stream)outStream, reportFile);

                }
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, new LogMessage() { MethodName = "GenerateDocFile ", LogErroMessage = "SOEID:" + soeid });
        }

        return reportFile;
    }

public static Aspose.Pdf.Document PdfFontUpdate(MemoryStream ms, string fontPath)
{
Aspose.Pdf.Document pdfdoc = new Aspose.Pdf.Document(ms);

        //Aspose.Pdf.Text.Font font = Aspose.Pdf.Text.FontRepository.OpenFont(fontPath);
        //Aspose.Pdf.Text.TextFragmentAbsorber textFragmentAbsorberForFontReplace = new Aspose.Pdf.Text.TextFragmentAbsorber();
        //pdfdoc.Pages.Accept(textFragmentAbsorberForFontReplace);
        //foreach (Aspose.Pdf.Text.TextFragment textFragment in textFragmentAbsorberForFontReplace.TextFragments)
        //{
        //    textFragment.TextState.Font = font;
        //    //textFragment.TextState.ApplyChangesFrom(textFragment.TextState);
        //}

        return pdfdoc;
    }
  1. If I uncomment the lines “PdfFontUpdate” then fonts are changing but alignment of text will be not proper. Please find the attahced document for sample of PDF (After uncoment the code of method). PAP_PP57229_STL202304009_20230505152954.pdf (202.3 KB)

@rs43733,

If it is a font that is not installed, you can use this to add it:

var fontSource = new FolderFontSource("FontPath");

FontRepository.Sources.Add(fontSource);

Also, do not use “Open” for using a font, use “Find”:

var font = FontRepository.FindFont("YourFont");

Then you can assign the font like you have been doing.

If you want to change all existing fragments and remove old fonts that are not used anymore, you can do the following:

var absorber = new TextFragmentAbsorber(new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));
doc.Pages.Accept(absorber);

absorber.ApplyForAllFragments(FontRepository.FindFont("TheNewFontYouWantToUse"));