hi.
my application has a timer that once a day will create a pdf using spire.pdf.
when i a trying to do that it gives me an error of current thread is not in a single-threaded apartment.
how can i overcome this problam?
PdfDocument document = new PdfDocument();
Thread thread = new Thread(() =>{
document.LoadFromHTML("test.html", true, true, true);
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
document.SaveToFile("sample.pdf")
"Message":"ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.","Error":{"ClassName":"System.Threading.ThreadStateException","Message":"ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":"
at System.Windows.Forms.WebBrowserBase..ctor(String clsidString)\r\n
at System.Windows.Forms.WebBrowser..ctor()\r\n
at Spire.Pdf.HtmlConverter.HtmlConverter.ᜀ()\r\n
at Spire.Pdf.HtmlConverter.HtmlConverter..ctor()\r\n
at Spire.Pdf.PdfSection.LoadFromHTML(String url, Boolean enableJavaScript, Boolean enableHyperlinks, PdfHtmlLayoutFormat layoutFormat)\r\n
at Spire.Pdf.PdfDocument.LoadFromHTML(String Url, Boolean enableJavaScript, Boolean enableHyperlinks, Boolean autoDetectPageBreak)\r\n
public static void Create(string urltoconvert, string savepath)
{
var doc = new PdfDocument();
var thread = new Thread(() => doc.LoadFromHTML(urltoconvert, false, true, true));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
doc.SaveToFile(savepath);
doc.Close();
System.Diagnostics.Process.Start(savepath);
}
Pdf.Generate.Create(url, filepath);