dynamic form field sizes

6 posts, 0 answers
  1. Rick
    Rick avatar
    57 posts
    Member since:
    Sep 2017

    Posted 12 Feb Link to this post

    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?

  2. Dimitar
    Admin
    Dimitar avatar
    2983 posts

    Posted 15 Feb Link to this post

    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/.

  3. Rick
    Rick avatar
    57 posts
    Member since:
    Sep 2017

    Posted 01 Apr in reply to Dimitar Link to this post

    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?

  4. Dimitar
    Admin
    Dimitar avatar
    2983 posts

    Posted 02 Apr Link to this post

    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/.

  5. Rick
    Rick avatar
    57 posts
    Member since:
    Sep 2017

    Posted 02 Apr in reply to Dimitar Link to this post

    Hi Dimitar,

    I am using the html import, is this still valid for that?

  6. Dimitar
    Admin
    Dimitar avatar
    2983 posts

    Posted 05 Apr Link to this post

    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.

Back to Top