Free Support Forum - aspose.com

Set default fontname in PDF document using Aspose.PDF in C# .NET

We are using aspose pdf creaing a document. But our requirements is how to change default font name in aspose pdf document and using aspose version is 8.2.0.
we are using following code for creationg pdf.

Aspose.Words.Document pdfDoc = doc.Clone();
doc = null;
pdfDoc.Save(pdfOoutStream, Aspose.Words.SaveFormat.Pdf);

            Models.TenantConfiguration tenantConfig = new Models.TenantConfiguration();
            if (!string.IsNullOrEmpty(tenantConfig.PdfVersion))
            {
                Aspose.Pdf.Document pdfDocument = Services.DocumentHelper.ConvertPdf(pdfOoutStream, tenantConfig.PdfVersion);
                
                pdfOoutStream = new System.IO.MemoryStream();
                pdfDocument.Save(pdfOoutStream);
            }

How can i acheive this and pl share sample project?

Thanks in advance.

@ksmani82,

Thanks for contacting support.

In order to accomplish your requirements, please follow the instructions specified over Replace fonts in existing PDF file.

In case you encounter any issue, please share the input PDF file, so that we can further test the scenario in our environment.

Thanks a lot for your support codewarior.

We tried this and for our pdf (of 10 page), it takes around 10 mins to walk through each textsegment and update the font. Even after updating, we dont see the right doc.

We have got Aerial MT Pro font license install (and donot have Aerial Narrow). We generate a word doc (from a word template doc) and then use ConvertPdf method to convert it into pdf (after converting the generated word doc to memoryStream). We would like to somehow substitute the font in aspose (from AerialNarrow to Aerial MT Pro) before convertion to Pdf so that pdf comes up fine without any alignment issues.

Do you have any suggestion?

Thanks for your kind support.

@ksmani82,

Thanks for sharing the details.

Can you please share the input PDF file, so that we can test the scenario in our environment.[quote=“ksmani82, post:3, topic:100781”]
We have got Aerial MT Pro font license install (and donot have Aerial Narrow). We generate a word doc (from a word template doc) and then use ConvertPdf method to convert it into pdf (after converting the generated word doc to memoryStream). We would like to somehow substitute the font in aspose (from AerialNarrow to Aerial MT Pro) before convertion to Pdf so that pdf comes up fine without any alignment issues.
[/quote]
Please share the input DOC file and required font files, so that we can also perform DOC to PDF conversion. We are sorry for this inconvenience.

Thanks for sharing the details.

  As Per Your suggestion regarding Replace Font in existing pdf document. but it will take 30 mins more time to convert to pdf.

   We have have created Simple input file for  word document and used Arial Narrow MT Pro. Then We tried it convert it to PDF and it is not showing font as expected. Please find the attached input file and out file. Our code below,

          Aspose.Words.Document doc = new Aspose.Words.Document("C:\\input.docx");
            System.IO.MemoryStream pdfOoutStream = new System.IO.MemoryStream();
            Aspose.Words.Document pdfDoc = doc.Clone();
            doc = null;
            pdfDoc.Save(pdfOoutStream, Aspose.Words.SaveFormat.Pdf);
            string tempPath = System.IO.Path.GetTempPath();
            pdfDoc.Save(tempPath + "\\Output.pdf");

Desktop.zip (53.3 KB)

Thank in advance.

@ksmani82

Thanks for your inquiry. You may use SetFontSubstitutes() method of Aspose.Words for your requirement. Please check following sample code, it will help you to accomplish the task.

Document doc = new Document("input.docx");
FontSettings FontSettings = new FontSettings();
FontSettings.SetFontSubstitutes("MS Gothic", new string[] { "Arial Unicode MS" });
doc.FontSettings = FontSettings;
doc.Save("output.pdf");

Please feel free to contact us for any further assistance.

Best Regards,

@Tilal.ahmad

        Thanks for your support!

The code is working fine but still font name does not changed in PDF.

@ksmani82

Thanks for your feedback. We will appreciate it if you please share your input and output documents along with the font file. We will test the scenario at our end and will guide you accordingly.

Best Regards,

@tilal.ahmad

                 Thanks you very much,  We tried your code and  generated the pdf file but  Font name does not changed in PDF. Please check it. We attached files here.

Fontsubstitute.zip (52.8 KB)

@ksmani82

Thanks for sharing your source documents. It seems you are using quite old version of Aspose.Words for .NET, 13.8. I have installed your shared font and converted your source Word document to PDF using Aspose.Words for .NET 17.7, it seems okay. Please find attached output PDF document and confirm.
output_aw177.pdf (11.2 KB)

Best Regards,

@tilal.ahmad,

 Thanks for your support,
  I  downloaded latest version aspose.words 17.7 then used as a evaluation version, but still not changed font as expected in pdf. for example applied the font Name in existing word file using FontSettings method  and it does not changed When opening Word file  showing Times New Roman but Font Name Specified in code level like  "Bauhaus 93" or "Arial Narrow Mt Pro"). I have attached the sample projects(removed dll) (vs2012). Please check this.

WebApplication1.zip (138.2 KB)

   Does we need to change any Embedded font settings in system?

@ksmani82

Thanks for sharing your project. Please share your input document as well, it will help us to address your issue exactly.

We are sorry for the inconvenience.

Best Regards,

Please find the attached Input file.input.zip (9.9 KB)

@ksmani82

Thanks for sharing the additional information. Please note SetFontSubstitue method is used when Aspose.Words does not find the used font on host machine. If the document fonts are available on host machine or embedded in document then it is not called. Please check Font Availability and Substitution section for more details.

Furthermore, if you want to replace some available font with another font then you can use FontChange class. Please check following code snippet, it changes document fonts to “Arial Narrow MT Pro”. Hopefully it will help you to accomplish the task.
output_aw.zip (20.4 KB)

protected void Page_Load(object sender, EventArgs e)
{
            

    Aspose.Words.Document doc = new Aspose.Words.Document("input.docx");
    FontChanger changer = new FontChanger();
    doc.Accept(changer);
    doc.Save("output_aw.pdf");
         
}
/// <summary>
/// Class inherited from DocumentVisitor, that changes font.
/// </summary>
class FontChanger : DocumentVisitor
{
    /// <summary>
    /// Called when a FieldEnd node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)
    {
        //Simply change font name
        ResetFont(fieldEnd.Font);
        return VisitorAction.Continue;
    }

    /// <summary>
    /// Called when a FieldSeparator node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
    {
        ResetFont(fieldSeparator.Font);
        return VisitorAction.Continue;
    }

    /// <summary>
    /// Called when a FieldStart node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFieldStart(FieldStart fieldStart)
    {
        ResetFont(fieldStart.Font);
        return VisitorAction.Continue;
    }

    /// <summary>
    /// Called when a Footnote end is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFootnoteEnd(Footnote footnote)
    {
        ResetFont(footnote.Font);
        return VisitorAction.Continue;
    }

    /// <summary>
    /// Called when a FormField node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFormField(FormField formField)
    {
        ResetFont(formField.Font);
        return VisitorAction.Continue;
    }

    /// <summary>
    /// Called when a Paragraph end is encountered in the document.
    /// </summary>
    public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
    {
        ResetFont(paragraph.ParagraphBreakFont);
        return VisitorAction.Continue;
    }

    /// <summary>
    /// Called when a Run node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitRun(Run run)
    {
        ResetFont(run.Font);
        return VisitorAction.Continue;
    }

    /// <summary>
    /// Called when a SpecialChar is encountered in the document.
    /// </summary>
    public override VisitorAction VisitSpecialChar(SpecialChar specialChar)
    {
        ResetFont(specialChar.Font);
        return VisitorAction.Continue;
    }

    private void ResetFont(Aspose.Words.Font font)
    {
        //if (font.Name == "Arial Narrow MT Pro")
        font.Name = mNewFont;
                
    }

    //Font by default
    private string mNewFont = "Arial Narrow MT Pro";
            
}

Best Regards,

1 Like

@tilal.ahmad,

Thanks for sharing information. We will work with this, we are unable to download zip file of attachment.

@ksmani82,

Can you please share some details i.e. error message which you are getting.

Pleas note that in order to download the attachments, you need to first login to our support system/forums. Please share the details, so we may further look into this matter.

@codewarior,

    Thanks, We attached Error scrennshot, showed error after loggedin.

Download Attachment failed.png (9.0 KB)

@ksmani82,

Thanks for sharing the details. We are further looking into attachment related issue and will get back to you soon.

However as a workaround, I have also uploaded the attachment over this link.

Should you have any further query, please feel free to contact.

@ksmani82,

Can you please try logging in again and downloading the file attached to this forum thread? Please let us know if you still see the same issue.

Best Regards,

@tilal.ahmad,

                  Thank you for sharing details, We have fixed the PDF font issues in our end  based on your previous solutions.