Apply Font style across PDF document


What is the best way of apply same Font-Family and Font Size across the complete pdf file instead of going page by page for every text fragments.


Hi Carol,

Thanks for contacting support.

Yes, you can apply same Font-Family to entire document. Please see below sample code snippet for your reference.


com.aspose.pdf.Document pdf1 = new com.aspose.pdf.Document();

com.aspose.pdf.TextState textState = new com.aspose.pdf.TextState();
textState.setFont(FontRepository.findFont("Times New Roman"));

pdf1.getPages().get_Item(1).getParagraphs().add(new com.aspose.pdf.TextFragment("Hello World in Times New Roman"));
pdf1.getPages().get_Item(1).getParagraphs().add(new com.aspose.pdf.TextFragment("Second Text line..."));"c:/pdftest/outputresults.pdf");

If you still face any issue, please feel free to contact us.

Best Regards,


Thanks for your help, but how do we convert the existing pdf document to use 1 single font name, font size etc.


Hi Carol,

I am afraid, there is no specific approach to define font name for whole existing PDF document but you need to explicitly define the font information for individual TextFragment and TextSegment. The following code snippets shows how to replace font inside PDF document. It traverse through all TextSegments inside PDF file and then update the Font information.


<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>String myDir = “/Users/fahadadeel/Downloads/resources/”;

// Load existing PDF Document

Document pdf = new Document(myDir + “pdf-sample.pdf”);

// Search text fragments and set edit option as remove unused fonts

TextFragmentAbsorber absorber = new TextFragmentAbsorber(new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));

// accept the absorber for all the pages


// traverse through all the TextFragments

TextFragmentCollection textFragmentCollection = absorber.getTextFragments();

for (Iterator iterator = textFragmentCollection.iterator(); iterator.hasNext():wink: {

TextFragment textFragment =;

String fontName = textFragment.getTextState().getFont().getFontName();

textFragment.getTextState().setFont(FontRepository.findFont(“Courier New”));


// Save the updated document + “output_test.pdf”);

If you still face any issue, please feel free to contact us.

Best Regards,