Aspose word to pdf c# - header content section from word not converting proplery to pdf

while converting the word document to pdf we are facing issue with header section in word, it is not converting properly in pdf. However it is showing proper in word docx file.

Header content is cropped in pdf, not sure why it is not working.

@Vilas1627 Could you please attach your input and output documents here for testing? We will check the issue and provide you more information. Unfortunately, it is impossible to tell what causes the problem without your problematic documents.

Hi @alexey.noskov

Thanks for checking.

Here are the attached DOCX and PDF files.

And we are using aspose word to pdf code as below in C#

Aspose.Words.License lic = new Aspose.Words.License();
lic.SetLicense(HttpContext.Current.Server.MapPath("~/Aspose.Wordsfor.NET.lic");
var doc = new Aspose.Words.Document(inputWordFile);
doc.Save(outputFilePath);

@Vilas1627 The problem is not reproducible using the latest 24.5 version of Aspose.Words. Here is PDF document produced on my side: out.pdf (101.5 KB)

So, please try using the latest version and let us know if the problem is still persist on your side.

Hi @alexey.noskov

Thanks for your quick reply.

I have just updated the version from 23. to 24.5 and loaded latest DLLs changes on server as well.

Still facing same issue.

FYI, while converting the same document on your demo site, giving same issue.

https://products.aspose.app/words/conversion#

@Vilas1627 Most likely the problem on your side occurs because the fonts used in the document are not available in the environment where the document is converted to PDF. Please note Aspose.Words needs the fonts used in the document to build document layout upon conversion to PDF. If Aspose.Words cannot find the font used in the document, the font is substituted. This might lead into fonts mismatch and document layout differences due to the different fonts metrics. You can implement IWarningCallback to get notifications when font substitution is performed.
In your case you can copy the required fonts into your Docker image. Please see our documentation to learn how to specify location where Aspose.Words will look for fonts:
https://docs.aspose.com/words/net/specifying-truetype-fonts-location/

Hi @alexey.noskov

As per my understanding it is not issue with font as it is converted to proper font style.

Seems issue with header content margin or padding while converting.

Please clear me if I am worng.

How you printed the document from your end? You mentioned in previous reply it was working fine at your end.

How it worked, can you please let me know what changes you did or what code you applied while converting.

Becuase using your live demo site for conversion it is still not working properly.

@Vilas1627 I used the following simple code for testing:

Document doc = new Document(@"C:\Temp\in.docx");
doc.WarningCallback = new FontSubstitutionWarningCallback();
doc.Save(@"C:\Temp\out.pdf");
internal class FontSubstitutionWarningCallback : IWarningCallback
{
    public void Warning(WarningInfo info)
    {
        if (info.WarningType == WarningType.FontSubstitution)
            Console.WriteLine(info.Description);
    }
}

Hi @alexey.noskov
Thanks for your reply.

Can you please let me know from where I need to download fonts to copy in my local folder, also I want to keep original font matrix as it is. Please provide me sample code for this as well as location to download fonts

Thanks

@Vilas1627 Aspose.Words is not fonts vendor and does not provide any fonts. Usually fonts are installed as a part of OS. You can learn where Aspose.Words looks for fonts in the following article:
https://docs.aspose.com/words/net/specifying-truetype-fonts-location/
Also the following article might be useful for you:
https://docs.aspose.com/words/net/installing-truetype-fonts-on-linux/

Hi @alexey.noskov

It giving the warning as below

Font ‘Garamond’ has not been found. Using ‘Yu Gothic’ font instead. Reason: font info substitution.

To handle this warning of missing font -
I have applied the required code to replace the missing font by default font - Arial

But still it is not working it exporting the PDF with breaking header content.

Below is the code that I have used to replace the font by default - Arial font.

public class FontWarningCallback : IWarningCallback
{
    private readonly DocumentBuilder builder;
    private readonly string defaultFontName;

    public FontWarningCallback(DocumentBuilder builder, string defaultFontName)
    {
        this.builder = builder;
        this.defaultFontName = defaultFontName;
    }

    public void Warning(WarningInfo info)
    {
        if (info.WarningType == WarningType.FontSubstitution)
        {
            // Apply default font
            ApplyDefaultFont(info);
        }
    }

    private void ApplyDefaultFont(WarningInfo info)
    {
        // Get the missing font name
        string missingFontName = info.Description.Substring(info.Description.IndexOf("Font '") + 6, info.Description.IndexOf("'", info.Description.IndexOf("Font '") + 6) - info.Description.IndexOf("Font '") - 6);

        // Apply default font
        SystemMessaging.AddErrorToTable($"Font '{missingFontName}' not found. Applying default font: {defaultFontName}");
        builder.Font.Name = defaultFontName;
    }
}



public static void ConvertWordToPDF(string inputWordFile, string outputFilePath)
{
    //Convert Word File to PDF using Aspose Word Package.
    Aspose.Words.License lic = new Aspose.Words.License();
    lic.SetLicense(HttpContext.Current.Server.MapPath("Aspose.Wordsfor.NET.lic");

    // Load the document
    var doc = new Aspose.Words.Document(inputWordFile);

    // Set default font
    string defaultFont = "Arial";

    // Create a DocumentBuilder
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Create an instance of FontWarningCallback with default font
    FontWarningCallback callback = new FontWarningCallback(builder, defaultFont);

    // Register the callback
    doc.WarningCallback = callback;

    doc.Save(outputFilePath);

}

Please provide me more information, if anything missing here…

@Vilas1627 Changing the font in the document will not resolve the issue since different fonts might have different metrics and as a result document layout will be affected. To resolve the problem you should provide the missed font as described in the above provided articles.

Hi @alexey.noskov

As per your suggestion provided the required true type fonts from the custom folder.

It worked very well, thanks for your help on this.

public static void ConvertWordToPDF(string inputWordFile, string outputFilePath)
{
    //Convert Word File to PDF using Aspose Word Package.
    License lic = new License();
    lic.SetLicense(HttpContext.Current.Server.MapPath(GetWebConfigVar("PLCTAsposeLicense")) + "Aspose.Wordsfor.NET.lic");

    FontSettings.DefaultInstance.SetFontsSources(new FontSourceBase[]
     {
                new SystemFontSource(), new FolderFontSource(HttpContext.Current.Server.MapPath(GetWebConfigVar("PLCTAsposeFontFamily")), true)
     });

    // Load the document
    var doc = new Document(inputWordFile);

    // Create an instance of FontWarningCallback with default font
    FontWarningCallback callback = new FontWarningCallback();

    // Register the callback
    doc.WarningCallback = callback;

    doc.Save(outputFilePath);

}

public class FontWarningCallback : IWarningCallback
{
    public void Warning(WarningInfo info)
    {
        if (info.WarningType == WarningType.FontSubstitution)
        {
            FontWarnings.Warning(info);
            SystemMessaging.AddErrorToTable(info.Description);
        }
    }

    public WarningInfoCollection FontWarnings = new WarningInfoCollection();
}

@Vilas1627 It is perfect that you managed to resolve the problem. Please feel free to ask in case of any other issues.