How can I Create a downloadable pdf file which includes below points.

1 Answer 112 Views
PdfProcessing WordsProcessing
Rohit
Top achievements
Rank 1
Rohit asked on 03 Mar 2023, 11:37 AM | edited on 03 Mar 2023, 11:48 AM
It should have the following features using latest version of the documentation:
1)The ability to set margins should be available.
2)The ability to export PDF files should be available.
3)I'd like to incorporate html content in various blocks, as well as one logo and signature.

1 Answer, 1 is accepted

Sort by
1
Accepted
Yoan
Telerik team
answered on 07 Mar 2023, 05:04 PM

Hello Rohit,

The mentioned requirements can indeed be achieved and implemented through the WordsProcessing and PdfProceesing libraries. I will get straight to answering your questions while providing some resources that can clarify things for you.

   1) Customizing the margins of a document can be achieved by iterating through the document sections, each of which exposes a PageMargins property, and setting that property by your preference. You can find more details in the Modifying a Section article or you can take a look at the Change Flow Document Properties when converting to PDF example.

   2) RadWordsProcessing provides a PdfFormatProvider class that allows the export of a RadFlowDocument to PDF. You can check its Supported Features and also read more about its implementation and limitations in the Export section.

   3) Importing and exporting to/from HTML format to RadFlowDocument while preserving as much as possible of the document structure and formatting can be achieved by utilizing the HtmlFormatProvider. In the documentation, you can also find examples of the supported HTML elements and Features of the provider. 

       Images (such as logos) in WordsProcessing can be inserted into a RadFlowDocument as an ImageInline or a FloatingImage element. More about them, their formatting, use, and implementation can be found in the Modifying an ImageInline and Modifying a FloatingImage sections as well as in the Floating Images Blog.

       When working with a PDF document, signing and validating signatures in PdfProcessing is done through a Digital Signature. A signature confirms that the document's content originated from the signer and has not been modified in any way. More about its implementation and use can be found in the Signing a Document and Validating a Signature sections as well as the Signing a document with a digital signature example.

    Hope this information proves helpful and if you happen to have any further questions, feel free to ask.

    Regards, Yoan 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/.

    Rohit
    Top achievements
    Rank 1
    commented on 08 Mar 2023, 10:03 AM

    I appreciate the response.
    However I do have a 1-2 page document. I need to import the html for each page to just a certain area of the body section. Every page should have a logo image in the header. Each page should have another html string in the footer.


     public void GenerateDocument()
            {
               
                string htmlBody = "<html><body><h1>This is Body</h1></body></html>";
                string htmlFooter = "<html><body><h1>This is footer.</h1></body></html>";
    
                RadFlowDocument document = new();
    
                var htmlDocument = new HtmlFormatProvider();
                document = htmlDocument.Import(htmlBody);
    
                Section section = document.Sections.AddSection();
                var defaultHeader = document.Sections.First().Headers.Add();
                var header = defaultHeader.Blocks.AddParagraph();
    
                ImageInline imageInline = new(document);
    
                byte[] data = System.IO.File.ReadAllBytes("Random.jpg");
                imageInline.Image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(data, "jpg");
    
                //Setting img sizes
                imageInline.Image.Size = new Telerik.Documents.Primitives.Size(200, 100);
    
                header.Inlines.Add(imageInline);
    
                MemoryStream stream = new();
                var flowProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
                flowProvider.Export(document, stream);
    
            }
    The end result will be imported html only, but I also need my logo to appear in the header section.
    Yoan
    Telerik team
    commented on 10 Mar 2023, 02:59 PM

    Hello Rohit,

    Setting HTML content as a part of a RadFlowDocument can be achieved through the HtmlFormatProvider and the DocumentElementImporter. The provider creates a document instance from an HTML input and then the importer makes it possible so that elements from that document can be imported to another RadFlowDocument for example as a header/footer like in this case. 

    I am sending you a few links to some articles that I believe might prove useful to you:

    I have integrated the provided code snippet into a project on my end and allowed myself to modify it a little bit so that it implements the above-mentioned functionality and also fits the requirements of the desired behavior. The approach creates a new document from the 'bodyHtml' string, iterates all its sections, and sets a header and a footer for each one by utilizing the DocumentElementImporter where necessary.

    I am attaching that sample project for you to take a look at. Feel free to examine and modify it as you like and if you have any questions or feedback, feel free to share them with us.

    Regards,

    Yoan

    Tags
    PdfProcessing WordsProcessing
    Asked by
    Rohit
    Top achievements
    Rank 1
    Answers by
    Yoan
    Telerik team
    Share this question
    or