Ticket T1285230
Visible to All Users

Document generation using RichEditDocumentServer performance tips

created 5 days ago

Hello,

We are evaluating the Office File API to create Word documents and PDF documents - a sample here is created with a competitors product.
https://www.centrel-solutions.com/media/xiaconfiguration/capabilities/activedirectory.pdf

The following sample code takes 10 seconds to generate a 200 page document - we'd like to make this faster.

We found that creating the table with the correct rows and columns then inserting text and images was much faster than appending rows and columns one at a time. The BeginUpdate() and EndUpdate methods seem to make some difference.

Are there any other improvements you can recommend?

Is there any way we can reuse images that are used multiple times in these tables - would that make it faster to remove the duplication?

C#

private void GenerateDocument()
{
Stopwatch stopwatch = Stopwatch.StartNew();
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
Document document = wordProcessor.Document;
document.BeginUpdate();
document.Unit = DevExpress.Office.DocumentUnit.Point;
document.CompatibilitySettings.CompatibilityMode = CompatibilityMode.Mode15;
for (int i = 0; i < 100; i++)
{
Section section = document.AppendSection();
Table table = document.Tables.Create(document.Range.End, 50, 5);
table.BeginUpdate();
for (int j = 0; j < 50; j++)
{
DocumentImageSource imageSource = DocumentImageSource.FromFile(@"D:\CENTRELSolutions\XIAConfiguration\ConfigurationServer\SourceCode\images\tables\BackupExecServer\NetworkSettings.png");
DocumentImage documentImage = document.Images.Insert(table.Rows[j].Cells[0].ContentRange.End, imageSource);
documentImage.Size = new SizeF(PixelsToPoints(16), PixelsToPoints(16));
document.InsertText(table.Rows[j].Cells[0].ContentRange.End, "Testing");
document.InsertText(table.Rows[j].Cells[1].ContentRange.End, "The performance");
}
table.EndUpdate();
}
document.EndUpdate();
String filename = @"C:\temp\document2.docx";
document.SaveDocument(filename, DocumentFormat.OpenXml);
Process.Start(filename);
stopwatch.Stop();
MessageBox.Show(stopwatch.Elapsed.TotalSeconds.ToString());
}
}

Code

Thanks,

Dave

Answers approved by DevExpress Support

created 4 days ago

Hello David,

Thank you for the code snippet. I tested it on my side and confirm that it takes a significant amount of time (from 10 to 30 seconds, depending on the inserted image) to generate a document. You can, however, significantly improve performance if you generate a template document first, and use the AppendDocumentContent method to append the required template document to the main document.

I created a small project that illustrates this solution. On my side, this solution takes approximately 6 seconds and doesn't depend on the inserted image size. Please find it attached.

Also, depending on your usage scenario, you may want to consider the following options:

I look forward to your reply.

    Comments (2)

      Hi Sasha,

      Thanks for the reply - I think we'll need to generate the tables manually, our software writes technical documentation for various systems such as Active Directory, Azure etc so there isn't really a fixed template we can use.

      That's fine as long as we're using the optimal way and we're not missing any code and we can't reuse images that are used multiple times to save time?

      Thanks,

      Dave

      Sasha (DevExpress Support) 4 days ago

        Hello David,

        Thank you for the clarification.

        You are right; you can't reuse images. I found, however, that you can slightly improve performance if you create DocumentImageSource outside of the for loop:

        C#
        DocumentImageSource imageSource = DocumentImageSource.FromFile(imagePath);

        You can use this approach and create DocumentImageSources for all images simultaneously.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.