Skip to content

Commit d019d72

Browse files
Merge branch 'master' into new-kb-inserting-special-symbols-pdf-radpdfprocessing-edb732d7e0be4da39da3676e06ecdab3
2 parents 3f9060c + 3f4a658 commit d019d72

File tree

10 files changed

+223
-4
lines changed

10 files changed

+223
-4
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
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
title: Populate a Table with Data using Nested Mail Merge Functionality
3+
description: Learn how to populate a table with data using the Nested Mail Merge functionality.
4+
type: how-to
5+
page_title: Populate a Table with Data using Nested Mail Merge Functionality
6+
slug: populate-table-data-mail-merge
7+
tags: mail, merge, mailmerge, word, table, data, nested
8+
res_type: kb
9+
---
10+
11+
# Environment
12+
13+
| Version | Product | Author |
14+
| --- | --- | ---- |
15+
| 2024.1.124 | RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
16+
17+
# Description
18+
19+
Learn how to generate a Word (.DOCX) document that contains a [Table]({%slug radwordsprocessing-model-table%}) with a header row and an item row merge field. Then, passing a collection of records automatically creates and populates the data rows.
20+
21+
# Solution
22+
23+
To achieve the desired result, you can use the [Nested Mail Merge]({%slug radwordsprocessing-editing-mail-merge%}) functionality that [RadWordsProcessing]({%slug radwordsprocessing-overview%}) offers and populate the data rows automatically.
24+
25+
Here's a sample code snippet that demonstrates how to achieve this:
26+
27+
```csharp
28+
internal class Program
29+
{
30+
static void Main(string[] args)
31+
{
32+
List<Player> players = GetPlayers();
33+
List<PlayersHolder> playersHolders = new List<PlayersHolder>() { new PlayersHolder(players) };
34+
RadFlowDocument document = new RadFlowDocument();
35+
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
36+
37+
Table table = editor.InsertTable(2, 2);
38+
39+
table.Rows[0].Cells[0].Blocks.AddParagraph().Inlines.AddRun("First Name");
40+
table.Rows[0].Cells[1].Blocks.AddParagraph().Inlines.AddRun("Last Name");
41+
TableRow row = table.Rows[1];
42+
var firstNameParagraph = table.Rows[1].Cells[0].Blocks.AddParagraph();
43+
editor.MoveToParagraphStart(firstNameParagraph);
44+
editor.InsertField("MERGEFIELD TableStart:Players", "");
45+
editor.InsertField("MERGEFIELD FirstName", "");
46+
47+
var lastNameParagraph = table.Rows[1].Cells[1].Blocks.AddParagraph();
48+
editor.MoveToParagraphStart(lastNameParagraph);
49+
editor.InsertField("MERGEFIELD LastName", "");
50+
editor.InsertField("MERGEFIELD TableEnd:Players", "");
51+
52+
table.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);
53+
Border border = new Border(1, BorderStyle.Single, new ThemableColor(Colors.Black));
54+
table.Borders = new TableBorders(border);
55+
56+
editor.MoveToTableEnd(table);
57+
58+
editor.InsertParagraph();
59+
RadFlowDocument mergedDocument = document.MailMerge(playersHolders);
60+
string resultFileNameDocx = "merged.docx";
61+
File.Delete(resultFileNameDocx);
62+
DocxFormatProvider provider = new DocxFormatProvider();
63+
using (var output = File.OpenWrite(resultFileNameDocx))
64+
{
65+
provider.Export(mergedDocument, output);
66+
}
67+
Process.Start(resultFileNameDocx);
68+
}
69+
70+
public static List<Player> GetPlayers()
71+
{
72+
var Players = new List<Player>();
73+
74+
Players.Add(new Player() { FirstName = "John", LastName = "Baker" });
75+
Players.Add(new Player() { FirstName = "Sam ", LastName = "Wayne" });
76+
Players.Add(new Player() { FirstName = "Patrick", LastName = "Gibbs" });
77+
Players.Add(new Player() { FirstName = "Oscar", LastName = "Stevens" });
78+
Players.Add(new Player() { FirstName = "Larry", LastName = "Vodden" });
79+
80+
return Players;
81+
}
82+
}
83+
84+
public class PlayersHolder
85+
{
86+
public PlayersHolder(List<Player> players)
87+
{
88+
Players = players;
89+
}
90+
91+
public List<Player> Players { get; set; }
92+
}
93+
94+
public class Player {
95+
public string FirstName { get; set; }
96+
public string LastName { get; set; }
97+
}
98+
```
99+
100+
This code snippet will produce the desired result, merging the data into the table in the Word document.
101+
102+
![Populate Table Data](images/populate-table-data-mail-merge.png)
103+
104+
105+
If you skip the MailMerge step, the following template will be produced:
106+
107+
![Populate Table Data](images/populate-table-data-mail-merge-original.png)
108+
109+
## See Also
110+
111+
* [Nested Mail Merge]({%slug radwordsprocessing-editing-mail-merge%})
112+
* [Table]({%slug radwordsprocessing-model-table%})

libraries/radpdfprocessing/cross-platform/fonts.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ You can find a detailed **FixedExtensibilityManager** and **FontsProvider** desc
2525
* [Standard Fonts]({%slug radpdfprocessing-concepts-fonts%})
2626
* [Cross-Platform Support]({%slug radpdfprocessing-cross-platform%})
2727
* [Inserting Special Symbols in PDF using RadPdfProcessing]({%slug inserting-special-symbols-pdf-radpdfprocessing%})
28-
* [How to Eliminate Formatting Issues when Exporting XLSX to PDF Format]({%slug exporting-xlsx-to-pdf-formatting-issues%})
28+
* [How to Eliminate Formatting Issues when Exporting XLSX to PDF Format]({%slug exporting-xlsx-to-pdf-formatting-issues%})
29+

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

libraries/radwordsprocessing/editing/mail-merge.md

+1
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,4 @@ If you want to separate the items into several rows you need to close the group
215215
* [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%})
216216
* [Hiding MailMerge Line in Output Word Document If Blank]({%slug hide-mailmerge-line-output-word-document-if-blank%})
217217
* [Inserting Images using Mail Merge]({%slug inserting-images-using-mail-merge-radwordsprocessing%})
218+
* [Populate a Table with Data using Nested Mail Merge Functionality]({%slug populate-table-data-mail-merge%})

0 commit comments

Comments
 (0)