Open-XML-SDK
Open-XML-SDK copied to clipboard
Supplying a value for EmbedRegularFont.FontKey corrupts DOCX output
Describe the bug
I am attempting to embed TTF fonts in a DOCX file. In order to be compatible with a third-party library that is able to update the Table of Contents page, it appears I must also provide a value for the EmbedRegularFont.FontKey property. However, supplying a value for the FontKey elicits an error from Microsoft Word and the embedded fonts are no longer presented in the application. The Open XML SDK Productivity tool also elicits an error.
Am I supplying erroneous values for FontKey? Where would I find the correct values?
Screenshots Word error:
Word found unreadable content in "report.docx". Do you want to recover the contents of this document? If you trust the source of this document, click Yes.
Word error, after clicking "Yes":
Word experienced an error trying to open the file. Try these suggestions. * Check the file permissions for the document or drive. * Make sure there is sufficient free memory and disk space. * Open the file with the Text Recovery Converter.
Open XML SDK 2.5 Productivity Tool for Microsoft Office error:
Cannot open the file: Specified part does not exist in the package.
To Reproduce // Please add a self-contained, minimum viable repro of the issue. // If you require external resources, please provide a gist or GitHub repro // An Xunit style test is preferred, but a console application would work too.
Steps to reproduce the behavior:
Un-comment the line (below) to set the FontKey.
private static async Task GenerateFontPartContent(FontPart fontPart, string path)
{
var fontPath = Path.Join(assemblyLocation, "Assets", "Fonts", path);
await using FileStream fontData = new FileStream(fontPath, FileMode.Open, FileAccess.Read);
fontPart.FeedData(fontData);
}
FontTablePart fontTablePart = document.AddNewPart<FontTablePart>("rId10");
FontPart fontPart9 = fontTablePart.AddNewPart<FontPart>("application/vnd.openxmlformats-officedocument.obfuscatedFont", "rId9");
await GenerateFontPartContent(fontPart9, "Manrope-Regular.ttf");
Font font7 = new Font(){ Name = "Manrope" };
FontCharSet fontCharSet7 = new FontCharSet(){ Val = "01" };
FontFamily fontFamily7 = new FontFamily(){ Val = FontFamilyValues.Auto };
Pitch pitch7 = new Pitch(){ Val = FontPitchValues.Variable };
EmbedRegularFont embedRegularFont = new EmbedRegularFont() { Id = "rId9" };
// embedRegularFont.FontKey = "{Manrope}";
fonts1.Append(font7);
fontTablePart.Fonts = fonts1;
Observed behavior Microsoft Word and the Productivity Tool report they cannot open the DOCX file.
After Microsoft Word recovers the file, embedded fonts are no longer presented in the application.
Expected behavior Applications should open the file without error. Embedded fonts should continue to be presented.
Desktop (please complete the following information):
- OS: Windows 11
- Office version: 16.0.17231.20194
- .NET Target: .NET 8.0 ASP.NET
- DocumentFormat.OpenXml Version: 3.0.1
I suspect the error messages elicited by Word and the productivity tool are appropriate. The third-party library I was referring to in the original report claims to have addressed whatever was requiring the FontKey to be provided. I have not had the time to verify this, yet.