Hi Robert,
Thanks for your inquiry. I am afraid I am unable to test your sample code due to missing references. We will appreciate it if you please share some static values of used variables, so we will test the scenario and will update you accordingly.
Furthermore, It seems Aspose.Pdf for Java is unable to find fonts on your system. Please note on non-Windows OSs Aspose.Pdf for Java looks fonts in system default font path. For Example in case of CentOS it looks fonts in /usr/share/fonts/. I had installed Microsoft fonts on the CentOS and these fonts are installed in system default font path i.e. /usr/share/fonts/msttcorefonts/. Can you please install Microsoft fonts on your system or copy true type fonts from your windows OS and paste to your system default font path, and try the conversion again? Hopefully it will resolve the issue.
You can also use fonts from a custom folder, you just need to add that folder path into LocalFontPath as following. You can use following methods to get system folder of fonts or set font path to font folders.
- Document.getLocalFontPath () - shows the system folder in which project will look for fonts.
- Document.setLocalFontPath (String) - Setting font path to custom folder
// Set font folder path<o:p></o:p>
String path = “/home/tilal/fonts/”;<o:p></o:p>
// Adding a single font directory<o:p></o:p>
// com.aspose.pdf.Document.addLocalFontPath(path);<o:p></o:p>
// setting the user list for standard font directories<o:p></o:p>
java.util.List list = com.aspose.pdf.Document.getLocalFontPaths();<o:p></o:p>
list.add(path);<o:p></o:p>
com.aspose.pdf.Document.setLocalFontPaths(list);<o:p></o:p>
…<o:p></o:p>
…
We are sorry for the inconvenience caused.
Best Regards,
We use RHEL which should be very similar to CentOS. I checked /usr/share/fonts and we have no msttcorefonts folder there. Are these fonts required? What fonts must be there? Is there any standard RH package for this? I checked and there is RPM package msttcorefonts-2.x.x so I would find corresponding package for our RH version. Please confirm and I will ask our admin to install this package. Thanks.
Hi Robert,
Thanks for your inquriy. You may install Microsoft fonts using RPM package on RHEL as suggested on following documentation link or can copy True Type fonts from your Windows machine and copy to default font folder path.
Best Regards,
Hi Robert,
When manipulating PDF files, the fonts used inside PDF file need to be installed over system, so that respective font used in document is loaded during manipulation. However we are further looking into this matter and will get back to you soon.
Yes adding MS fonts to system fixed issue. It would be good to improve diagnostic message for missing fonts as current exception is useless to find out cause. Thanks for help.
Hi Robert,
Thanks for your feedback. It is good to know that you have managed to resolved the issue by installing Microsoft fonts. However as requested we have logged an investigation ticket PDFNEWJAVA-35686 to improve the exception message for missing fonts. We will notify you as soon as it is resolved.
We are sorry for the inconvenience.
Best Regards,
@mslama2
Thanks for your patience.
We are pleased to inform you that earlier logged issue PDFJAVA-35686 has been resolved in Aspose.PDF for Java 18.1. Please check following code snippet reproducing new exception - which has been implemented: i.e FontNotFoundException
//Creating the template
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document();
Page p = pdfDocument.getPages().add();
TextFragment tf = new TextFragment("Aspose");
tf.getTextState().setFont(FontRepository.findFont("Times New Roman"));
p.getParagraphs().add(tf);
pdfDocument.save(myDir + "HelloWorld.pdf");
Document.setLocalFontPaths(new ArrayList());
// Open document
pdfDocument = new com.aspose.pdf.Document(myDir + "HelloWorld.pdf");
// Create TextAbsorber object to find all instances of the input search phrase
com.aspose.pdf.TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber("Aspose");
// Accept the absorber for first page of document
pdfDocument.getPages().accept(textFragmentAbsorber);
// Get the extracted text fragments into collection
com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
// Loop through the fragments
for (com.aspose.pdf.TextFragment textFragment : (Iterable<com.aspose.pdf.TextFragment>)textFragmentCollection)
{
// Update text and other properties
textFragment.setText("New Pharase");
}
// Save the updated PDF file
pdfDocument.save(myDir + "Updated_Text" + version + ".pdf");
After executing above code snippet, following exception will be received:
com.aspose.pdf.exceptions.FontNotFoundException: TimesNewRoman
Also, please use the following code snippet to substitute the absent fonts:
public void test() throws java.lang.Exception
{
initLicense();
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document();
Page p = pdfDocument.getPages().add();
TextFragment tf = new TextFragment("Aspose");
tf.getTextState().setFont(FontRepository.findFont("Times New Roman"));
p.getParagraphs().add(tf);
pdfDocument.save(myDir + "HelloWorld.pdf");
//Set the folder with the only one font LiberationSans-Regular.ttf
Document.setLocalFontPaths(new ArrayList());
Document.addLocalFontPath(myDir);
//configure font substitution
CustomSubst1 subst1 = new CustomSubst1();
FontRepository.getSubstitutions().add(subst1);
// Open document
pdfDocument = new com.aspose.pdf.Document(myDir + "HelloWorld.pdf");
// Create TextAbsorber object to find all instances of the input search phrase
com.aspose.pdf.TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber("Aspose");
// Accept the absorber for first page of document
pdfDocument.getPages().accept(textFragmentAbsorber);
// Get the extracted text fragments into collection
com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
// Loop through the fragments
for (com.aspose.pdf.TextFragment textFragment : (Iterable<com.aspose.pdf.TextFragment>)textFragmentCollection)
{
// Update text and other properties
textFragment.setText("New Pharase");
}
// Save the updated PDF file
pdfDocument.save(myDir + "Updated_Text" + version + "new.pdf");
}
private static class CustomSubst1 extends com.aspose.pdf.text.CustomFontSubstitutionBase
{
public boolean trySubstitute(com.aspose.pdf.text.CustomFontSubstitutionBase.OriginalFontSpecification originalFontSpecification, /*out*/ com.aspose.pdf.Font[] substitutionFont)
{
try
{
substitutionFont[0] = FontRepository.findFont("Liberation Sans"); ;
}
catch (java.lang.NullPointerException e)
{
return false;
}
return true;
}
}
In case of any further assistance, please feel free to contact us.
Hi Asad,
I used your code snippet to substitute the absent fonts, but still getting “Font Times New Roman was not found” exception. I took your code and tested it UNIX box. The only change I made to your code is:
- I removed pdfDocument.save(myDir + “HelloWorld.pdf”); line.
- I removed pdfDocument = new com.aspose.pdf.Document(myDir + “HelloWorld.pdf”);
- I updated Document.addLocalFontPath(myDir); line with Document.addLocalFontPath("/usr/share/fonts/liberation/"); because I see LiberationSans-Bold.ttf under /usr/share/fonts/liberation/LiberationSans-Bold.ttf in UNIX box
- I updated pdfDocument.save(myDir + “Updated_Text” + version + “new.pdf”); with
ByteArrayOutputStream output = new ByteArrayOutputStream();
pdfDocument.save(output);
Why I am still getting the exception?
@senthil125
Thanks for contacting support.
Would you please make sure if MS Fonts are installed in your environment. Please try your scenario again after installing MS Fonts package and in case you still face any issue, please share your complete environment details (i.e OS Version, Application Type, JDK Version, etc.). We will test the scenario in our environment and address it accordingly.
We are trying to figure out if there is a way to substitute a linux font for a MS specific font, because MS Fonts have copyright issues.
@senthil125
Thanks for writing back.
You may try copying MS Fonts from Windows machine and installing in your environment by following instructions over “How to install TrueType Fonts on Linux”. However in case it does not suit you, as requested earlier, please share your complete environment details (i.e OS Version, Application Type, JDK Version, etc.). We will test the scenario in our environment and address it accordingly.
Hi Asad,
We decided to use Lucida Sans font which is supported by our UNIX box.
Here is what I am trying to do: I have one PDF file serving as a template. This PDF file has multiple lines of sentences and saved in the database.
There are some words in the sentences that need to be replaced by dynamic string passed by application to my PDF editer.
Example: Couple of sentences in the PDF:
I want to replace #borrowerName from this line.
My name is #borrowerName.
What is #borrowerName’s role at Aspose?
I have two applications
- RESTful webservices which takes PDF file as stream, #borrowerName and dynamic text to replace #borrowerName.
- Web application that consumes RESTful webservices. Web application gets PDF file from Database and calls RESTful.
My initial test was to replace #borrowerName with “Ramesh Test” in the PDF and it was working. Still it is working. But when I try to replace #borrowerName with other names like “Carl Bell”, “Batman” I am getting this exception.
com.aspose.pdf.internal.ms.System.z106: Specified method is not supported.
Why Aspose PDF editor replaces #borrowerName with “Ramesh Test”, but not support other names for me?
Thanks,
@senthil125
Thanks for sharing all the details about the scenario.
We would really appreciate if you can please share your sample PDF document with us. We will test the scenario in our environment and address it accordingly.
Hi Asad,
Here is the file I am working on. I wan to replace all place holders like #orderID, #companyName, #borrowerName in the document. templateVOE.pdf (59.9 KB)
@senthil125,
We are working over your query and will get back to you soon.
@imran.rafique,
Do you have update for my query?
@senthil125
Thanks for your inquiry.
We are working over setting up the environment, in order to test the scenario. We will try to complete setting up specific environment and test your scenario during this weekend. As soon as we have some testing results, we will share with you. Please spare us little time.
We are sorry for this inconvenience.
@senthil125
Thanks for your patience.
We have tested the scenario in Linux based environment using Aspose.PDF for Java 18.3 and were unable to notice any issue. We were able to replace the #borrowerName text with ‘Batman’ OR ‘Carl Bell’. However, we have set fonts path as follows in the code snippet, which was shared above:
Document.addLocalFontPath("/usr/share/fonts/");
Moreover, we have used fonts other than Lucida Sans as well, while testing the scenario. For your kind reference, an output PDF document is also attached.
Updated_Text_new.pdf (96.2 KB)
Would you please set the font path as above in your environment and try replacing the word again. In case you still face any issue, please share a sample console application containing narrowed down code snippet which is able to replicate the issue. We will again test the scenario in our environment and address it accordingly.
Asad,
The above code you are mentioning is creating blank PDF document and adds page to it. Then adds the text to be replaced. But my scenario is to create PDF with InputStream of the pdf file I shared with you. Can you please send me your complete code for my scenario?
Another reminder: I don’t have MS Fonts on my UNIX box.