Docx to PDF Conversion Loses Footer and Checkboxes

1 Answer 91 Views
PdfProcessing WordsProcessing
Matthew
Top achievements
Rank 1
Matthew asked on 07 Nov 2022, 06:13 PM

When I convert a document from .docx to .PDF, the footer disappears.  There are a couple checkboxes in the docx that disappear as well.  Does this library support these things?  Is there something specific I have to do to make them work?

 

My code:


                RadFlowDocument document;

                Telerik.Documents.ImageUtils.ImagePropertiesResolver defaultImagePropertiesResolver = new Telerik.Documents.ImageUtils.ImagePropertiesResolver();
                Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver = defaultImagePropertiesResolver;

                using (FileStream input = new FileStream("file.bin", FileMode.Create, System.IO.FileAccess.ReadWrite))
                {
                    input.Write(doc.Content, 0, doc.Content.Count());

                    DocxFormatProvider provider = new DocxFormatProvider();
                    document = provider.Import(input);

                    //insert the data
                    RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);

                    foreach (var item in templateValues)
                    {
                        editor.ReplaceText(item.Key, item.Value);
                    }

                    //change the value in the footer
                    editor.ReplaceText("[Document Revised Date]", doc.lastModified.ToShortDateString());

                    PdfFormatProvider pdfProvider = new PdfFormatProvider();
                    var result = pdfProvider.Export(document);

                    return result;

1 Answer, 1 is accepted

Sort by
0
Peshito
Telerik team
answered on 10 Nov 2022, 03:03 PM

Hello Matthew,

Did you use content controls for your checkbox? If that so, it is most likely lost on export due to a missing font. The .NET Standard version of the suite does not have aces to the system fonts. PdfProcessing needs to have access to the font data so that it can read it and add it to the PDF file. That is why to allow the library to create and use fonts, you will need to provide an implementation of the FontsProviderBase abstract class and set this implementation to the FontsProvider property of FixedExtensibilityManager. For instance:

            Telerik.Windows.Documents.Extensibility.FontsProviderBase fontsProvider = new FontsProvider();
            Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider = fontsProvider;

Implementation of the FontsProviderBase class:

public class FontsProvider : Telerik.Windows.Documents.Extensibility.FontsProviderBase
    {
        private readonly string fontFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);

        public override byte[] GetFontData(FontProperties fontProperties)
        {
            string fontFamilyName = fontProperties.FontFamilyName;
            bool isItalic = fontProperties.FontStyle == FontStyles.Italic;
            bool isBold = fontProperties.FontWeight == FontWeights.Bold;
            
            if (fontFamilyName == "MS Gothic")
            {
                return this.GetFontDataFromFontFolder("msgothic.ttc");
            }

            return null;
        }

        private byte[] GetFontDataFromFontFolder(string fontFileName)
        {
            using (FileStream fileStream = File.OpenRead(this.fontFolder + "\\" + fontFileName))
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    fileStream.CopyTo(memoryStream);
                    return memoryStream.ToArray();
                }
            }
        }
    }

I noticed that MS Gothic is the font used for drawing the checkbox comparing it with the NET Framework version of our library.

Hope this helps.

Regards,
Peshito
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Matthew
Top achievements
Rank 1
commented on 10 Nov 2022, 06:06 PM

Thank you so much!  This definitely fixed the issue with the checkboxes disappearing.

Do you have any advice for the document footer?  That, too, does not come across when converting docx to PDF.

Peshito
Telerik team
commented on 11 Nov 2022, 11:57 AM

Hi Matthew, 

I am glad the offered solution helped and the disappearing checkboxes are visible now.

In order to assist you with the second issue I will need the document in question. You could either upload it to third-party web storage or submit a support ticket and attach it there. 

Regards,
Peshito
Progress Telerik
Tags
PdfProcessing WordsProcessing
Asked by
Matthew
Top achievements
Rank 1
Answers by
Peshito
Telerik team
Share this question
or