Click here to Skip to main content
15,881,248 members
Articles / Web Development / HTML

PDF To HTML SlideShow

Rate me:
Please Sign up or sign in to vote.
3.22/5 (9 votes)
14 Dec 2009CPOL1 min read 57K   2.2K   23   12
Generate a simple PDF file into an HTML image SlideShow
Image 1

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.

C#
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.

C#
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.

C#
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)


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

Comments and Discussions

 
QuestionSource code Pin
Michael Haephrati19-Aug-18 8:14
professionalMichael Haephrati19-Aug-18 8:14 
GeneralAn small problem Pin
V.SivaRamaKriShnaRaju27-Oct-10 1:41
V.SivaRamaKriShnaRaju27-Oct-10 1:41 
GeneralRe: An small problem Pin
Suha Mneimneh27-Oct-10 11:53
Suha Mneimneh27-Oct-10 11:53 
GeneralMy vote of 3 Pin
loverszhaokai27-Jul-10 15:49
loverszhaokai27-Jul-10 15:49 
GeneralRe: My vote of 3 Pin
Suha Mneimneh27-Jul-10 19:55
Suha Mneimneh27-Jul-10 19:55 
GeneralHi Suha Pin
Moooooodi28-Mar-10 1:35
Moooooodi28-Mar-10 1:35 
GeneralRe: Hi Suha Pin
loverszhaokai27-Jul-10 17:10
loverszhaokai27-Jul-10 17:10 
QuestionMy question is whether this Professional DLL is free or comes with commercial version? Pin
Priyank Bolia14-Dec-09 2:41
Priyank Bolia14-Dec-09 2:41 
GeneralIt's useful Pin
Md. Marufuzzaman14-Dec-09 1:36
professionalMd. Marufuzzaman14-Dec-09 1:36 
GeneralRe: It's useful Pin
loverszhaokai27-Jul-10 17:08
loverszhaokai27-Jul-10 17:08 
GeneralMy vote of 2 Pin
gaurav_verma_mca14-Dec-09 0:33
gaurav_verma_mca14-Dec-09 0:33 
GeneralRe: My vote of 2 Pin
Xmen Real 16-Dec-09 15:06
professional Xmen Real 16-Dec-09 15:06 

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.