Click here to Skip to main content
15,881,600 members
Articles / Web Development / ASP.NET
Article

ASP.NET Clipboard for PDF Rasterization

Rate me:
Please Sign up or sign in to vote.
4.41/5 (7 votes)
20 Sep 20062 min read 72.4K   26   18
Use the clipboard to rasterize and/or resize a PDF file. This allows you to also save it for use as a JPG.

Background

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.

Problem

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 null.

Solution

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:

C#
protected void Button_Click(object sender, EventArgs e)
{
     Thread cbThread = new Thread(new ThreadStart(CopyToClipboard));
     cbThread.ApartmentState = ApartmentState.STA;
     cbThread.Start();
     cbThread.Join();
}

[STAThread]
protected void CopyToClipboard()
{
     /*
     In here, put the code that copies AND retrieves from the clipboard.
     If you use global variables, the Bitmap you populate 
     here can be used elsewhere in your code.
     */
}

Final Notes

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.

History

This article was posted first on the telerik forums.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow it works on windows Pin
abdulqadar11-Aug-08 20:13
abdulqadar11-Aug-08 20:13 
Generalhelp me....................... Pin
Manish Langa22-Jul-08 19:14
Manish Langa22-Jul-08 19:14 
Generalthanks .. this code help me.. Pin
Manish Langa22-Jul-08 1:15
Manish Langa22-Jul-08 1:15 
GeneralReally Helpfull Pin
Komal Bhatia16-Mar-08 20:57
Komal Bhatia16-Mar-08 20:57 
GeneralReally It help in the same case given by U Pin
KKGaurav6-Nov-07 1:18
KKGaurav6-Nov-07 1:18 
QuestionHelp to tune the settings Pin
zan20063-Apr-07 5:44
zan20063-Apr-07 5:44 
GeneralI've the same problem Pin
Unfounds23-Jan-07 3:29
Unfounds23-Jan-07 3:29 
GeneralRe: I've the same problem Pin
Oleg Fridman23-Jan-07 3:33
Oleg Fridman23-Jan-07 3:33 
GeneralRe: I've the same problem Pin
shin99921-Jun-07 12:01
shin99921-Jun-07 12:01 
GeneralNot work in Win2003 server with IIS6.0 Pin
Ajay Singh24-Oct-06 1:57
Ajay Singh24-Oct-06 1:57 
AnswerRe: Not work in Win2003 server with IIS6.0 Pin
Oleg Fridman24-Oct-06 10:52
Oleg Fridman24-Oct-06 10:52 
Thanks for your interest in this code.

Try this for me:

Put a break in the code right before the "Clipboard.GetDataObject();" line but after the pdfPage.CopyToClipboard(pdfRect, 0, 0, 100); line.

Now, when the break hits, open up Paint on the server (or even Clipboard viewer), and see if there is anything there. If there is, what type is it? If you did it with paint, you should be able to hit Edit->Paste and see the pdf file in paint.

Also, are you sure you are running the line "Clipboard.Clear();"?

I have heard of problems with multiple items in the clipboard if it is not cleared every time.

I was not able to find much online about this error...

Sorry. Give those two things a shot and let me know.

-Oleg Fridman
AnswerRe: Not work in Win2003 server with IIS6.0 Pin
Oleg Fridman26-Oct-06 9:27
Oleg Fridman26-Oct-06 9:27 
GeneralRe: Not work in Win2003 server with IIS6.0 Pin
patric4213-Nov-07 2:35
patric4213-Nov-07 2:35 
GeneralRe: Not work in Win2003 server with IIS6.0 Pin
Oleg Fridman13-Nov-07 2:42
Oleg Fridman13-Nov-07 2:42 
GeneralWatch your EULA Pin
lrosenthol26-Sep-06 10:46
lrosenthol26-Sep-06 10:46 
GeneralRe: Watch your EULA Pin
Oleg Fridman26-Oct-06 9:30
Oleg Fridman26-Oct-06 9:30 
QuestionThis was right on the mark... Pin
westberg25-Sep-06 11:25
westberg25-Sep-06 11:25 
AnswerRe: This was right on the mark... Pin
Oleg Fridman25-Sep-06 14:54
Oleg Fridman25-Sep-06 14:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.