Aspose Pdf elements TextInfo not available

Hi Team,
We are trying to have type-1 fonts embedded into a pdf. We were trying to follow the TextInfo class as provided in the below dotnet link in Java. (Problem using Adobe Postscript Type 1 Fonts - #8 by AndyWatt). As part of our research we were able to locate the TextInfo is available in com.aspose.pdf.elements as in referred documentation link below, but we are not finding the elements package.
TextInfo (Aspose.Pdf for Java)

Could you let us know which aspose version has the elements.TextInfo package and methods? if no present version could you let us know if this is a future implementation or a decomissioned implementation?

@ramachandra1988

TextInfo Property was used in the old API versions and it has been replaced with TextState in the newer version. You can use TextState and set Font from it for a TextFragment.

Thanks for the reply. However, we are still not able to use the pfb/afm files as the setfont gives an exception that it cannot recognize the font file. When we use the regular font files, the font gets changed, but the text gets overlapped and is not what we are expecting.

So, the ask from our side is, is it possible to use AFM/PFB files with Aspose PDF in Java similar to .Net. If so, could you please provide the exact methods as we cannot locate them in TextState.

Exception Message:
Could not determine font type of font file specified

StackTrace:
class com.aspose.pdf.exceptions.PdfException: Could not determine font type of font file specified
com.aspose.pdf.FontRepository.openFont(Unknown Source)
com.aspose.pdf.FontRepository.openFont(Unknown Source)

Code Snippet:
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(stream);
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(
new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));
pdfDocument.getPages().accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
textFragmentCollection.forEach( textFragment -> {
textFragment.getTextState().setFont(FontRepository.openFont(“arial.ttf”));
});

@ramachandra1988

Can you please share the sample font files for our reference as well? We will test the scenario in our environment and address it accordingly.

Step 1.pdf (86.9 KB)
Step 2.pdf (180.6 KB)
User_Input.zip (20.1 KB)

Please find attached files.

Step 1: Generated from the user input using Aspose Words

Step 2: Generated using Step1.pdf as input and using textfragment to replace the font as Arial. As you can see in step 2, all the text formatting like bold, italic are all are removed and we have a plain text.

Please let us know if any additional information is needed.

@ramachandra1988

We were able to notice the issue related to the font that bold text was changed into regular text in the output while using Aspose.PDF for Java 22.11. Can you please share if you are still facing the above exception? Have you tried after installing the font and calling FontRepository.findFont() method?

As much as text formatting is an issue (like bold to regular, removing italic, etc), our primary concern is to get the text in Type-1 embedded format output. So when we use FontRepository.openFont its not detecting the afm/pfb files from resources folder and we have already copied the exception above? Please find the afm/pfb files attached and let us know of a way where we can use these files and have the text in pdf as Type-1 with embedded mode?

afm-pfb-inf-fonts (1).zip (355.7 KB)

@ramachandra1988

We have logged an investigation ticket as PDFJAVA-42337 in our issue tracking system for your requirement of embedding AFM/PFB font files. Also, the issue of losing text formatting is logged as PDFJAVA-42338 in our issue management system. We will look into details of the ticket and let you know as soon as they are resolved. Please be patient and spare us some time.

We are sorry for the inconvenience.

@ramachandra1988

This is not a bug. The font style information is stored in a font file and as there was used the regular font “arial.ttf” then all text becomes regular.

By default in Windows OS, the bold font has the name “arialbd.ttf” (italic has the name “ariali.ttf”, italic bold - “arialbi.ttf”). You should use all these font files so the code snippet could be modified to:

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(stream);
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(
                new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));

pdfDocument.getPages().accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
textFragmentCollection.forEach(textFragment -> {
   String fontName = textFragment.getTextState().getFont().getFontName();
   if (fontName.contains("Bold") && fontName.contains("Italic")) {                
       textFragment.getTextState().setFont(FontRepository.openFont("C:/Windows/Fonts/arialbi.ttf"));
   } else if (fontName.contains("Bold")) {                
      textFragment.getTextState().setFont(FontRepository.openFont("C:/Windows/Fonts/arialbd.ttf"));
   } else if (fontName.contains("Italic")) {                
      textFragment.getTextState().setFont(FontRepository.openFont("C:/Windows/Fonts/ariali.ttf"));
   } else {                
       textFragment.getTextState().setFont(FontRepository.openFont("C:/Windows/Fonts/arial.ttf"));
   }
});

Arial font is wider than the TimesNewRomanPSMT font in the original document so I have some problems with rendering. Maybe, the ArialNarrow font family could be more fit for this document (“C:/Windows/Fonts/ARIALN.ttf”) than Arial.