How can we use custom fonts for docx to PDF conversion

We have a requirement where users can use any font they want in the document and when they convert the document to pdf, the same font should be used.
Is there a way we can provide additional fonts for conversion?
I did check the font substitution, but that won’t solve this use case.

@kswati2206

Could you please share following details:

  • Sample conversion code
  • Source file
  • Substitute font
  • Java version
  • OS details

I am using the Sample project file which is provided by Groupdocs.

Converter converter = new Converter(sourceDocumentPath);
        PdfConvertOptions options = new PdfConvertOptions();
        converter.convert(convertedFile, options);
  • Java version : OpenJdk 1.8

  • OS details : MacOs

  • Source File :
    Telia Sans font.docx (12.9 KB)

  • Substitute font : We want to provide the font resouce file and the pdf should be generated with the same font.

1 Like

@kswati2206

Please take a look at this WordProcessingLoadOptions. You can substitute specific fonts when converting Words document.

Thanks for your reply. I did try this, but something seems to be missing. I am getting the following error

Used code:

PresentationLoadOptions loadOptions = new PresentationLoadOptions();
        List<FontSubstitute> fontSubstitutes = new ArrayList<FontSubstitute>();
        fontSubstitutes.add(FontSubstitute.create("Baguet Script", "Arial"));
        //loadOptions.setDefaultFont("resources/fonts/Helvetica.ttf");
        loadOptions.setFontSubstitutes(fontSubstitutes);
        //Converter converter = new Converter(Constants.SAMPLE_DOCX);
        Converter converter = new Converter(sourceDocumentPath, () -> loadOptions);
        PdfConvertOptions options = new PdfConvertOptions();
        converter.convert(convertedFile, options);

Error :

com.groupdocs.conversion.examples.quick_start.HelloWorld.main(HelloWorld.java:26)
	at com.groupdocs.conversion.internal.c.a.s.tdk.w9(Unknown Source)
	at com.groupdocs.conversion.internal.c.a.s.Presentation.ky(Unknown Source)
	at com.groupdocs.conversion.internal.c.a.s.Presentation.w9(Unknown Source)
	at com.groupdocs.conversion.internal.c.a.s.Presentation.<init>(Unknown Source)
	at com.groupdocs.conversion.internal.c.a.s.Presentation.<init>(Unknown Source)
	at com.groupdocs.conversion.factories.a.a(Unknown Source)
	at com.groupdocs.conversion.documents.J.ensureLoaded(Unknown Source)
	... 18 more
Caused by: class com.groupdocs.conversion.internal.c.a.s.internal.zs.bk ---> class com.groupdocs.conversion.internal.c.a.s.internal.zs.C: Can't find presentation part.

Could you please help?

My bad, I corrected it using WordProcessingLoadOptions class.

I am trying a simple use case. I have some text in a file using “Document1.docx” using “Times new Roman” font.
While converting it to pdf, i am trying to do font substitution for it :

fontSubstitutes.add(FontSubstitute.create("Times New Roman", "Arial"));

But, this does not seem to be working. Is my understanding of font substitution correct?
Document1.docx (13.1 KB)

Document1.pdf (31.1 KB)

@kswati2206

Please take a look at this complete code/example - specify font substitution. If issue persists, please share a simple console application using that issue could be reproduced.