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.
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";
}
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”.
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
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");