Object is currently in use elsewhere while generating PDF

We have the latest Aspose.Pdf 10.5 .NET and currently facing an issue where a few users “Click” generate PDF using the BindHTML gives random user of "System.InvalidOperationException: Object is currently in use elsewhere"


Anyone had faced this issue? I don’t think this is an acceptable behavior in Aspose.PDF.

Aspose.Pdf.Generator.Pdf.BindHTML(String html, String basePath)
STACKTRACE:
LOGMSG="System.InvalidOperationException: Object is currently in use elsewhere.
at System.Drawing.Graphics.FromImage(Image image)
at ?.?.(String , Single , FontStyle )
at ?.?.?(? , TextInfo , ??? )
at ?.?.?( )
at ?.?.Accept(? , Int32 )
at ?.?.Accept(? , Int32 )
at ?.?.Accept(? , Int32 )
at ?.?.Accept(? , Int32 )
at ?..Accept(? , Int32 )
at ?.?.?( )
at Aspose.Pdf.Generator.Pdf.BindHTML(String html, String basePath)

Code:
private MemoryStream GenerateDirectDepoistFormPdfFromHtml(AposePdfConfiguration config, string checkImageUrl, string clockImageUrl)
{
using (var memoryStream = new MemoryStream())
{
try
{
var lcn = new License();
lcn.SetLicense(“Aspose.Pdf.lic”);

var depositFormPdf = new Pdf
{
HtmlInfo =
{
ImgUrl = config.ImageUrl
},
PageSetup =
{
PageWidth = Aspose.Pdf.Generator.PageSize.LetterWidth,
PageHeight = Aspose.Pdf.Generator.PageSize.LetterHeight,
Margin = { Top = 36, Right = 36, Left = 36, Bottom = 36 }
},
IsFontNotFoundExceptionThrown = config.IsFontNotFoundExceptionThrown,
IsPageNumberForDocument = true

};

//setting image watermark
var checkImage = new Image
{
ImageInfo =
{
File = checkImageUrl,
ImageFileType = ImageFileType.Png
},
ImageScale = .90f
};

var clockImage = new Image
{
ImageInfo =
{
File = clockImageUrl,
ImageFileType = ImageFileType.Png
},
ImageScale = .70f
};

var checkImageWaterMark = new FloatingBox { Left = 200, Top = 91 };
checkImageWaterMark.Paragraphs.Add(checkImage);
depositFormPdf.Watermarks.Add(checkImageWaterMark);
depositFormPdf.IsWatermarkOnTop = false;

var depositSection = depositFormPdf.Sections.Add();

// Create FloatingBox with 40 as width and 40 as height
var clockImageWaterMark = new FloatingBox { Left = 5, Top = 573 };
// add FloatingBox to paragraphs collection of section object
depositSection.Paragraphs.Add(clockImageWaterMark);
clockImageWaterMark.Paragraphs.Add(clockImage);

depositFormPdf.BindHTML(config.HtmlToRender);
depositFormPdf.Save(memoryStream);

}
catch (Exception ex)
{
Logger.Error(ex);
throw;
}
return memoryStream;
}
}

Hi Jeff,


Thanks for contacting support.

It appears that you are using a legacy Aspose.Pdf.Generator approach for HTML to PDF conversion and now we recommend using new Document Object Model for Converting HTML to PDF Format but still we request you to please share some sample project along with your resource (HTML files), so that we can test the scenario in our environment. We are sorry for this inconvenience.

It looks like this multiple thread issue is widespread in this forum.

Here’s our code snippet spawning 50 concurrent users:

class Program
{
    static void Main(string[] args)
    {
        var lcn = new License();
        lcn.SetLicense("Aspose.Pdf.lic");

        for (int i = 0; i < 50; i++)
        {
            Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
            thread.Start();
        }

        Console.WriteLine("Hello!!!");
        Console.ReadKey();
    }

    public static void WorkThreadFunction()
    {
        try
        {
            var htmlContent = "this is a link to a web site like [this](http://www.aspose.com/)";

            Console.WriteLine("generate method");

            String html = "this is a link to a web site like [this](http://www.aspose.com/)";

            HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
            htmlLoadOptions.PageInfo.Margin.Bottom = 10;
            htmlLoadOptions.PageInfo.Margin.Top = 20;

            using (var memorystream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
            {
                Document doc = new Document(memorystream, htmlLoadOptions);
                doc.Save(Guid.NewGuid().ToString() + "output.pdf");
            };
        }
        catch (Exception ex)
        {
            Console.WriteLine("WorkThreadFunction:: " + ex.Message);
        }
    }
}

Hi Jeff,


Thanks for your feedback and source code. I have tested you code with Aspose.Pdf for .NET 10.6.0 and unable to notice the issue the reported issue. I will appreciate it if you pleas share a sample console project to replicate the issue, so we will investigate it further and update you accordingly.

We are sorry for the inconvenience caused.

Best Regards,

We found a not so great of fallback mechanism to RE-TRY rendering the Document if we get an exception.



We just bought Aspose.PDF 10.5 a month ago. Does this mean we get a free update since you mentioned 10.6?

Hi Jeff,


Thanks for your inquiry. Please check subscription expiry date from your license file. Please note you are entitled to use any Aspose.Pdf for .NET version released before your subscription expiry, as long as you wish. But without subscription renewal you will not be able to use new releases, launched after your subscription expiry.

Please feel free to contact us for any further assistance.

Best Regards,