Hello,
I am experiencing an issue when converting a DOCX file to PDF using Aspose.Words for .NET. The DOCX file contains special fonts, and I have downloaded the required fonts and placed them in a folder. This approach works for most fonts, but the fonts “Akkurat-Light” and “Akkurat-Bold” appears larger in the converted PDF, causing unexpected line breaks and formatting issues.
Here is the code I am using for the conversion:
private Stream ConvertToPdf(Stream documentStream, string documentName)
{
try
{
License awLic = new License();
awLic.SetLicense("Aspose.Words.NET.lic");
}
catch (Exception ex)
{
logger.Error("Aspose-Lizenz konnte nicht geladen werden für Word.", ex);
throw;
}
try
{
var path = HttpContext.Current.Server.MapPath("~/App_Data/hyph_de_CH.dic");
if (System.IO.File.Exists(path))
{
Hyphenation.RegisterDictionary("de-CH", path);
}
Document doc = new Document(documentStream);
// save original font sources
FontSourceBase[] originalFontSources = FontSettings.DefaultInstance.GetFontsSources();
// update font source
UpdateFontSources(originalFontSources);
doc.FieldOptions.FileName = documentName;
Stream pdfStream = new MemoryStream();
doc.Save(pdfStream, SaveFormat.Pdf);
// Restore the original font sources.
FontSettings.DefaultInstance.SetFontsSources(originalFontSources);
return pdfStream;
}
catch (Exception ex)
{
logger.Error("Fehler bei der PDF-Konvertierung.", ex);
throw;
}
}
/// <summary>
///
/// </summary>
/// <param name="originalFontSources"></param>
private void UpdateFontSources(FontSourceBase[] originalFontSources)
{
// Create a font source from a folder that contains fonts.
var fontPath = HttpContext.Current.Server.MapPath("~/App_Data/");
FolderFontSource folderFontSource = new FolderFontSource(fontPath, true);
// Apply a new array of font sources that contains the original font sources, as well as our custom fonts.
FontSourceBase[] updatedFontSources = { originalFontSources[0], folderFontSource };
FontSettings.DefaultInstance.SetFontsSources(updatedFontSources);
}
I have attached screenshots of the DOCX and PDF files for your reference.
Is there a known issue with the “Akkurat” font? Or am I doing something wrong in my code?
Thank you for your assistance.
Best regards,
Melvin
@melvinschenker Could you please attach the problematic input and output documents along with the fonts here for testing? We will check the issue and provide you more information.
Thanks for the quick response.
I justed attached a ZIP-file with the input and output documents, and my downloaded fonts from https://fontsfree.net/?s=Akkurat.
aspose-akkurat-font-problem.zip (1,2 MB)
@melvinschenker Thank you for additional information. The problem occurs because by default MS Word uses font open type features. Aspose.Words.Shaping.Harfbuzz package provides support for OpenType features in Aspose.Words using the HarfBuzz text shaping engine. You should enabling open type features to get the expected result. To achieve this you should add reference to Aspose.Words Shaping Harfbuzz
plugin and use the following code to convert your document:
Hyphenation.RegisterDictionary("de-CH", @"C:\Temp\hyph_de_CH.dic");
Document doc = new Document(@"C:\Temp\in.docx");
doc.LayoutOptions.TextShaperFactory = Aspose.Words.Shaping.HarfBuzz.HarfBuzzTextShaperFactory.Instance;
doc.Save(@"C:\Temp\out_HarfBuzz.pdf");
out_HarfBuzz.pdf (498.0 KB)
Thank you! The output is now much better, but the font is still a bit larger as it should be (see the yellow-highlighted text on the first page of your attachment).
Here is an example of how this larger font could break the formatting:
output1.pdf (180,8 KB)
input1.docx (215,4 KB)
@melvinschenker Could you please save your document to PDF using MS Word on your side and attach it here for our reference?
Yes, here it is. Thanks.
output1MSWordConvert.pdf (168,9 KB)
@melvinschenker Thank you for additional information. As I can see in the attached PDF document produced by MS Word Akkurat
font is not used. MS Word substitute it with Calibri
:
Most likely Akkurat
fonts are not installed in the environment where document is viewed in MS Word. So MS Word substitutes it with Calibri
. That is why you see the difference.
Could you please give me a few options on how I could proceed here?
@melvinschenker If you need to render the document the same way as MS Word does, i.e. substitute Akkurat
font with Calibri
, then you can simply remove Akkurat
font. In this case Aspose.Words will also substitute the font.
Thank you very much, you helped me a lot. I deleted the Akkurat fonts in my folder.
I got still one test document (the one i send you before - input1.docx / output1.pdf) which has a broken format after converting. Do you know what causes the problem there?
@melvinschenker
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-27813
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
The issues you have found earlier (filed as WORDSNET-27813) have been fixed in this Aspose.Words for .NET 25.2 update also available on NuGet.