We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Saving a .doc to Pdf/A results in a System.Drawing System.InvalidOperationException - 'Object is currently in use elsewhere.'

Good morning,

we are using Aspose.Words to convert .doc files to .pdf ones (particularly pdf/a) and the code looks like this:

/// 
/// The (pdf) save options
/// 
internal static SaveOptions DocumentSaveOptions = new PdfSaveOptions
{
    Compliance = PdfCompliance.PdfA1b,
    ImageColorSpaceExportMode = PdfImageColorSpaceExportMode.Auto,
    FontEmbeddingMode = PdfFontEmbeddingMode.EmbedAll,
    EmbedFullFonts = true,
    UseHighQualityRendering = true
};
using (var inputFileMemoryStream = new MemoryStream(fileToConvert.Content)) // << fileToConvert.Content is a byte[] containing the .doc
{
    // load input document into Aspose lib
    var document = new Document(inputFileMemoryStream, new LoadOptions { LoadFormat = LoadFormat.Auto });
    cancellationToken.ThrowIfCancellationRequested();
    using (var documentToPdfOutputStream = new MemoryStream())
    {
        // save as pdf
        document.Save(documentToPdfOutputStream, DocumentSaveOptions); // **<< THIS is were the exception is happening**
                                                                        // …
    }
}

However, sporadically (this is a multithreaded application and the multiple (different) documents CAN be converted at the same time) we get the following exception:

System.Drawing : System.InvalidOperationException
Object is currently in use elsewhere.
at System.Drawing.Font.ToLogFont(Object logFont, Graphics graphics)
at .(String , Single , FontStyle )
at .(String , Single , FontStyle )
at .(String , Single , FontStyle )
at .(String , Single , FontStyle )
at ? .( , Single )
at ? . ()
at . ()
at . ()
at . ()
at ? .()
at ? .(? , Int32 )
at ? .(? )
at ? .(? )
at ? .(? , Int32 )
at .( , Int32 , Boolean )
at .()
at .( , Int32 , Boolean , Boolean )
at . ()
at .( , Int32 )
at .( )
at .( )
at .( )
at .(Boolean )
at .(Boolean )
at . ()
at .(Document , )
at Aspose.Words.Document.UpdatePageLayout()
at Aspose.Words.Document.(Boolean )
at Aspose.Words.Document. ()
at Aspose.Words.Document.get_PageCount()
at .( , ? )
at .? ( )
at Aspose.Words.Document.(Stream , String , SaveOptions )
at Aspose.Words.Document.Save(Stream stream, SaveOptions saveOptions)
at Backend.Host.AttachmentConverters.AsposeWordsConverter.d__8.MoveNext() in d:\Workspaces\TII\ProductDevelopment\Backend\Backend.Host\AttachmentConverters\AsposeWordsConverter.cs:line 85
---- snip

The version is 14.10.0.0.

What’s going on here? Can I not use Aspose.Words in a multithreaded application?! And/or what can I do to prevent this?

Thanks,

-Jörg B.

This also happens for .rtf files, not only .doc ones.

Hi Joerg,

Thanks for your inquiry. Could you please attach your input Word documents you’re getting this problem with here for testing? We will investigate the issue on our end and provide you more information.

Best regards,

Good morning Awais,

this problem happens only under heavy/high load and not for a or multiple specific documents (the exemplary documents mentioned before work perfectly fine when converted on a system with 0% load).

So basically only under heavy load the conversion API seems to “break apart” - as the exception shows there’s apparently System.Drawing involved/used by Aspose.Words which is quite surprising to me asSystem.Drawing is known to have problems (leaks/runtime exceptions) for Server applications and I basically would like to know: is Aspose.Words supported in Server environments, particularly multi-threaded applications with high throughput and if so, why does it not work reliably?

Thanks,

-Jörg

Hi Jörg,

Thanks for your inquiry. Yes, Aspose.Words supports multi-threading and it can be used on servers under heavy load; the only thing you need to make sure is that you always use separate Document instances per each thread. One thread should use one Document object. Secondly, the Document object provides a family of the Print methods to print documents and these methods print via the .NET printing classes defined in the System.Drawing.Printing namespace that is why Aspose.Words uses this .NET namespace.

Best regards,