|
| 1 | +--- |
| 2 | +title: How to Eliminate Formatting Issues when Exporting XLSX to PDF Format |
| 3 | +description: This article provides a solution to formatting difficulties when exporting an XLSX file to PDF using the RadSpreadProcessing library. |
| 4 | +type: troubleshooting |
| 5 | +page_title: How to Eliminate Formatting Issues when Exporting XLSX to PDF Format |
| 6 | +slug: exporting-xlsx-to-pdf-formatting-issues |
| 7 | +tags: excel, pdf, font, width, column, standard |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +##Environment |
| 12 | +| Version | Product | Author | |
| 13 | +| --- | --- | ---- | |
| 14 | +| .NET Standard | RadSpreadProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 15 | + |
| 16 | +## Description |
| 17 | +This article demonstrates how to deal with formatting difficulties when exporting an XLSX file to PDF using the RadSpreadProcessing library in .NET Standard. |
| 18 | + |
| 19 | +The most common scenario is: |
| 20 | +1. [Export an XLSX file to PDF]({%slug radspreadprocessing-formats-and-conversion-pdf-pdfformatprovider%}) using the RadSpreadProcessing library. |
| 21 | +2. Observe the resulting PDF file with truncated columns or different font. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## Solution |
| 26 | +The limitations of .NET Standard may cause differences in the font and text size (text measuring) when converting to PDF format. |
| 27 | + |
| 28 | +1\. To address the issue with the size discrepancy, set a [SpreadFixedTextMeasurer]({%slug radspreadprocessing-cross-platform-text-measure%}}) to handle the problem with the size: |
| 29 | + |
| 30 | +```csharp |
| 31 | + SpreadTextMeasurerBase fixedTextMeasurer = new SpreadFixedTextMeasurer(); |
| 32 | + SpreadExtensibilityManager.TextMeasurer = fixedTextMeasurer; |
| 33 | +``` |
| 34 | + |
| 35 | +2\. Implement a [FontsProvider]({%slug pdfprocessing-implement-fontsprovider%}) to handle differences in fonts. |
| 36 | + |
| 37 | +```csharp |
| 38 | + FontsProviderBase fontsProvider = new FontsProvider(); |
| 39 | + FixedExtensibilityManager.FontsProvider = fontsProvider; |
| 40 | +``` |
| 41 | + |
| 42 | +This class provides a mechanism to read the fonts used in the document: |
| 43 | + |
| 44 | +```csharp |
| 45 | + public class FontsProvider : Telerik.Windows.Documents.Extensibility.FontsProviderBase |
| 46 | + { |
| 47 | + public override byte[] GetFontData(Telerik.Windows.Documents.Core.Fonts.FontProperties fontProperties) |
| 48 | + { |
| 49 | + string fontFileName = fontProperties.FontFamilyName + ".ttf"; |
| 50 | + string fontFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts); |
| 51 | + |
| 52 | + //The fonts can differ depending on the file |
| 53 | + if (fontProperties.FontFamilyName == "Trebuchet MS") |
| 54 | + { |
| 55 | + if (fontProperties.FontStyle == FontStyles.Italic && fontProperties.FontWeight == FontWeights.Bold) |
| 56 | + { |
| 57 | + fontFileName = $"trebucbi.ttf"; |
| 58 | + } |
| 59 | + else if (fontProperties.FontStyle == FontStyles.Italic) |
| 60 | + { |
| 61 | + fontFileName = $"trebucit.ttf"; |
| 62 | + } |
| 63 | + else if (fontProperties.FontWeight == FontWeights.Normal) |
| 64 | + { |
| 65 | + fontFileName = "trebuc.ttf"; |
| 66 | + } |
| 67 | + else if (fontProperties.FontWeight == FontWeights.Bold) |
| 68 | + { |
| 69 | + fontFileName = $"trebucbd.ttf"; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + //...add more fonts if needed... |
| 75 | +
|
| 76 | + DirectoryInfo directory = new DirectoryInfo(fontFolder); |
| 77 | + FileInfo[] fontFiles = directory.GetFiles(); |
| 78 | + |
| 79 | + var fontFile = fontFiles.FirstOrDefault(f => f.Name.Equals(fontFileName, StringComparison.InvariantCultureIgnoreCase)); |
| 80 | + if (fontFile != null) |
| 81 | + { |
| 82 | + var targetPath = fontFile.FullName; |
| 83 | + using (FileStream fileStream = File.OpenRead(targetPath)) |
| 84 | + { |
| 85 | + using (MemoryStream memoryStream = new MemoryStream()) |
| 86 | + { |
| 87 | + fileStream.CopyTo(memoryStream); |
| 88 | + return memoryStream.ToArray(); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return null; |
| 94 | + } |
| 95 | + } |
| 96 | +``` |
| 97 | +Now, the font in the exported PDF document is the correct one and the text is not clipped. |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +## See Also |
| 102 | +- [Cross-Platform Support]({%slug radspreadprocessing-cross-platform%}) |
| 103 | +- [Text Measuring]({%slug radspreadprocessing-cross-platform-text-measure%}) |
| 104 | +- [Fonts in PdfProcessing]({%slug radpdfprocessing-cross-platform-fonts%}) |
| 105 | +- [How to implement FontsProvider]({%slug pdfprocessing-implement-fontsprovider%}) |
0 commit comments