Skip to content

Commit 75ff59a

Browse files
committed
polish article
1 parent 5cc0f76 commit 75ff59a

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed
104 KB
Loading

knowledge-base/wordsprocessing-preventing-table-row-splitting-html-pdf.md

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,44 @@ type: how-to
55
page_title: Avoiding Table Row Splitting in HTML to PDF Conversion
66
meta_title: Avoiding Table Row Splitting in HTML to PDF Conversion
77
slug: wordsprocessing-preventing-table-row-splitting-html-pdf
8-
tags: wordsprocessing,telerik document processing,html-to-pdf,table-row-splitting
8+
tags: words, processing, telerik, document, html, pdf, conversion, table, row, splitting, split, flow, word, measure, calculate
99
res_type: kb
1010
ticketid: 1700721
1111
---
1212

13-
## Environment
14-
<table>
15-
<tbody>
16-
<tr>
17-
<td> Product </td>
18-
<td>
19-
WordsProcessing for Telerik Document Processing
20-
</td>
21-
</tr>
22-
<tr>
23-
<td> Version </td>
24-
<td> Current </td>
25-
</tr>
26-
</tbody>
27-
</table>
13+
# Environment
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 2025.3.806 | RadWordsProcessing |[Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov)|
2817

2918
## Description
3019

31-
I want to convert HTML with tables containing uneven row heights to a PDF using Telerik Document Processing. My issue arises when some rows split across pages, even though I need them to stay intact. In my previous implementation using another library, `page-break-inside: avoid` handled this behavior correctly. However, this style does not seem to work with Telerik's methods.
20+
This article shows how to use [WordsProcessing]({%slug radwordsprocessing-overview%}) and [PdfProcessing]({%slug radpdfprocessing-overview%}) libraries to convert HTML with tables to a PDF, without splitting rows across pages.
3221

3322
This knowledge base article also answers the following questions:
3423
- How can I prevent table rows from splitting across pages during HTML to PDF conversion?
3524
- How do I handle uneven row heights in tables when exporting to PDF?
36-
- How can I ensure HTML table rows are preserved on a single page in Telerik Document Processing?
25+
- How can I ensure HTML table rows are preserved on a single page during HTML to PDF conversion?
3726

3827
## Solution
3928

40-
To prevent table rows from splitting across pages, manually recreate the PDF table from scratch by copying the HTML table content to a new PDF table. Use the `Measure` method to check whether the table exceeds the page boundary. If it does, create a new page and continue building the table.
29+
To prevent table rows from splitting across pages, manually recreate the PDF table from scratch by copying the HTML table content to a new PDF table. Use the **Measure** method to check whether the table exceeds the page boundary. If it does, create a new page and continue building the table.
4130

4231
### Steps to Implement
4332

44-
1. **Set up the HTML import settings:** Use the `HtmlFormatProvider` and implement the `LoadImageFromUri` event for resolving images in the HTML content.
33+
1. **Set up the HTML import settings:** Use the [HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%}) and implement the [LoadImageFromUri]({%slug radwordsprocessing-formats-and-conversion-html-settings%}#loadimagefromuri-and-loadstylesheetfromuri-events) event for resolving images in the HTML content.
4534

46-
2. **Load the HTML document:** Import the HTML content into a `RadFlowDocument` object.
35+
2. **Load the HTML document:** Import the HTML content into a [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}) object.
4736

4837
3. **Extract rows from the HTML table:** Enumerate the rows from the HTML table.
4938

50-
4. **Create a new PDF table:** For each page, create a new table and add rows while ensuring they fit within the page boundaries.
39+
4. **Create and format a new PDF table:** For each page, create a new table and add rows while ensuring they fit within the page boundaries, while also setting the desired formatting.
5140

52-
5. **Check row measurements:** After adding each row, use the `Measure` method to verify whether the table exceeds the page height. If it does, move the remaining rows to a new page.
41+
5. **Check row measurements:** After adding each row, use the **Measure** method to verify whether the table exceeds the page height. If it does, move the remaining rows to a new page.
5342

54-
6. **Export the PDF:** Use the `PdfFormatProvider` to save the final PDF document.
43+
6. **Export the PDF:** Use the [PdfFormatProvider]({%slug radpdfprocessing-formats-and-conversion-pdf-pdfformatprovider%}) to save the final PDF document.
44+
45+
![HTML to PDF Table Without Split Rows](images/html-pdf-table-no-split-rows.png)
5546

5647
### Code Example
5748

@@ -61,7 +52,6 @@ const string OutputPdfPath = "..\\..\\..\\output.pdf";
6152

6253
static void Main(string[] args)
6354
{
64-
// Load HTML document
6555
HtmlFormatProvider htmlFormatProvider = new HtmlFormatProvider();
6656
HtmlImportSettings importSettings = new HtmlImportSettings();
6757

@@ -83,6 +73,7 @@ static void Main(string[] args)
8373
var mainPdfDocument = new RadFixedDocument();
8474
int currentRowIndex = 0;
8575

76+
// Process rows across multiple pages
8677
while (currentRowIndex < htmlRows.Count)
8778
{
8879
var pdfPage = mainPdfDocument.Pages.AddPage();
@@ -93,12 +84,14 @@ static void Main(string[] args)
9384
{
9485
var testTable = CreateNewTable();
9586

87+
// Copy existing rows to test table
9688
for (int i = currentRowIndex - rowsAdded; i < currentRowIndex; i++)
9789
if (i >= 0) AddRowToTable(testTable, htmlRows[i], pdfPage);
9890

9991
AddRowToTable(testTable, htmlRows[currentRowIndex], pdfPage);
10092

101-
if (testTable.Measure().Height > pdfPage.Size.Height - 40 && rowsAdded > 0)
93+
// Check if exceeds page height
94+
if (testTable.Measure().Height > pdfPage.Size.Height && rowsAdded > 0)
10295
break;
10396

10497
AddRowToTable(pdfTable, htmlRows[currentRowIndex], pdfPage);
@@ -118,26 +111,30 @@ static void Main(string[] args)
118111

119112
private static Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table CreateNewTable()
120113
{
121-
var border = new Telerik.Windows.Documents.Fixed.Model.Editing.Border(2, new RgbColor(0, 0, 0));
114+
var border = new Border(1, new RgbColor(0, 0, 0));
122115
return new Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table
123116
{
124117
DefaultCellProperties = { Borders = new Telerik.Windows.Documents.Fixed.Model.Editing.Tables.TableCellBorders(border, border, border, border) },
125-
Margin = new System.Windows.Thickness(10)
118+
Margin = new Thickness(10)
126119
};
127120
}
128121

129-
private static void AddRowToTable(Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table pdfTable, Telerik.Windows.Documents.Flow.Model.TableRow htmlRow, RadFixedPage pdfPage)
122+
private static void AddRowToTable(Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table pdfTable, TableRow htmlRow, RadFixedPage pdfPage)
130123
{
131124
var pdfRow = pdfTable.Rows.AddTableRow();
132125

133126
for (int i = 0; i < htmlRow.Cells.Count; i++)
134127
{
135128
var pdfCell = pdfRow.Cells.AddTableCell();
136-
pdfCell.PreferredWidth = pdfPage.Size.Width * (i == 0 ? 0.10 : 0.80);
129+
pdfCell.PreferredWidth = pdfPage.Size.Width * (i == 0 ? 0.05 : 0.25);
130+
pdfCell.Padding = new Thickness(5);
131+
pdfCell.Background = (pdfTable.Rows.Count % 2 == 0 ? new RgbColor(249, 249, 249) : new RgbColor(255, 255, 255));
137132

138-
foreach (var htmlBlock in htmlRow.Cells[i].Blocks.OfType<Telerik.Windows.Documents.Flow.Model.Paragraph>())
133+
foreach (var htmlBlock in htmlRow.Cells[i].Blocks.OfType<Paragraph>())
139134
{
140135
var pdfBlock = pdfCell.Blocks.AddBlock();
136+
pdfBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Left;
137+
pdfBlock.VerticalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.VerticalAlignment.Center;
141138

142139
foreach (var htmlInline in htmlBlock.Inlines)
143140
{
@@ -146,8 +143,8 @@ private static void AddRowToTable(Telerik.Windows.Documents.Fixed.Model.Editing.
146143
using (var stream = new MemoryStream(htmlImageInline.Image.ImageSource.Data))
147144
pdfBlock.InsertImage(new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(stream));
148145
}
149-
else if (htmlInline is Telerik.Windows.Documents.Flow.Model.Run run)
150-
{
146+
else if (htmlInline is Run run)
147+
{
151148
pdfBlock.InsertText(run.Text);
152149
}
153150
else if (htmlInline is Break)
@@ -161,7 +158,7 @@ private static void AddRowToTable(Telerik.Windows.Documents.Fixed.Model.Editing.
161158
```
162159

163160
## See Also
164-
- [HtmlFormatProvider Overview](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/html/html-overview)
165-
- [RadFixedDocument API Reference](https://docs.telerik.com/devtools/document-processing/api/telerik.windows.documents.fixed.model.radfixeddocument)
166-
- [PdfFormatProvider Overview](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/formats-and-conversion/pdf/pdf-overview)
161+
* [Table]({%slug radpdfprocessing-editing-table-overview%})
162+
* [TableRow]({%slug radpdfprocessing-editing-table-tablerow%})
163+
* [TableCell]({%slug radpdfprocessing-editing-table-tablecell%})
167164
---

0 commit comments

Comments
 (0)