Skip to content

Commit f3a8acd

Browse files
committed
polish content and add images
1 parent 77056bd commit f3a8acd

File tree

5 files changed

+113
-47
lines changed

5 files changed

+113
-47
lines changed
19.4 KB
Loading
19.9 KB
Loading

knowledge-base/insert-html-content-into-pdf-tablecell-radpdfprocessing.md

Lines changed: 111 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,135 @@ description: Learn how to insert HTML content into a TableCell in a PDF document
44
type: how-to
55
page_title: How to Insert HTML Content into PDF TableCell Using RadPdfProcessing
66
slug: insert-html-content-into-pdf-tablecell-radpdfprocessing
7-
tags: radpdfprocessing, documentprocessing, pdf, html, tablecell, insert, radwordsprocessing
7+
tags: pdfprocessing, document, processing, pdf, html, table, cell, insert, wordsprocessing
88
res_type: kb
99
ticketid: 1671595
1010
---
1111

12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 2024.3.806 .NET Standard| RadWordsProcessing-RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
1218
## Description
13-
When generating PDF documents, a common requirement is to insert HTML content into specific sections of the document, such as a `TableCell`. This article demonstrates how to achieve this using the RadPdfProcessing and RadWordsProcessing libraries. This knowledge base article also answers the following questions:
14-
- How can I display HTML content in a PDF document?
15-
- What is the approach to convert HTML to PDF content for insertion into a PDF table cell?
16-
- How to use RadWordsProcessing to import HTML content and RadPdfProcessing to insert it into a PDF document?
17-
18-
## Environment
19-
20-
<table>
21-
<tbody>
22-
<tr>
23-
<td>Product</td>
24-
<td>
25-
RadPdfProcessing for Document Processing, <br/>
26-
RadWordsProcessing for Document Processing
27-
</td>
28-
</tr>
29-
</tbody>
30-
</table>
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)
3136

3237
## Solution
33-
To insert HTML content into a `TableCell` in a PDF document, you can use the RadWordsProcessing library to import the HTML content and then either convert it to a PDF document or export it as images to insert into the PDF. Below are the steps and code snippet for achieving this.
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 exported 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.
3445

35-
1. **Import HTML Content**: Use RadWordsProcessing's `HtmlFormatProvider` to import the HTML content into a `RadFlowDocument`.
36-
2. **Export HTML to PDF or Images**: Use RadWordsProcessing's `PdfFormatProvider` to convert the `RadFlowDocument` into PDF format or export it as images.
37-
3. **Insert PDF or Images into TableCell**: Use RadPdfProcessing to create or edit a PDF document and insert the converted PDF content or images into the desired `TableCell`.
46+
1. **Insert the exported Images into the PDF TableCell**: Use RadPdfProcessing's [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%}) to create or edit a PDF document and insert the converted PDF images into the desired [TableCell]({%slug radpdfprocessing-editing-tablecell%}).
3847

3948
### Inserting HTML Content as PDF
4049

4150
```csharp
42-
// Import HTML as RadFlowDocument
43-
HtmlFormatProvider htmlFormatProvider = new HtmlFormatProvider();
44-
RadFlowDocument htmlDocument = htmlFormatProvider.Import(htmlContent, TimeSpan.FromSeconds(10));
51+
Telerik.Documents.ImageUtils.ImagePropertiesResolver defaultImagePropertiesResolver = new Telerik.Documents.ImageUtils.ImagePropertiesResolver();
52+
Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver = defaultImagePropertiesResolver;
4553

46-
// Export HTML to PDF
47-
Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider flowPdfFormatProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
48-
byte[] htmlToPdfByteArray = flowPdfFormatProvider.Export(htmlDocument, TimeSpan.FromSeconds(10));
54+
RadFixedDocument mainPdfDocument = new RadFixedDocument();
55+
RadFixedPage page = mainPdfDocument.Pages.AddPage();
4956

50-
// Import htmlToPdfByteArray as RadFixedDocument
51-
Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider fixedPdfFormatProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
52-
RadFixedDocument htmlPdfDocument = fixedPdfFormatProvider.Import(htmlToPdfByteArray, TimeSpan.FromSeconds(10));
57+
FixedContentEditor editor = new FixedContentEditor(page);
58+
Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider fixedPdfFormatProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
5359

54-
// Insert the HTML PDF content into the table cell in the main PDF document
55-
// Refer to the provided code snippet in the support engineer response for detailed implementation
56-
```
60+
//Create PDF table
61+
Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table table = new Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table();
5762

58-
### Inserting HTML Content as Images
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);
5967

60-
```csharp
61-
// Export the HTML content as images
62-
Telerik.Documents.Fixed.FormatProviders.Image.Skia.SkiaImageFormatProvider imageProvider = new Telerik.Documents.Fixed.FormatProviders.Image.Skia.SkiaImageFormatProvider();
63-
byte[] resultImage = imageProvider.Export(htmlDocument.Pages.First(), TimeSpan.FromSeconds(10));
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));
6486

65-
// Insert the image into the table cell in the main PDF document
66-
// Refer to the provided code snippet in the support engineer response for detailed implementation
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);
67133
```
68134

135+
![HTML Inside the PDF Table](images/html-inside-pdf-table.png)
136+
69137
## See Also
70-
- [RadPdfProcessing Overview](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/overview)
71-
- [RadWordsProcessing Overview](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/overview)
72-
- [Importing HTML Content with RadWordsProcessing](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/html/htmlformatprovider)
73-
- [Converting Documents to Images with RadPdfProcessing](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/formats-and-conversion/convert-to-image/using-image-format-provider)
74-
- [Generate Table with Images using RadPdfProcessing](https://docs.telerik.com/devtools/document-processing/knowledge-base/generate-table-with-images-pdf-processing)
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)