Generate a letter using Mail merge functionality of Essential DocIO

Hi, 

I'm having trouble trying to generate properly a word document (letter), using Mail merge functionality of Essential DocIO.
After being generated, I need to convert it to PDF.

Errors:

- When I open the generated file in WinWord: "unreadable content"
- When generating pdf: "Zip exception.Can't locate end of central directory record. Possible wrong file format or archive is corrupt.'

Code snippet: 

            string[] aDataSet = { "ADVANCE GROUPAMA", "00030007637016103000", "Dr. Dias Neto", "17339",
                "Regina Maria da Silva Deus Conde", "pelo Arquivo de", "a", "portadora", "esta",
                "Bilhete de Identidade", "123456", "London", "04/04/2022", "25", "four",
                "April", "2022", "dois", "dois", "Rua de Nampula 34 3 D", "Portugal",
                "Disease", "Care message that we should have with the patient", "Windsor" };

            string[] aFieldNames = { "Acordo", "Beneficiario", "Medico", "Cedula",
                "NomeUtente", "Origem", "Genero", "Portador", "Este",
                "TipoDocumento", "BI", "Arquivo", "ValidadeBI", "Dia", "ExtensoDia",
                "Mes", "Ano", "Dias", "ExtensoDias", "Morada", "Naturalidade",
                "Doenca", "Cuidados", "LocalEmissao" };


                document.MailMerge.Execute(fieldNames, fieldValues);  
                FileStream fileOutputStreamPath = new FileStream(sOutputWord, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

                document.Save(fileOutputStreamPath, FormatType.Docx);
                document.Close();

sOutputWord = @"C:\NewProjects\Clinicasoft\ClinicaSoft.Web\wwwroot\Medical_Documents\\Certificates\Certificate_a_1.docx";
sOutputPDF  = @"C:\NewProjects\Clinicasoft\ClinicaSoft.Web\wwwroot\Medical_Documents\\Certificates\Certificate_a_1.pdf";

                GeneratePDF_FromDocx(sOutputWord, sOutputPDF);  
               .....

   private bool GeneratePDF_FromDocx(string docWord, string docWordPDF)
        {
            try
            {
                FileStream fileStreamPath = new FileStream(docWord, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                // **** Error in next line ****
                WordDocument wordDocument = new WordDocument(fileStreamPath, FormatType.Docx);
                // Sets page size as A5 for each sections.
                foreach (IWSection iWSection in wordDocument.Sections)
                    iWSection.PageSetup.PageSize = Syncfusion.DocIO.DLS.PageSize.A5;

                DocIORenderer render = new DocIORenderer();
                //Sets Chart rendering Options.
                render.Settings.ChartRenderingOptions.ImageFormat = ExportImageFormat.Jpeg;
                //Converts Word document into PDF document
                PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
                //Releases all resources used by the Word document and DocIO Renderer objects
                render.Dispose();
                wordDocument.Dispose();
                //Saves the PDF file
                MemoryStream outputStream = new MemoryStream();
                pdfDocument.Save(outputStream);
                //Closes the instance of PDF document object
                pdfDocument.Close();

                //Process.Start(docWordPDF);
                pdfDocument.Close(true);
                wordDocument.Close();

                if (wordDocument != null)
                {
                    wordDocument = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();

                return true;
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
            }
        }



The text of the document, after translation would look something like this:

"Doctor Name, physician, with Professional Certificate no. 17339, 
attests for his Professional Honor that Patient Name, 
holder of Identity Card No. 123456, passed by the London Archives, 
valid until 04/04/2022, is unable to attend the service from the 4th (four) of April 2022, 
and for a probable period of two (two) days, due to Disease disease, 
which requires urgent, indispensable and permanent Care message 
that we should have with the patient."

Word document (dotx) attached.

I would appreciate your help, I have many similar documents to generate.

Regards, 
Fausto


Attachment: AtestadoFamilia_99587640.rar

15 Replies 1 reply marked as answer

FA Fausto January 24, 2021 08:22 PM UTC

Where it says "document.MailMerge.Execute(fieldNames, fieldValues); ", should be "document.MailMerge.Execute(aFieldNames, aDataSet );

Just as a note: instead of using a template document (dotx), I also experimented with a normal word document (docx) and the result was the same. No luck...




MJ Mohanaselvam Jothi Syncfusion Team January 25, 2021 11:37 AM UTC

Hi Fausto,

Thank you for contacting Syncfusion support.

Regarding When I open the generated file in WinWord: "unreadable content":
We have found that the FileStream used for Word document saving is not disposed properly. So, it rises problem when opening improper Word file in Microsoft Word application. Please refer the below highlighted code snippet to resolve this problem at your end:

 
  document.MailMerge.Execute(aFieldNames, aDataSet); 
FileStream fileOutputStreamPath = new FileStream(sOutputWord, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); 
document.Save(fileOutputStreamPath, FormatType.Docx); 
fileOutputStreamPath.Flush(); 
fileOutputStreamPath.Dispose(); 
document.Close(); 

Regarding When generating pdf: "Zip exception.Can't locate end of central directory record. Possible wrong file format or archive is corrupt.':
In the given code snippet, the Word document not saved properly. So, it doesn’t able to open in DocIO which throws above exception while trying to open that improper file. You can use the above solution to resolve this issue at your end and save the intermediate Word document (after mail merge) properly.

Please let us know if you have any other questions.

Regards,
Mohanaselvam J
 


Marked as answer

FA Fausto January 25, 2021 12:43 PM UTC

Hello again,

The first situation was resolved. Thanks.

Now, in relation to the second (pdf generation), the error message that appears is:

"System.ApplicationException: 'Method 'CreateFont' in type 'Syncfusion.DocIORenderer.Drawing.RenderHelper' from assembly 'Syncfusion.DocIORenderer.Portable, Version=18.3200.0.53, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89' does not have an implementation.'"

Regards, 
Fausto.


FA Fausto January 25, 2021 01:36 PM UTC

As I suspected, changing the font to the source document (Times New Roman), solved the situation, although I would like to keep that font.

Another question:

Inscriptions appears at the top of the documents: "Created with a trial version of Syncfusion Essential PDF". 
Is DocIO no longer free for those with a license?

Regards, 
Fausto.


HC Hemalatha Chiranjeevulu Syncfusion Team January 26, 2021 04:09 PM UTC

Hi Fausto,

Thank you for your update.

Currently, we are checking on your query and will update you with more details on 27th January 2021.

Please let us know if you have any other questions.

Regards,
Hemalatha C 



HC Hemalatha Chiranjeevulu Syncfusion Team January 28, 2021 01:10 AM UTC

Hi Fausto,

Thank you for your patience.

Regarding
System.ApplicationException: 'Method 'CreateFont' in type 'Syncfusion.DocIORenderer.Drawing.RenderHelper' from assembly 'Syncfusion.DocIORenderer.Portable, Version=18.3200.0.53, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89' does not have an implementation:
We have tried to reproduce the reported exception issue using the given details, but it works properly at our end. For your reference, we have attached the sample application which we tried to reproduce the reported problem and generated output PDF document at our end. Please find the sample and output document from the below link:

Sample: https://www.syncfusion.com/downloads/support/forum/161833/ze/Sample-1529607140

Output PDF document: https://www.syncfusion.com/downloads/support/forum/161833/pd/Result-223730724

If you still facing issue means, please share the modified sample of above to reproduce the exact problem at our end. Thereby, we can proceed further to analyze and update you with the appropriate details.

Regarding "Created with a trial version of Syncfusion Essential PDF". Is DocIO no longer free for those with a license?:
We suspect that you have used NuGet packages without license key or used with trial version in your sample application so, reported issue occurs. We suggest you to use the license key in your application or licensed assemblies to resolve the reported issue. Please refer the below links to get license key:
 



FA Fausto January 28, 2021 11:38 AM UTC

Hi again, 

The problem remains, it must be due to versions of the asssemblies I was using for DocIO. I made a backup of the application and upgraded essential studio to version 18.4.0.30 (I was working with vs 3.0.53). I know I'm going to have a lot of compilation errors, but I'll try it anyway, since the Word library is an important part of the application I'm developing. Using WordML as an alternative, is something I want to avoid ...

I have two questions (for now :):

1. after upgrading from Essential studio, libraries for Word and Excel have to be installed separately via nuget, right?
2. I can't find Syncfusion's Project Migration in the extensions. I searched the net and found no clues; through the visual studio extension manager, no luck again; do I have to load and install it separately? If so, can you guide me through the process?

Thanks again.

Regards, 
Fausto


FA Fausto January 28, 2021 12:03 PM UTC

I include the Manage Extensions screenshot (no Blazor Extensions in the options).






HC Hemalatha Chiranjeevulu Syncfusion Team January 29, 2021 06:54 PM UTC

Hi Fausto,

Thank you for your update.

Regarding after upgrading from Essential studio, libraries for Word and Excel have to be installed separately via nuget, right?

If you wish to use NuGet package, you can use from build installed location or download from nuget.org. If you wish to use assemblies, you can get it from build installed location (latest Essential Studio).

Regarding I can't find Syncfusion's Project Migration in the extensions. I searched the net and found no clues; through the visual studio extension manager, no luck again; do I have to load and install it separately? If so, can you guide me through the process
?

We can able to reproduce the reported issue from our side, when the application doesn’t have the NuGet packages which doesn’t have the keyword Syncfusion.Blazor. In our project migration tool for Blazor we are showing the migration option based on the already available Syncfusion NuGet packages which have the keyword Syncfusion.Blazor. In this case for Word and Excel control we are using the Syncfusion.DocIo.Net.Core and Syncfusion.XlsIO.Net.Core NuGet packages only in the application. Here project file doesn’t have the Syncfusion.Blazor keyword contains NuGet packages. So, the Syncfusion Project Migration option doesn’t show in the solution explorer context menu. We have created the bug report for this issue from our side. We will fix this issue and will be included in our upcoming release. Please track this issue from the below feedback link.

https://www.syncfusion.com/feedback/21918/project-migration-is-not-visible-when-the-project-file-dont-have-the-syncfusion

Regarding include the Manage Extensions screenshot (no Blazor Extensions in the options):

From your screenshot the Syncfusion Blazor extensions has been already installed in your machine. We are naming the Syncfusion Blazor extensions like “Syncfusion Blazor Template Studio” and that name is available in your Extension Manager dialog.

Please let us know if you have any other questions.

Regards,
Hemalatha C
 



FA Fausto January 29, 2021 07:44 PM UTC

Hi, 


I have already upgraded to version 18.4.0.39, from the assemblies that I am actually using in the application. There is a lot of code to fix, as I calculated, but the process is going well. In terms of stylesheeets (css), I'm still not sure what happened, there are controls (menus, for example) that appear in strange places ...
Will I not have to upgrade them too?

The "colorpicker" control, for example, does not work at all. The component is inserted inside a dialog box, with several Tabs
A large square is shown, hiding the controls below ...

<SfTab Width="800" ID="TabId">
    <TabItems>
        <TabItem>
            <ChildContent>
                <TabHeader Text=@L["TituloIdentificacao"]></TabHeader>
            </ChildContent>
            <ContentTemplate>
........

<div class="form-group">
                <div>
                        <label class="control-label font-weight-bold"> @L["TituloEscolhaCor"]</label>
</div>
                        <div>
                        <SfColorPicker @bind-Value="@SelectedDoctor_Record.DoctorColor" ValueChange="OnChange"></SfColorPicker>
</div>
</div>
            </ContentTemplate>
        </TabItem>

..... more tabs ...

  protected void OnChange(ColorPickerEventArgs args)
    {
        string hexValue = args.CurrentValue.Hex;
        SelectedDoctor_Record.DoctorColor = hexValue;
    }



If necessary, I can send the component's source code in a zip file

Thanks again, 

Regards, 
Fausto


AS Aravinthan Seetharaman Syncfusion Team February 2, 2021 02:26 AM UTC

Hi Fausto, 
 
We have checked your reported issue with provided details. We cannot reproduce your reported issue in our end. If possible, share the issue reproducible sample, based on that we will check and provide you a better solution quickly. 
 
Regards, 
Aravinthan S 



FA Fausto February 2, 2021 02:59 AM UTC

Hi,

As requested, I attach the component where the problem exists. 

The situation occurs even if you only have the SfColorPicker declaration on a page.

This occurred when I upgraded from version 18.3.0.53 to 18.4.0.39.

Regards, 
Fausto

Attachment: EditDoctor_8cabee88.zip


AS Aravinthan Seetharaman Syncfusion Team February 2, 2021 04:53 PM UTC

 
Thanks for the Sample. 
 
We have checked your reported issue with provided sample. We cannot reproduce your reported issue in our end. However, we suspect that this issue occurred due to referring three different CSS theme files in _Host.cshtml file. We suggest you please remove unwanted CSS and refer only required CSS. For your reference we have created sample here. 
_Host.cshtml
 
<html lang="en"> 
<head> 
    <link rel='nofollow' href="https://cdn.syncfusion.com/ej2/bootstrap4.css" rel="stylesheet" /> 
</head> 
 <body> 
 </body> 
</html> 
 
 
 
 
To upgrade into newer version please refer the below link. 
 
 
If you are still facing the issue, kindly share the below details. 
 
  • If possible, try to reproduce the reported issue in provided sample or share the issue reproducible sample.
  • Please share us the video demonstration of this issue.
 
Please provide the above requested information, based on that we will check and provide you a better solution quickly. 
 
Regards, 
Aravinthan S 



FA Fausto February 2, 2021 06:37 PM UTC

Solved, thanks.

The problem entry was:

=> https://cdn.syncfusion.com/blazor/18.3.40/styles/bootstrap4.css (one of yours, right? :) )

I now had a problem with the icons contained in the grids I use, since they have now inverted colors; sometimes the icon color change too (from black to white), as you may see in the screenhot bellow..


I use the stylesheets that you have in your 'AppointmentPlanner' demo, could the problem be there? I looked for the various .css, but I couldn't find anything that could justify it. In the site.css also, no clue either (as you suggested, I removed all the css that were not being used). What file (s) should I look for?

Can you help, please?



Thanks, once more.

Regards, 



HB Hareesh Balasubramanian Syncfusion Team February 5, 2021 03:46 AM UTC

Hi Fausto, 

Thanks for the update. 

The appointment planner uses customized local CSS specifically for that application. Hence we suggest you to use our CDN styles or generate a customized styles from theme studio instead of using CSS from 'AppointmentPlanner' demo. 


Kindly try the above solution and get back to us if you need any further assistance. 

Regards, 
Hareesh 


Loader.
Up arrow icon