Skip to content

Commit 400d98d

Browse files
Merge pull request #398 from telerik/new-kb-exporting-xlsx-to-pdf-formatting-issues-f3c6937b7d494d70b65394ac207b1a45
Added new kb article exporting-xlsx-to-pdf-formatting-issues
2 parents 9e103b6 + cf8136b commit 400d98d

File tree

6 files changed

+110
-3
lines changed

6 files changed

+110
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
![Export Differences](images/exporting-xlsx-to-pdf-formatting-issues01.png)
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+
![Hande Export Differences](images/exporting-xlsx-to-pdf-formatting-issues02.png)
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%})
Loading
Loading

libraries/radpdfprocessing/cross-platform/fonts.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ You can find a detailed **FixedExtensibilityManager** and **FontsProvider** desc
2020

2121
>important If the FontsProvider property is not set, a default font will be used when exporting the document.
2222
23+
## See Also
24+
- [How to Eliminate Formatting Issues when Exporting XLSX to PDF Format]({%slug exporting-xlsx-to-pdf-formatting-issues%})

libraries/radspreadprocessing/cross-platform-support/text-measure.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ You can assign any **SpreadTextMeasurerBase** implementation to the **SpreadExte
109109

110110

111111
## See Also
112-
112+
* [How to Eliminate Formatting Issues when Exporting XLSX to PDF Format]({%slug exporting-xlsx-to-pdf-formatting-issues%})
113113
* [Cross-Platform Support]({%slug radspreadprocessing-cross-platform%})
114114
* [Using XlsxFormatProvider]({%slug radspreadprocessing-formats-and-conversion-xlsx-xlsxformatprovider%})
115115
* [Using PdfFormatProvider]({%slug radspreadprocessing-formats-and-conversion-pdf-pdfformatprovider%})

libraries/radspreadprocessing/formats-and-conversion/pdf/pdfformatprovider.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ The result from the export method is a document that can be opened in any applic
6969
>tip __RadFixedDocument__ is the base class of the __RadPdfProcessing__ library. Additional information on the library and its functionality can be found [here]({%slug radpdfprocessing-overview%}).
7070
7171
## See Also
72-
73-
* [Import/Load and Export/Save RadSpreadProcessing Workbook]({%slug import-export-save-load-workbook%})
72+
- [How to Eliminate Formatting Issues when Exporting XLSX to PDF Format]({%slug exporting-xlsx-to-pdf-formatting-issues%})
73+
- [Import/Load and Export/Save RadSpreadProcessing Workbook]({%slug import-export-save-load-workbook%})
7474

0 commit comments

Comments
 (0)