Skip to content

Commit 212a79a

Browse files
Merge pull request #486 from telerik/new-kb-insert-html-content-into-pdf-tablecell-radpdfprocessing-9668ce5795b1453fa3a813c4b9604d6f
Added new kb article insert-html-content-into-pdf-tablecell-radpdfprocessing
2 parents 3ac05e9 + 05b9447 commit 212a79a

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed
19.4 KB
Loading
19.9 KB
Loading
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
title: Inserting HTML Content into PDF TableCell with RadPdfProcessing
3+
description: Learn how to insert HTML content into a TableCell in a PDF document using RadPdfProcessing and RadWordsProcessing libraries.
4+
type: how-to
5+
page_title: How to Insert HTML Content into PDF TableCell Using RadPdfProcessing
6+
slug: insert-html-content-into-pdf-tablecell-radpdfprocessing
7+
tags: pdfprocessing, document, processing, pdf, html, table, cell, insert, wordsprocessing
8+
res_type: kb
9+
ticketid: 1671595
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 2024.4.1106.NET Standard| RadWordsProcessing-RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
When generating PDF documents, a common requirement is to insert HTML content into specific sections of the document, such as a [TableCell]({%slug radpdfprocessing-editing-tablecell%}). This article demonstrates how to achieve this using the smooth integration between [RadPdfProcessing]({%slug radpdfprocessing-overview%}) and [RadWordsProcessing]({%slug radwordsprocessing-overview%}) libraries.
20+
21+
>caption Sample HTML content to Insert
22+
23+
```HTML
24+
<!DOCTYPE html>
25+
<html>
26+
<body>
27+
<p>I am normal</p>
28+
<p style="color:red;">I am red</p>
29+
<p style="color:blue;">I am blue</p>
30+
<p style="font-size:50px;">I am big</p>
31+
</body>
32+
</html>
33+
```
34+
35+
![HTML Displayed in the Browser](images/sample-html-content.png)
36+
37+
## Solution
38+
To insert HTML content into a `TableCell` in a PDF document, you can obtain the HTML content as an image and insert the image inside the PDF table cell. Below are the steps and complete code snippet for achieving this:
39+
40+
1. **Import HTML Content**: Use the RadWordsProcessing's [HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%}) to import the HTML content into a [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}).
41+
42+
1. **Export the HTML Content to PDF Format**: Use the RadWordsProcessing's [PdfFormatProvider]({%slug radwordsprocessing-formats-and-conversion-pdf-pdfformatprovider%}) to convert the `RadFlowDocument` into PDF format.
43+
44+
1. **Convert the exported PDF content to an Image**: Use the RadPdfProcessing's [PdfFormatProvider]({%slug radpdfprocessing-formats-and-conversion-pdf-pdfformatprovider%}) to import the PDF-converted HTML content to [RadFixedDocument]({%slug radpdfprocessing-model-radfixeddocument%}) and the [SkiaImageFormatProvider]({%slug radpdfprocessing-formats-and-conversion-image-using-skiaimageformatprovider%}) to export the PDF pages to images.
45+
46+
1. **Insert the exported Images into the PDF TableCell**: Use RadPdfProcessing's [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%}) to create the main PDF document with a table and insert the converted PDF images into the desired [TableCell]({%slug radpdfprocessing-editing-tablecell%}).
47+
48+
### Inserting HTML Content as PDF
49+
50+
```csharp
51+
Telerik.Documents.ImageUtils.ImagePropertiesResolver defaultImagePropertiesResolver = new Telerik.Documents.ImageUtils.ImagePropertiesResolver();
52+
Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver = defaultImagePropertiesResolver;
53+
54+
RadFixedDocument mainPdfDocument = new RadFixedDocument();
55+
RadFixedPage page = mainPdfDocument.Pages.AddPage();
56+
57+
FixedContentEditor editor = new FixedContentEditor(page);
58+
Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider fixedPdfFormatProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
59+
60+
//Create PDF table
61+
Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table table = new Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table();
62+
63+
//Set borders
64+
Telerik.Windows.Documents.Fixed.Model.Editing.Border border = new Telerik.Windows.Documents.Fixed.Model.Editing.Border();
65+
table.Borders = new Telerik.Windows.Documents.Fixed.Model.Editing.TableBorders(border);
66+
table.DefaultCellProperties.Borders = new Telerik.Windows.Documents.Fixed.Model.Editing.Tables.TableCellBorders(border, border, border, border);
67+
68+
//Set table properties
69+
table.Margin = new Thickness(20);
70+
table.DefaultCellProperties.Padding = new Thickness(5);
71+
table.BorderSpacing = 2;
72+
73+
//Create row
74+
Telerik.Windows.Documents.Fixed.Model.Editing.Tables.TableRow row1 = table.Rows.AddTableRow();
75+
Telerik.Windows.Documents.Fixed.Model.Editing.Tables.TableCell imgCell = row1.Cells.AddTableCell();
76+
imgCell.PreferredWidth = 200;
77+
string htmlContent = File.ReadAllText("cellContent.html");
78+
79+
//Import HTML as RadFlowDocument
80+
HtmlFormatProvider htmlFormatProvider = new HtmlFormatProvider();
81+
RadFlowDocument htmlDocument = htmlFormatProvider.Import(htmlContent, TimeSpan.FromSeconds(10));
82+
83+
//Export HTML to PDF
84+
Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider flowPdfFormatProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
85+
byte[] htmlToPdfByteArray = flowPdfFormatProvider.Export(htmlDocument, TimeSpan.FromSeconds(10));
86+
87+
//Import htmlToPdfByteArray as RadFixedDocument
88+
RadFixedDocument htmlPdfDocument = fixedPdfFormatProvider.Import(htmlToPdfByteArray, TimeSpan.FromSeconds(10));
89+
90+
RgbColor bordersColor = new RgbColor(255, 0, 0);
91+
Telerik.Windows.Documents.Fixed.Model.Editing.Border cellBorder = new Telerik.Windows.Documents.Fixed.Model.Editing.Border(2, Telerik.Windows.Documents.Fixed.Model.Editing.BorderStyle.Single, bordersColor);
92+
Telerik.Windows.Documents.Fixed.Model.Editing.Tables.TableCellBorders tableCellsBorder = new Telerik.Windows.Documents.Fixed.Model.Editing.Tables.TableCellBorders(border, border, border, border, null, null);
93+
94+
//Export the PDF pages as images
95+
Telerik.Documents.Fixed.FormatProviders.Image.Skia.SkiaImageFormatProvider imageProvider = new Telerik.Documents.Fixed.FormatProviders.Image.Skia.SkiaImageFormatProvider();
96+
string imagesFolderPath = @"..\..\..\Images\";
97+
int count = 1;
98+
foreach (RadFixedPage p in htmlPdfDocument.Pages)
99+
{
100+
byte[] resultImage = imageProvider.Export(p, TimeSpan.FromSeconds(10));
101+
File.WriteAllBytes(imagesFolderPath + count++ + ".png", resultImage);
102+
}
103+
104+
Block imageBlock;
105+
string[] pdfFilePaths = Directory.GetFiles(imagesFolderPath);
106+
foreach (string imageFilePath in pdfFilePaths)
107+
{
108+
109+
imageBlock = imgCell.Blocks.AddBlock();
110+
imageBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;
111+
imageBlock.InsertImage(new FileStream(imageFilePath, FileMode.Open),new Size(300,400));
112+
imgCell = row1.Cells.AddTableCell();
113+
imgCell.Borders = tableCellsBorder;
114+
}
115+
116+
//Draw the generated table
117+
editor.DrawTable(table);
118+
119+
string outputFile = "output.pdf";
120+
File.Delete(outputFile);
121+
//Export main PDF with changes
122+
using (Stream output = File.OpenWrite(outputFile))
123+
{
124+
fixedPdfFormatProvider.Export(mainPdfDocument, output, TimeSpan.FromSeconds(10));
125+
}
126+
//Open main PDF
127+
var psi = new ProcessStartInfo()
128+
{
129+
FileName = outputFile,
130+
UseShellExecute = true
131+
};
132+
Process.Start(psi);
133+
```
134+
135+
![HTML Inside the PDF Table](images/html-inside-pdf-table.png)
136+
137+
## See Also
138+
- [Generate Table with Images using RadPdfProcessing]({%slug generate-table-with-images-pdf-processing%})

libraries/radpdfprocessing/editing/fixedcontenteditor.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,4 @@ __FixedContentEditor__ has some properties and methods that affect how it will b
337337
* [Adding Images with a Shadow in PDF Documents]({%slug add-shadow-image-radpdfprocessing%})
338338
* [Splitting a Large Image Across Multiple PDF Pages]({%slug split-export-large-image-multiple-pdf-pages-radpdfprocessing%})
339339
* [Resizing Large Images to Fit in the PDF Page]({%slug resize-images-radpdfprocessing%})
340+
* [ Inserting HTML Content into PDF TableCell with RadPdfProcessing]({%slug insert-html-content-into-pdf-tablecell-radpdfprocessing%})

libraries/radpdfprocessing/editing/tablecell.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,4 @@ The result from __Example 3__ is illustrated on __Figure 1__.
108108
* [Creating Custom Layout Tables with RadPdfProcessing]({%slug customize-table-layout-radpdfprocessing%})
109109
* [Implementing Column Span in RadPdfProcessing Tables]({%slug table-column-span-radpdfprocessing%})
110110
* [Creating a PDF Table with Form Fields Inside the Cells]({%slug insert-form-xobject-elements-pdf-table-cell%})
111+
* [ Inserting HTML Content into PDF TableCell with RadPdfProcessing]({%slug insert-html-content-into-pdf-tablecell-radpdfprocessing%})

0 commit comments

Comments
 (0)