Hello,
In our program aspose-words-18.2-jdk16.jar is used to generate pdf files. There is a letter template in .doc format which has a line with OCR A Extended font.
When it is saved as PDF this font is changed to Ming Li U. We have OCR A Extended installed on the server, but that does not work.
Here is part of the code to save as pdf. What should we do to keep the OCR A Extended font? Our client needs that for their letter.
ByteArrayInputStream bdyTmplStream = new ByteArrayInputStream(letterContent);
Document tmplDoc = new Document(bdyTmplStream);
ByteArrayOutputStream mergedOutputStream = new ByteArrayOutputStream();
PdfSaveOptions opt = new PdfSaveOptions();
opt.setExportDocumentStructure(true);
opt.setEmbedFullFonts(true);
if(fontDir != null && (new File(fontDir)).isDirectory()){
FontSettings.getDefaultInstance().setFontsFolder(fontDir, true);
}
tmplDoc.save(mergedOutputStream, opt);
Thanks,
Farnaz
@farnaz2021
To ensure that the OCR A Extended font is preserved when generating PDF files from a .doc template using Aspose.Words, you can follow these steps:
-
Check Font Installation: First, confirm that the OCR A Extended font is correctly installed on the server where the application is running. Sometimes, the font may not be recognized due to installation issues.
-
Set Font Directory: In your code, you are already setting the font directory using FontSettings.getDefaultInstance().setFontsFolder(fontDir, true);
. Make sure that the fontDir
variable points to the correct directory where the OCR A Extended font is located.
-
Embed Full Fonts: You are using the PdfSaveOptions
and setting opt.setEmbedFullFonts(true);
. This is correct as it ensures that all fonts used in the document are embedded in the PDF. This is crucial for maintaining the appearance of the document.
-
Use the Correct Save Options: Ensure that you are using the correct save options when saving the document. Here’s a sample code snippet that incorporates these suggestions:
ByteArrayInputStream bdyTmplStream = new ByteArrayInputStream(letterContent);
Document tmplDoc = new Document(bdyTmplStream);
ByteArrayOutputStream mergedOutputStream = new ByteArrayOutputStream();
PdfSaveOptions opt = new PdfSaveOptions();
opt.setExportDocumentStructure(true);
opt.setEmbedFullFonts(true); // Ensure all fonts are embedded
// Set the font directory if it is not null and is a directory
if (fontDir != null && (new File(fontDir)).isDirectory()) {
FontSettings.getDefaultInstance().setFontsFolder(fontDir, true);
}
// Save the document as PDF
tmplDoc.save(mergedOutputStream, opt);
-
Test with Different Fonts: If the issue persists, try testing with a different font to see if the problem is specific to the OCR A Extended font. This can help isolate the issue.
By following these steps, you should be able to maintain the OCR A Extended font in the generated PDF. If the problem continues, consider checking for any specific font licensing issues or compatibility problems with the Aspose.Words version you are using.
If you need further assistance, feel free to ask!
Sources:
[1]: Embed Fonts in PDF Document | Aspose.Words Document Processing API
Thanks for the response. We have done all these, but still have the issue. I tested with another font (Impact font) and that worked fine. What else do you suggest to try?
@farnaz2021 Could you please attach your input and output documents here for testing? We will check conversion on our side and provide you more information.
The problem on your side might occur because the fonts used the documents are not available in the environment where documents are converted. To build an accurate document layout the fonts are required. If Aspose.Words cannot find the fonts used in the document the fonts are substituted. This might lead into the layout differences due to differences in fonts metrics. You can implement IWarningCallback to get a notification when font substitution is performed.
The following articles can be useful for you:
https://docs.aspose.com/words/java/specify-truetype-fonts-location/
https://docs.aspose.com/words/java/install-truetype-fonts-on-linux/
I have uploaded the template document and generated pdf file to the bellow link. Also you can found the font used OCRAEXT.TTF in that link. This font in installed in our sever where the pdf is generated.
Please Note → The line with the font issue is the last line of page 1 of the document.
https://app.box.com/s/tew7zahvlvpt4kvwie34jcmvdj6ztck0
@farnaz2021 I cannot reproduce the problem on my side using the latest 24.10 version of Aspose.Words. Here are output documents produced on my side
Aspose.Words: out.pdf (147.6 KB)
MS Word: ms.pdf (151.6 KB)
Please try using the latest 24.10 version on your side and let us know if the problem still persists.
Thanks for your reply. Is the issue reproducible for you by using aspose-words-18.2-jdk16.jar? We may not be ready to upgrade at this point and want to see if there is any other possible cause.
@farnaz2021 The problem is not reproducible with 18.2 version on my side:
out_18.2.pdf (146.1 KB)
Hi Alexy, I wrote a very simple program to read a .doc file with one line with the OCR A Extended font and then using aspose-words to save it as pdf. This still has the font issue. Please see attached zip folder with the instruction. Could you please let me if there is anything I can change in the code to fix the issue?
Font issue.zip (9.8 MB)
@farnaz2021 Could you please try implementing IWarningCallback on your side and check whether font substitution is performed. The problem is still not reproducible on my side. Here is the produced output:
out.pdf (6.9 KB)