Specify different sources for fonts

Hello,


Is it possible to specify different font sources in Aspose.Pdf for Java?

I see that the API has public classes like in Aspose.Words:
- com.aspose.pdf.FontSource
- com.aspose.pdf.FolderFontSource
- com.aspose.pdf.FileFontSource
- com.aspose.pdf.MemoryFontSource

but I could not find any methods to specify a list (or array) of different font sources.

In Aspose.Words the method is
com.aspose.words.FontSettings.setFontsSources(com.aspose.words.FontSourceBase[] sources);

Best regards, Evgeniy

Hi Evgeniy,


Thanks for your inquiry. Yes you can set font path for different font sources. Please check following code snippet to set font path if there are more than one font path. Hopefully it will help you to accomplish the task.


//geting the list for standard font directories<o:p></o:p>

java.util.List list = com.aspose.pdf.Document.getLocalFontPaths();<o:p></o:p>

//seting the user list for standard font directories<o:p></o:p>

list.add(“c:\fonts2\”);<o:p></o:p>

list.add(“c:\fonts3\”);<o:p></o:p>

com.aspose.pdf.Document.setLocalFontPaths(list);


Please feel free to contact us for any further assistance.


Best Regards,

Hi Tilal,


Thanks, it’s clear.

But I would like to set fonts not only from filesystem but also from memory.
Use case is we have a jar-file with the fonts inside so they may be accesed only as resources classpath:/com/a/b/resources/fonts/abc.ttf

In Aspose.Words we use class MemoryFontSource for this, I expect to see similar approach in Aspose.Pdf.

Best regards, Evgeniy

Hi Evgeniy,


Thanks for your feedback. After initial investigation, we have logged a ticket PDFNEWJAVA-34627 in our issue tracking system to investigate and implement your requirement to use fonts from memory. We will keep you updated about the issue resolution progress.

We are sorry for the inconvenience caused.

Best Regards,

Hi Evgeniy,


Thanks for your inquiry. You may use fonts from stream as following.

Hopefully it will help you to accomplish the task.

// Open font<o:p></o:p>

InputStream fontStream = new FileInputStream("C:\\WINDOWS\\Fonts\\arial.ttf");

Font font = FontRepository.openFont(fontStream, FontTypes.TTF);

// Open document

Document doc = new Document("input.pdf");

// Create TextFragmentAbsorber object to find all "hello world" text occurrences

TextFragmentAbsorber absorber = new TextFragmentAbsorber("hello world");

// Accept the absorber for first page

doc.getPages().get_Item(1).accept(absorber);

// Change font of the first text occurrence

absorber.getTextFragments().get_Item(1).getTextState().setFont ( font);

// Save document

doc.save("output.pdf");


Best Regards,