Thursday, March 15, 2012

Capture image from Pdf

Use PDFLibCmdLine.exe and PDFLibNet.dll to your Bin folder to capture image from pdf.
Here FileName is the name of pfd file from which we want to capture image,
and intPageNumber is the page number which we want to capture.
Here is the code for class.
private string getImageFromPDF(string FileName,int intPageNumber)
        {
            try
            {
                intPageNumber = 1;
                string PngName = FileName.Replace(".pdf", "");
                if (File.Exists(Server.MapPath("PDF/" + PngName + ".png").ToString()))
                    return PngName + ".png";

                string imageLocation = null;
                imageLocation = ExternalPDFLib.GetPageFromPDF(Server.MapPath("bin"), Server.MapPath("PDF/" + FileName).ToString(), Server.MapPath("PDF").ToString(), intPageNumber , 120, "", 0, "", 0);
                return imageLocation;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

By this method we can capture image of any page from pdf file.

Additionaly we have to use one more class to use the PDFLibCmdLine.exe and capture the image.

public class ExternalPDFLib
{
    public static string appName = "PDFLibCmdLine.exe";

    const int RENDER_DPI = 150;
   
    public static string GetPageFromPDF(string appPath, string sourceFileName, string destFolderPath, int iPageNumber, int DPI, string password, int rotations, string searchText, int searchDir)
    {
        string functionReturnValue = null;
        functionReturnValue = "";
        string myString = ExecuteCMD(appPath + "\\" + appName, string.Format("{0} \"{1}\" \"{2}\" {3} {4} \"{5}\" \"{6}\" {7}", "png", sourceFileName, destFolderPath, iPageNumber, DPI, password, searchText, searchDir));
        try {
            if (Regex.IsMatch(myString, "^authfail", RegexOptions.IgnoreCase)) {
                return "authfail";
            }
            string myFilePath = Regex.Replace(myString, "png=(.+)\\npage=.*$", "$1").Trim();
            string myPageNumString = Regex.Replace(myString, "[\\d\\D]+page=(\\d+)[\\d\\D]*$", "$1");
           
            functionReturnValue = myFilePath;
            iPageNumber = Convert.ToInt32(myPageNumString);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return functionReturnValue;
    }

    const int CmdTimeout = 3;
    public static string ExecuteCMD(string cmd, string args)
    {
        try
        {
            string functionReturnValue = null;
            functionReturnValue = "";
            using (Process myProcess = new Process())
            {
                myProcess.StartInfo.FileName = cmd;
                myProcess.StartInfo.Arguments = args;
                myProcess.StartInfo.UseShellExecute = false;
                myProcess.StartInfo.RedirectStandardOutput = true;
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.Start();
                ThreadedKill(myProcess.Id);
                //myprocess.PriorityClass = ProcessPriorityClass.Normal;
                functionReturnValue = myProcess.StandardOutput.ReadToEnd();
                myProcess.WaitForExit();               
            }
            return functionReturnValue;
        }
        catch (Exception ex)
        {
            return "";
        }
    }

    public static void KillProcessAfterTimeout(object Opid)
    {
        try
        {
            int pid = (int)Opid;
            Process pProcess = System.Diagnostics.Process.GetProcessById(pid);
            DateTime expiration = DateTime.Now.AddSeconds(CmdTimeout);
            while (DateTime.Now < expiration)
            {
                Thread.Sleep(100);
                if (null == pProcess)
                {
                    return;
                }
            }
            if (pProcess != null)
            {
                pProcess.Kill();
            }
        }
        catch (Exception ex)
        {
        }
    }

    public static void ThreadedKill(int pid)
    {
        Thread myThread = new Thread(new ParameterizedThreadStart(KillProcessAfterTimeout));
        myThread.Start(pid);
    }


}


No comments:

Post a Comment