The first question you may ask is "Why would you ever want to use the server's clipboard to do anything?" Good question . I came across this problem when I was trying to write a PDF parser that would rasterize the front page and save it into a
Bitmap object. From here, you can save it to a file, database, etc. To do this, I was using the Adobe Acrobat COM library that comes with Adobe Acrobat 7.0. Unfortunately, they do not have a function that allows you to simply save to a file. They do, however, let you copy the image to the clipboard and then recover it in whatever format you want.
I found some great code by Jonathan Hodgson here: Generate Thumbnail Images from PDF Documents. This code was written for a C#/VB.NET Windows app, and works great if used that way. However, when I tried to use this text in the
OnClick event of an ASP.NET
Button control, I found that nothing was happening. Turns out, the
CopyToClipboard command was working fine, because if I traced through, I could press Ctrl+v and see the image perfectly. However, when the
Clipboard.GetObject method was called, it was always returning
After much digging and two days of work, I stumbled on the reason: the thread started for the ASP.NET application did not have the right
ApartmentState to read the
Clipboard class. So, here is what I did to work around this:
protected void Button_Click(object sender, EventArgs e)
Thread cbThread = new Thread(new ThreadStart(CopyToClipboard));
cbThread.ApartmentState = ApartmentState.STA;
protected void CopyToClipboard()
I do not recommend doing this in a multi-user environment as there is no guarantee that the user that copied to the clipboard will be the one who retrieves from it. Also, images/PDFs can be very large and the clipboard is slow.
I hope this is of some use to someone. It solved my problem, and saved me $900 by not having to buy a PDF Rasterizer control. Feel free to respond, and let me know if it helped you out or if you have any questions.
This article was posted first on the telerik forums.