Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Tuesday, January 6, 2015

Hot to Get the Image form the PDF file in WINDOWS C#

Hot to Get the Image form the PDF file in WINDOWS C#

Step1:- Add the reference of the DLL

using iTextSharp.text;
using iTextSharp.text.pdf;

Download the iTextSharp DLL from this link


The full name spaces will look like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Drawing.Imaging;

Step2:- Create one public class with the name “ExtractImages”

And write the below code

public class ExtractImages
    {


        #region ExtractImagesFromPDF
        public static void ExtractImagesFromPDF(string sourcePdf, string outputPath)
        {
            // NOTE:  This will only get the first image it finds per page.
            PdfReader pdf = new PdfReader(sourcePdf);
            RandomAccessFileOrArray raf = new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf);

            try
            {
                for (int pageNumber = 1; pageNumber <= pdf.NumberOfPages; pageNumber++)
                {
                    PdfDictionary pg = pdf.GetPageN(pageNumber);
                    PdfDictionary res =
                      (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
                    PdfDictionary xobj =
                      (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
                    if (xobj != null)
                    {
                        foreach (PdfName name in xobj.Keys)
                        {
                            PdfObject obj = xobj.Get(name);
                            if (obj.IsIndirect())
                            {
                                PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
                                PdfName type =
                                  (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
                                if (PdfName.IMAGE.Equals(type))
                                {

                                    int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture));
                                    PdfObject pdfObj = pdf.GetPdfObject(XrefIndex);
                                    PdfStream pdfStrem = (PdfStream)pdfObj;
                                    byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)pdfStrem);
                                    if ((bytes != null))
                                    {
                                        using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes))
                                        {
                                            memStream.Position = 0;
                                            // ByteArrayToImage(bytes);

                                            System.Drawing.Image img = System.Drawing.Image.FromStream(memStream, true, true);
                                            // must save the file while stream is open.
                                            if (!Directory.Exists(outputPath))
                                                Directory.CreateDirectory(outputPath);

                                            string path = Path.Combine(outputPath, String.Format(@"{0}.jpg", pageNumber));
                                            System.Drawing.Imaging.EncoderParameters parms = new System.Drawing.Imaging.EncoderParameters(1);
                                            parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0);
                                            // GetImageEncoder is found below this method
                                            System.Drawing.Imaging.ImageCodecInfo jpegEncoder = GetImageEncoder("JPEG");
                                            img.Save(path, jpegEncoder, parms);
                                            break;

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            catch
            {
                throw;
            }
            finally
            {
                pdf.Close();
            }


        }
        #endregion

        //public static System.Drawing.Image ByteArrayToImage(byte[] input)
        //{
        //    try
        //    {
        //        var ms = new MemoryStream(input);
        //        var tempByteArrayToImage = System.Drawing.Image.FromStream(ms);
        //        ms.Dispose();
        //        return tempByteArrayToImage;
        //    }
        //    catch (Exception ex)
        //    {
        //        //ShowMessage.ShowError(ex.Message);
        //        return null;
        //    }
        //}

        #region GetImageEncoder
        public static System.Drawing.Imaging.ImageCodecInfo GetImageEncoder(string imageType)
        {
            imageType = imageType.ToUpperInvariant();



            foreach (ImageCodecInfo info in ImageCodecInfo.GetImageEncoders())
            {
                if (info.FormatDescription == imageType)
                {
                    return info;
                }
            }

            return null;
        }
        #endregion

    }


Step3:- Create once windows (Win) form and write the code in the page load event or in the in the button click (as your wish to use where ever).

private void Form1_Load(object sender, EventArgs e)
        {
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            string localPath = new Uri(path).LocalPath;
            ExtractImages.ExtractImagesFromPDF(localPath + "\\SAMPLE.pdf", localPath);

        }



Note:-

Make sure place once PDF file having images in the debug folder.

I am using the SAMPLE.PDF file in the Tutorial

After execute the code check the debug folder you will find the extracted images in the folder

And also customize the code as you want base on the requirement 
Read More »

Thursday, June 26, 2014

How to write the text file using the WPF application



Introduction

This article goes to understanding of the code of writing the text file from the c# .
Here the “ExceptionHandler” will takes the input parameter as string to what the data to write in the text file
This method “CallExceptionHandler” will work for writing the test in the file
Note:-Make Shure to Create one folder in the c drive with the name of “Log”

public void ExceptionHandler(string ExceptionMessage)
        {
            try
            {
                // string root = Directory.GetCurrentDirectory() + "\\Log";
                // if (!Directory.Exists(root))
                // {
                // Directory.CreateDirectory(root);
                // }

                string root = "C:\\Log\\";
                string path = root + "SampleTextFile" + System.DateTime.Now.ToString("ddMMyyy") + ".txt";

                CallExceptionHandler(path, ExceptionMessage);

            }
            catch (Exception)
            { }
        }
 private void CallExceptionHandler(string strPath, string strException)
        {
            FileStream fsFile = null;
            StreamWriter swWriter = null;
            try
            {
                if (File.Exists(strPath))
                {
                    fsFile = new FileStream(strPath, FileMode.Append, FileAccess.Write);
                    swWriter = new StreamWriter(fsFile);
                    swWriter.WriteLine(Environment.NewLine + DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss") + "-----------" + Environment.NewLine);

                    swWriter.WriteLine(strException);
                }
                else
                {
                    fsFile = new FileStream(strPath, FileMode.Create, FileAccess.Write);
                    swWriter = new StreamWriter(fsFile);
                    swWriter.WriteLine(strException);
                }
                swWriter.Flush();

            }
            catch (Exception)
            {
            }
            finally
            {
                swWriter.Close(); fsFile.Close(); swWriter.Dispose(); fsFile.Dispose();
            }
        }


Read More »