We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Apply Font style across PDF document

Hi,


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.

Regards,

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.

[Java]

com.aspose.pdf.Document pdf1 = new com.aspose.pdf.Document();<o:p></o:p>

com.aspose.pdf.TextState textState = new com.aspose.pdf.TextState();<o:p></o:p>

textState.setForegroundColor(com.aspose.pdf.Color.getGreen());<o:p></o:p>

textState.setFont(FontRepository.findFont(“Times New Roman”));<o:p></o:p>

pdf1.getPageInfo().setDefaultTextState(textState);<o:p></o:p>

pdf1.getPages().add();<o:p></o:p>

pdf1.getPages().get_Item(1).getParagraphs().add(new com.aspose.pdf.TextFragment(“Hello World in Times New Roman”));<o:p></o:p>

pdf1.getPages().get_Item(1).getParagraphs().add(new com.aspose.pdf.TextFragment(“Second Text line…”));<o:p></o:p>

pdf1.save(“c:/pdftest/outputresults.pdf”);<o:p></o:p>

pdf1.close();

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

Best Regards,

Hi,


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

Regards,

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.

JAVA

<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

pdf.getPages().accept(absorber);

// traverse through all the TextFragments

TextFragmentCollection textFragmentCollection = absorber.getTextFragments();

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

TextFragment textFragment = iterator.next();

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

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

}

// Save the updated document

pdf.save(myDir + “output_test.pdf”);

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

Best Regards,