Click here to Skip to main content
6,633,937 members and growing! (18,103 online)
Email Password   helpLost your password?
Web Development » Charts, Graphs and Images » Images and multimedia     Intermediate

ASP.NET Clipboard for PDF Rasterization

By Oleg Fridman

Use the clipboard to rasterize and/or resize a PDF file. This allows you to also save it for use as a JPG.
C#, VB, Windows, .NET 2.0, ASP.NET, GDI+, VS2005, Dev
Posted:20 Sep 2006
Views:30,873
Bookmarked:25 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 3.73 Rating: 4.41 out of 5

1

2

3
1 vote, 14.3%
4
6 votes, 85.7%
5

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:

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

About the Author

Oleg Fridman


Member

Location: United States United States

Other popular Charts, Graphs and Images articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
QuestionHow it works on windows Pinmemberabdulqadar21:13 11 Aug '08  
Generalhelp me....................... Pinmemberlangamanish@yahoo.com20:14 22 Jul '08  
Generalthanks .. this code help me.. Pinmemberlangamanish@yahoo.com2:15 22 Jul '08  
GeneralReally Helpfull PinmemberKomal Bhatia21:57 16 Mar '08  
GeneralReally It help in the same case given by U PinmemberKKGaurav2:18 6 Nov '07  
QuestionHelp to tune the settings Pinmemberzan20066:44 3 Apr '07  
GeneralI've the same problem PinmemberUnfounds4:29 23 Jan '07  
GeneralRe: I've the same problem PinmemberOleg Fridman4:33 23 Jan '07  
GeneralRe: I've the same problem Pinmembershin99913:01 21 Jun '07  
GeneralNot work in Win2003 server with IIS6.0 PinmemberAjay Singh2:57 24 Oct '06  
AnswerRe: Not work in Win2003 server with IIS6.0 PinmemberOleg Fridman11:52 24 Oct '06  
AnswerRe: Not work in Win2003 server with IIS6.0 PinmemberOleg Fridman10:27 26 Oct '06  
GeneralRe: Not work in Win2003 server with IIS6.0 Pinmemberpatric423:35 13 Nov '07  
GeneralRe: Not work in Win2003 server with IIS6.0 PinmemberOleg Fridman3:42 13 Nov '07  
GeneralWatch your EULA Pinmemberlrosenthol11:46 26 Sep '06  
GeneralRe: Watch your EULA PinmemberOleg Fridman10:30 26 Oct '06  
QuestionThis was right on the mark... Pinmemberwestberg12:25 25 Sep '06  
AnswerRe: This was right on the mark... PinmemberOleg Fridman15:54 25 Sep '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Sep 2006
Editor: Smitha Vijayan
Copyright 2006 by Oleg Fridman
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project