I have a form in a blazor web app that I need to export the content from to a pdf.
The form fields are rich textboxes that can have varying degrees of content, from single lines to who knows how many.
If I export this first to html, and then feed that html into your pdfprocesser, will it be able to automatically create as many pages as it needs or do I need to do calculations to see what will fit on a page and I create the new pages myself?
5 Answers, 1 is accepted
Hello Rick,
You do not need to manually calculate the content size. First, you need to create a RadFlowDocument by importing the Html, if you have more than one document, merge them, and finally export the result document to pdf. More information about this is available in the following articles:
I hope this helps. Should you have any other questions do not hesitate to ask.
Regards,
Dimitar
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Is there anything I can do to make table elements not split between pages?
If it seems the content will be too large to fit in the page, it adds it to the next page instead?
Hello Rick,
Yes, this is the default behavior. What you can do is measure the table before adding it, then change the page size and draw the table using the FixedContentEditor. Here is an example of this:
RadFixedDocument document = new RadFixedDocument();
Table table = new Table();
Border border = new Border(1, new RgbColor(255,0,0));
table.Borders = new TableBorders(border);
table.DefaultCellProperties.Borders = new TableCellBorders(border, border, border, border);
for (int i = 0; i < 100; i++)
{
TableRow row = table.Rows.AddTableRow();
row.Cells.AddTableCell().Blocks.AddBlock().InsertText("cell 1");
row.Cells.AddTableCell().Blocks.AddBlock().InsertText("cell 2");
row.Cells.AddTableCell().Blocks.AddBlock().InsertText("cell 3");
}
RadFixedPage page = new RadFixedPage();
var desiredSize = table.Measure(new System.Windows.Size(page.Size.Width, double.PositiveInfinity));
if (desiredSize.Height > page.Size.Height)
{
page.Size = new System.Windows.Size(page.Size.Width, desiredSize.Height + 100);
}
FixedContentEditor editor = new FixedContentEditor(page);
editor.Position.Translate(50, 50);
editor.DrawTable(table);
document.Pages.Add(page);
PdfFormatProvider provider = new PdfFormatProvider();
using (Stream output = File.OpenWrite(@"..\..\Hello.pdf"))
{
provider.Export(document, output);
}
Let me know if I can assist you further.
Regards,
Dimitar
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Hi Dimitar,
I am using the html import, is this still valid for that?
Hello Rick,
No, the mistake is mine. Unfortunately, the WordsProcessing library does have a method that can measure the table. The only solution I can suggest is to manually increase the size of the pages in the RadFlowDocuemnt. Here is an example of this:
var provider = new HtmlFormatProvider();
var document = provider.Import(File.ReadAllText(@"..\..\Doc1.html"));
foreach (var item in document.Sections)
{
item.PageSize = new System.Windows.Size(1000, 3000);
}
var pdfFormatProvider = new PdfFormatProvider();
File.WriteAllBytes(@"..\..\result.pdf",pdfFormatProvider.Export(document));
Another approach is to use your original idea and iterate all elements in the RadFlowDocument and manually create the pdf elements. Here is an example that shown how you can convert the tables:
var provider = new HtmlFormatProvider();
var document = provider.Import(File.ReadAllText(@"..\..\Doc1.html"));
RadFixedDocument fixedDocument = new RadFixedDocument();
var page = fixedDocument.Pages.AddPage();
var tables = document.EnumerateChildrenOfType<Telerik.Windows.Documents.Flow.Model.Table>();
var table = new Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table();
Border border = new Border(1, new RgbColor(255, 0, 0));
table.Borders = new TableBorders(border);
table.DefaultCellProperties.Borders = new Telerik.Windows.Documents.Fixed.Model.Editing.Tables.TableCellBorders(border, border, border, border);
foreach (var t in tables)
{
foreach (var row in t.Rows)
{
var newRow = table.Rows.AddTableRow();
foreach (var cell in row.Cells)
{
var block = cell.Blocks[0] as Telerik.Windows.Documents.Flow.Model.Paragraph;
string text = (block.Inlines[0] as Telerik.Windows.Documents.Flow.Model.Run).Text;
var newCell = newRow.Cells.AddTableCell();
newCell.Blocks.AddBlock().InsertText(text);
}
}
}
var desiredSize = table.Measure(new System.Windows.Size(page.Size.Width, double.PositiveInfinity));
if (desiredSize.Height > page.Size.Height)
{
page.Size = new System.Windows.Size(page.Size.Width, desiredSize.Height + 100);
}
FixedContentEditor editor = new FixedContentEditor(page);
editor.Position.Translate(50, 50);
editor.DrawTable(table);
var pdfProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
using (Stream output = File.OpenWrite(@"..\..\Hello.pdf"))
{
pdfProvider.Export(fixedDocument, output);
}
Let me know if I can assist you further.
Regards,
Dimitar
Progress Telerik
Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.
Some areas of the pdf file generated is cutting off text in the export.
Is there something I should be doing?
The pasted screen shows the last of the generated text but there is more text after this in the html.
Hello Rick,
This sounds like an issue. The best approach would be to open a new ticket which is a private thread and attach your HTML file there. This way we will be able to properly investigate this case and determine why the ext is cut off.
Thank you in advance for your patience and cooperation.