Fill a Word Document from ASP.Net and export to it to PDF

8 posts, 0 answers
  1. Amit
    Amit avatar
    12 posts
    Member since:
    Jul 2012

    Posted 01 May Link to this post

    Hi,

    My apologies in advance if this cannot be done using Telerik Document Processing and I'm wasting everyone's time. I'll try and explain my situation the best I can.

    We have large number of word documents related to a process which need to be converted to online forms. That's the easy part. The business wants the reporting of these forms exactly in the same format as in the original word documents. So we are hoping to use the word documents as templates (not necessarily .DOTX). 

    Since the format of word documents often change The idea is to

    • Have one base document of each format kept on the server.
    • This base doc will have place-holders for the data to be filled in by asp.net (C#) for e.g. Employee Name: <<NAME>>.
    • When a report is to be generated, the relevant data is pulled from the SQL database and filled in the placeholders of the document. So in the above example <<NAME>> will be replaced by real name of the employee from database.
    • There will be some Base64 Images which will come from database and to be placed in the document e.g. signatures, profile pictures.
    • Once the document is completely filled, it is converted to PDF and downloaded to user's browser.
    • Whenever the format of a word document changes, it is replaced on the server and the asp.net fills this new format.  

    Since we are in design phase of this I'm open to adapt to any suggestions. Any pointers/demo/advice on approach is welcome.

     

    Thanks in Advance.

  2. Tanya
    Admin
    Tanya avatar
    560 posts

    Posted 04 May Link to this post

    Hi Amit,

    You can achieve the desired scenario using the WordsProcessing library. The Mail Merge feature will help you implement the placeholders in the template DOCX document. You can generate the template using the API of WordsProcessing and then export it via DocxFormatProvider. When you have the data from the customer, you will need to perform the mail merge, then export the document to PDF with the PdfFormatProvider class.

    There are several from our online examples that will help you in getting a better understanding of the features and in implementing the scenario. You will find them at http://demos.telerik.com/aspnet-ajax/wordsprocessing/.

    Hope this is helpful.

    Regards,
    Tanya
    Telerik by Progress

  3. Amit
    Amit avatar
    12 posts
    Member since:
    Jul 2012

    Posted 04 May in reply to Tanya Link to this post

    Hi Tanya,

    Thank you for your reply. I was suspecting that Mail Merge should be the solution for this.

    I actually have been previously to the WordsProcessing Mail Merge example here that you've mentioned here, however it is not working. When you click the Mail Merge button, I get page not found error. So got confused. Might be something for your guys to fix.

    I will get the developers have a look at WordsProcessing and Mail Merge. Will get back to you in case they get stuck somewhere.

    Thanks again for the pointers.

     

  4. Tanya
    Admin
    Tanya avatar
    560 posts

    Posted 05 May Link to this post

    Hi Amit,

    Thank you for bringing the issue with the examples to our attention. We managed to fix it and now you should be able to check the demos.

    Regards,
    Tanya
    Telerik by Progress

  5. Amit
    Amit avatar
    12 posts
    Member since:
    Jul 2012

    Posted 05 May in reply to Tanya Link to this post

    Thanks Tanya,

    Works perfectly now

  6. Amit
    Amit avatar
    12 posts
    Member since:
    Jul 2012

    Posted 17 May Link to this post

    Hi Tanya,

    we have taken your suggestion and moved ahead with Mail Merge for for generating our reports. However have hit a roadblock. We need to insert images in some merge fields (user signatures etc.). The images are stored as Base64 in an SQL database. could you please provide some suggestions or point us to examples which demonstrate how to insert an image from database onto a merge field of a document.

    Thanks in advance

  7. Amit
    Amit avatar
    12 posts
    Member since:
    Jul 2012

    Posted 18 May in reply to Tanya Link to this post

    Hi Tanya / Any other admin,

    Could you please provide some help on the issue that we are facing (add image from DB to Mail Merge) its proving to be a roadblock for us.

     

    Thanks

    Amit

     

     

  8. Tanya
    Admin
    Tanya avatar
    560 posts

    Posted 19 May Link to this post

    Hi Amit,

    You can find the placeholder and replace it with the image using code similar to the following one:
    RadFlowDocument document = new RadFlowDocument();
    RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
      
    editor.InsertText("1");
    editor.InsertText("EXAMPLE_IMAGE");
    editor.InsertText("2");
      
    foreach (var run in editor.Document.EnumerateChildrenOfType<Run>().ToArray())
    {
        if(run.Text == "EXAMPLE_IMAGE")
        {
            Paragraph paragraph = run.Paragraph;
            int indexOfCurrentRun = paragraph.Inlines.IndexOf(run);
            paragraph.Inlines.RemoveAt(indexOfCurrentRun);
      
            ImageInline imageInline = new ImageInline(document);
            imageInline.Image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(imageData, "png");
            paragraph.Inlines.Insert(indexOfCurrentRun, imageInline);
        }
    }

    The ImageSource constructor overloads allow you pass a byte[] or a stream containing the image data, so you will need to convert the Base64 string.

    We have also logged a feature request to implement similar functionality in RadWordsProcessing: INCLUDEPICTURE field. You can vote for its implementation and track its status through the public item in our feedback portal.

    Regards,
    Tanya
    Telerik by Progress

Back to Top