Click here to Skip to main content
Click here to Skip to main content

PDF To HTML SlideShow

By , 14 Dec 2009
 

Introduction

I created a tool that converts a simple PDF page into an HTML slide show page. This tool uses Acrobat reader Professional DLL and it will slice the PDF into JPG images, and the results will be displayed in a simple HTML slide show.

Using the Code

The form contains 2 the source PDF file and the destination folder where the HTML slide show will be generated. We first select the PDF, then start parsing the PDF to check the number of pages, and prepare the destination folder.

string path = textBox1.Text;
string folder = openFileDialog1.SafeFileName.Replace(".pdf", "");
string destination = textBox2.Text + @"\" + folder;

//create the new directory for the new FlipBook
System.IO.Directory.CreateDirectory(destination);
System.IO.Directory.CreateDirectory(destination + @"\Pages"); 
Acrobat.AcroPDDoc pdf = new Acrobat.AcroPDDoc();
Acrobat.AcroPDPage pdfPage;
Acrobat.AcroPoint pdfRectTemp;
Acrobat.AcroRect pdfRect;

pdf.Open(path);

int pageNum = pdf.GetNumPages(); 

After we get the number of pages, we start looping on each page, get the height and width of the internal page, copy the page into the clipboard, paste the clipboard into a bitmap image.

for (int i = 0; i < pageNum; i++)
{
    pdfPage = (Acrobat.AcroPDPage)pdf.AcquirePage(i);
    pdfRectTemp = (Acrobat.AcroPoint)pdfPage.GetSize();
    pdfRect = new Acrobat.AcroRect();
    pdfRect.Left = 0;
    pdfRect.right = pdfRectTemp.x;
    pdfRect.Top = 0;
    pdfRect.bottom = pdfRectTemp.y;
    pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);

    IDataObject clipboardData = Clipboard.GetDataObject();
    if (clipboardData.GetDataPresent(DataFormats.Bitmap))
    {    
        Bitmap pdfBitmap = clipboardData.GetData
	(System.Windows.Forms.DataFormats.Bitmap) as Bitmap;
        //create a dummy image then resize it to the real sizes
        ImageUtility.GenerateImage(pdfBitmap, destination + @"\Pages\Page" + 
		(i + 1).ToString("00") + ".jpg");
    }
}

We then resize the image using the class ImageUtility to resize the image and convert it into a JPG image with good resolution and fixed size. Then we save the generated images into the destination folder.

After we finish the image generation, we read the HTML body and add the script for the slide show.

System.IO.StreamReader reader = new System.IO.StreamReader
	(Application.StartupPath + "/HtmlBook/HtmlBook.html", 
	System.Text.Encoding.GetEncoding("Windows-1256"));
string strFileContents = reader.ReadToEnd();
reader.Close();

System.IO.DirectoryInfo pagesFolder = 
	new System.IO.DirectoryInfo(destination + @"\Pages");
FileInfo[] pages = pagesFolder.GetFiles();
//set the javascript file for rotation
string script = @"var list = new Array();";

int k = 0;
foreach (FileInfo file in pages)
{
    script += @"
    list[" + k + @"] = ""Pages/" + file.Name + @"""
    ";
    k++;
}

strFileContents = strFileContents.Replace("@@Javascript", script);
//save the file after generating the javascript html
StreamWriter sw = new StreamWriter(destination + "/HtmlBook.html");
sw.Write(strFileContents);
sw.Flush();
sw.Close();

The generated images will be added to a JavaScript array replaced on top of the HTML template and then saved back to the destination folder.

At the end, you will find a folder contains a simple HTML page with images generated aside. The HTML page will display the content of the PDF file:

New_Picture__2_.jpg

History

  • 14th December, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Suha Mneimneh
Team Leader IDS
Lebanon Lebanon
Member
Adore programming, interested in workflows, SharePoint and silverlight, Entity Framework.
My Blog: http://suhamneimne.wordpress.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAn small problemmemberV.SivaRamaKriShnaRaju27 Oct '10 - 1:41 
Hi
 

It is an nice article i found for converting an pdf to html file and it is really useful for most of the people and i downloaded the code and try to execute it but it throwing an error while conversion, and which is related to com components and the error message is as follows "Retrieving the COM class factory for component with CLSID {FF76CB60-2E68-101B-B02E-04021C009402} failed due to the following error: 80040154."
 
please let me know how to solve this error.
 

Thanks and Regards,
v.s.r.k.raju.
GeneralRe: An small problemmemberSuha Mneimneh27 Oct '10 - 11:53 
Thanks for you note.
 
In order to run the code correctly, you need to check that you have the Acrobat Professional, not just the Acrobat Reader, usually this error comes when the application don't find the proper assemblies in your system. So it is required to have the Acrobat Professional installed.
 
In case you still encounter any problem please don't hesitate to contact me
 
Best Regards,
GeneralMy vote of 3memberloverszhaokai27 Jul '10 - 15:49 
Sorry to fail it.
then I wanna send you the errors.
GeneralRe: My vote of 3memberSuha Mneimneh27 Jul '10 - 19:55 
what kind of error you are getting ?
GeneralHi SuhamemberMoooooodi28 Mar '10 - 1:35 
Thanks,
 
It's very useful.
 
Shokran kteer kteer l2ene kont 3m dawer 3la hek she mn zman !!!
GeneralRe: Hi Suhamemberloverszhaokai27 Jul '10 - 17:10 
Sorry to say that I only understand English.
what do you mean by say that? Sleepy | :zzz:
QuestionMy question is whether this Professional DLL is free or comes with commercial version?memberPriyank Bolia14 Dec '09 - 2:41 
My question is whether this Professional DLL is free or comes with commercial version? If I need some license from Adobe, then its as bad as any other PDF2HTML software available on net.
 

GeneralIt's usefulgroupMd. Marufuzzaman14 Dec '09 - 1:36 
Good work...Thumbs Up | :thumbsup:
 
Thanks
Md. Marufuzzaman


Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
 
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

GeneralRe: It's usefulmemberloverszhaokai27 Jul '10 - 17:08 
Wink | ;) It sounds funny.Unfortunately I wonder if the program can go without errors.
 
I am going to cry ,as I have failed many times.
 
who can help me? Big Grin | :-D
GeneralMy vote of 2membergaurav_verma_mca14 Dec '09 - 0:33 
Nothing great... its something we can google out.

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 14 Dec 2009
Article Copyright 2009 by Suha Mneimneh
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid