While converting Doc to PDF, multiple fonts are visible

Hi,

We have the below scenario and need assistance. We are using Aspose - 17.4.1 version (Paid).
We are converting doc to pdf.

The doc has text set to “Times New Roman”. From different doc file we copy some text that has “Myriad Pro”.
We want the output in PDF, only with “Times New Roman” but could not achieve it.
This is our requirement to attain the pdf in default text which is “Times New Roman”.

With in the code we have set the Font (FontSettings class) but it is not able to convert the entire doc to pdf in TimesRoman.
Specific fonts (“Times New Roman.ttf” and “Myriad Pro.ttf” are present on the server/machine location.

Appreciate assistance here.
Thanks,

@ranjith_rajendran,

You can use the following code before saving to PDF to change Font of entire document. Hope, this helps.

Document doc = new Document(MyDir + @"in.docx");

FontChanger changer = new FontChanger();
doc.Accept(changer);

doc.Save(MyDir + @"18.1.pdf");

class FontChanger : DocumentVisitor
{
    ///
    /// Called when a FieldEnd node is encountered in the document.
    ///
    public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)
    {
        //Simply change font name
        ResetFont(fieldEnd.Font);
        return VisitorAction.Continue;
    }

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

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

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

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

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

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

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

    private void ResetFont(Aspose.Words.Font font)
    {
        font.Name = mNewFontName;
    }

    private string mNewFontName = "Times New Roman";
}
1 Like

Hi,

Thanks for providing the solution but it is not working.
a) We are using Java API version. I have changed the code sent by you accordingly but it is not working.
b) The output PDF file still has the following content
- First Paragraph is in Times New Roman - this is correct as this is the font we want in the pdf.
- Second paragraph (which was copied from other doc file and has font “Myriad Pro”) remain the same in the output PDF as “Myriad Pro” and not converted to “Times New Roman” using the code shared by you in your response.

Please note that -
I am sharing the output of the generated PDF file in the form of screen shot.
Due to security reasons I have red marked some of the contents.

Please note that the paragraphs blocked with green color are in “Times New Roman” and paragraph blocked with red color is in “Myriad Pro”.

We want the output of the entire file in single Text which is “Times New Roman”.

Thanks,

PastedGraphic-11.png (661.2 KB)

@ranjith_rajendran,

Please ZIP and upload your input Word documents here for testing. We will investigate the issue on our end and provide you more information.

It is safe to attach files in the forum. If you attach your documents here, only you and Aspose staff members can download them.

As requested please find the attached document.
Please note that paragraph starts from “Customer… forth below” is “Myriad Pro” and other two paragraphs are “Times New Roman”.

As an output PDF, We are looking all paragraphs in “Times New Roman” only.

Note -
a) we are using Aspose - 17.4.1 version.
b) Java 1.7

Test.docx.zip (151.1 KB)

@ranjith_rajendran,

Please try running the following simple code:

Document doc = new Document("D:\\temp\\Test.docx");

for(Run run : (Iterable<Run>)doc.getChildNodes(NodeType.RUN, true)){
    run.getFont().setName("Times New Roman");
}

doc.save("D:\\temp\\awjava-18.1.pdf"); 

Hope, this helps.

Thanks Awais, the solution that you have provided in last comment worked for me.
Appreciate your help.

I have raised one more issue and appreciate if you or someone from your team can help.

Thanks,

@ranjith_rajendran,

Please follow your other thread for further proceedings.