While Converting Document to PDF format . We are Getting Spaces and alignment issue in generated PDF

While Converting Document to PDF format . We are Getting Spaces and alignment issue in generated PDF

We have simple .doc files which i have attached in this Topic but while save in Document as
doc.save();

It automatically Adding spaces in some places and adding in proper alignment .

Could you please help me …what could be the issuesLUX_BLKROLU001_L_0_V2_C_High.docx.docx (84.3 KB)
AML_9999_05072023.pdf (145.9 KB)

@Smital279 As I can see fonts in your output document are substituted with DejaVuSans. Most likely the problem occurs because fonts required for document rendering are not available in your Linux environment. 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 and appearance difference. 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/
Please try either installing or providing the fonts required to render the document. This should resolve the problem.

@alexey.noskov We don’t want any installation on operating System . Can we handle this using programmatically in application .

If yes , could you please provide us Sample program to handle this

@Smital279 You can put the required fonts into a folder accessible by your application and use the folder as a font source:
https://docs.aspose.com/words/java/specify-truetype-fonts-location/
Also, you can use StreamFontSource to load fonts from web for example or from JAR. See the following article for more information:
https://docs.aspose.com/words/java/specify-truetype-fonts-location/#loading-fonts-from-stream

Thanks @alexey.noskov

But could you please let me know what would be there in that Folder , I have gone through example below example but what should be folder “MyFonts” Folder

new FolderFontSource("C:\\MyFonts\\", true) });

FontSettings.getDefaultInstance().setFontsSources(
		new FontSourceBase[] { new SystemFontSource(), new FolderFontSource("C:\\MyFonts\\", true) });

Document doc = new Document(dataDir + "Rendering.doc");
dataDir = dataDir + "Rendering.SetFontsFolders_out.pdf";
doc.save(dataDir);

@Smital279 You should place the required fonts into the MyFonts folder, i.e. the required TTF, TTC etc. files.

@alexey.noskov @alexey.noskov from where we can get this file

@Smital279 On windows machine, for example, fonts are installed in C:\Windows\Fonts folder. You can download fonts from web or purchase them, for example free Noto fonts are available here.

@alexey.noskov Our application in install on Linux server .

If i mentioned Arial Font from C:\Windows\Fonts and deploy application .

Will that work ? while generation of PDF File

@Smital279 Yes, it will work. Please please see the following article:
https://docs.aspose.com/words/java/install-truetype-fonts-on-linux/

Thanks @alexey.noskov

I have uploaded FONTS on resources/fonts folder in my spring boot application such as

<resources/fonts>

arial.ttf
arialbd.ttf
arialbi.ttf
ariali.ttf

Program which i have written as . Correct me if i am wrong :-

//Enable the merge fields in template
templateDoc.getMailMerge().setUseNonMergeFields(true);
templateDoc.setFontSettings(new FontSettings());
templateDoc.getFontSettings().setFontsSources(
    new FontSourceBase[] {
        new SystemFontSource(), // Search for fonts in the default system locations
        new FolderFontSource("fonts/", true) // Search for fonts in the specified folder
    });

Can you please help me how can i apply this font to PDF file while generation ?

and how can i verify whether it is applied or not .

@Smital279 Please make sure the resources/fonts folder is accessible form your application. Also, please try specifying absolute path to the fonts folder. You can use the following code to check what fonts are loaded from the specified font sources:

Document doc = new Document("/temp/in.docx");

doc.setFontSettings(new FontSettings());
doc.getFontSettings().setFontsSources(
    new FontSourceBase[] {
        new SystemFontSource(), // Search for fonts in the default system locations
        new FolderFontSource("/temp/fonts/", true) // Search for fonts in the specified folder
    });

// Print the fonts available in the sources specified in FontSettings
for (FontSourceBase fsb : doc.getFontSettings().getFontsSources())
{
    for (PhysicalFontInfo fontInfo : fsb.getAvailableFonts())
    {
        System.out.println("FontFamilyName : " + fontInfo.getFontFamilyName());
        System.out.println("FullFontName  : " + fontInfo.getFullFontName());
        System.out.println("Version  : " + fontInfo.getVersion());
        System.out.println("FilePath : " + fontInfo.getFilePath());
    }
    System.out.println("=========================");
}

doc.save("/temp/out.pdf");

Thanks a lot for helping me to trace this issue @alexey.noskov

In my application , I want to use font which i will be passing so that while deploying application in LINUX , It will not break the font alignment issue.

So in Resources folder , i am paasing arial.ttf
arialbd.ttf
arialbi.ttf
ariali.ttf etc.

i have below queries

  1. Will this font work in LINUX server ? If not what will be the solution
  2. How can i pass resources/Font path . As per below code snippet , It is taking absolute path only

templateDoc.getFontSettings().setFontsSources(
    new FontSourceBase[] {
        new FolderFontSource("I:\\TA-Adhoc-Corres-Workspace\\ta-adhoc-correspondence\\src\\main\\resources\\fonts", true) // Search for fonts in the specified folder
    });

@Smital279 If I understand you correctly, you would like to load fonts from your JAR. Unfortunately, there is no way to specify font’s folder inside JAR, but in this case you can use StreamFontSource. See the following article for more information:
https://docs.aspose.com/words/java/specify-truetype-fonts-location/#loading-fonts-from-stream