|
| 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 | + |
| 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 | + |
| 136 | + |
| 137 | +## See Also |
| 138 | +- [Generate Table with Images using RadPdfProcessing]({%slug generate-table-with-images-pdf-processing%}) |
0 commit comments