Exemple #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() != string.Empty)
            {
                iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);

                // creation of the different writers
                iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(@"C:\temp\Converted.pdf", System.IO.FileMode.Create));

                // load the tiff image and count the total pages
                System.Drawing.Bitmap bm = new System.Drawing.Bitmap(this.openFileDialog1.FileName);
                int total = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);

                document.Open();
                iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
                for (int k = 0; k < total; ++k)
                {
                    bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k);

                    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, ImageFormat.Tiff);
                    img.SetAbsolutePosition(0, 0);
                    img.ScaleAbsoluteHeight(document.PageSize.Height);
                    img.ScaleAbsoluteWidth(document.PageSize.Width);
                    cb.AddImage(img);

                    document.NewPage();
                }
                document.Close();

                axAcroPDF1.LoadFile(@"C:\temp\Converted.pdf");
            }
        }
Exemple #2
0
        /// <summary>Converts the Tif files into a multi page PDF multiple.</summary>
        /// <param name="sourceDir">The source dir.</param>
        /// <param name="destinationDir">The destination dir.</param>
        /// <exception cref="System.Exception">Source folder '"+ sourceDir + "' contains no files!!!</exception>
        private static void ConvertMultiple(string sourceDir, string destinationDir)
        {
            try
            {
                DirectoryInfo di = new DirectoryInfo(sourceDir);

                int totalFiles = di.GetFiles().Length;
                if (totalFiles == 0)
                {
                    throw new System.Exception("Source folder '" + sourceDir + "' contains no files!!!");
                }
                Console.WriteLine("Total Files in Source Folder = " + totalFiles);

                foreach (var file in di.GetFiles())
                {
                    totalFiles = totalFiles -= 1;

                    if (file.Extension.ToString() == ".tif" || file.Extension.ToString() == ".tiff")
                    {
                        iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
                        string     fileName                  = Path.GetFileNameWithoutExtension(file.ToString());
                        string     filePath                  = string.Format("{0}\\{1}.{2}", destinationDir, fileName, "pdf");
                        string     sourceFilePath            = string.Format("{0}\\{1}.{2}", sourceDir, fileName, "tif");
                        FileStream fs                        = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
                        iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, fs);

                        // Counts the files remaining to be converting
                        Console.WriteLine("Converting: " + file.Name + " Total Files Left: " + totalFiles);

                        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(sourceFilePath);

                        document.Open();
                        iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;

                        int total = bmp.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
                        for (int k = 0; k < total; ++k)
                        {
                            bmp.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k);
                            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bmp, System.Drawing.Imaging.ImageFormat.Bmp);

                            // Scale the image to fit in the page
                            img.ScalePercent(72f / img.DpiX * 100);
                            img.SetAbsolutePosition(0, 0);
                            cb.AddImage(img);
                            document.NewPage();
                        }

                        bmp.Dispose();
                        writer.Flush();
                        document.Close();
                        document.Dispose();
                    }
                    else
                    {
                        Console.WriteLine("Not Converting: " + file.Name + " Total Files Left: " + totalFiles);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ForegroundColor = ConsoleColor.Gray;
            }
        }