Convertisseur pdf

Description

Ce code se sert de la librairie ItextSharp pour creer, des docuemnts PDF.
Dans ce code, je creé des document PDF des images présente dans un répertoir. Apres vous pouvez faire tout ce que vous voulez !!! (Insertion des textes, modification de la police etc...)

Source / Exemple :


using System;
using System.Windows.Forms;
using System.IO;

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

namespace PDFConvertor
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog;
		private System.Windows.Forms.TextBox browseTxt;
		private System.Windows.Forms.Button browseBtn;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button convertBtn;
		public string folderName = string.Empty;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.convertBtn = new System.Windows.Forms.Button();
			this.folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
			this.browseTxt = new System.Windows.Forms.TextBox();
			this.browseBtn = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// convertBtn
			// 
			this.convertBtn.Enabled = false;
			this.convertBtn.Location = new System.Drawing.Point(256, 40);
			this.convertBtn.Name = "convertBtn";
			this.convertBtn.Size = new System.Drawing.Size(80, 24);
			this.convertBtn.TabIndex = 0;
			this.convertBtn.Text = "Convert";
			this.convertBtn.Click += new System.EventHandler(this.convertBtn_Click);
			// 
			// browseTxt
			// 
			this.browseTxt.Location = new System.Drawing.Point(8, 8);
			this.browseTxt.Name = "browseTxt";
			this.browseTxt.Size = new System.Drawing.Size(264, 20);
			this.browseTxt.TabIndex = 1;
			this.browseTxt.Text = "";
			// 
			// browseBtn
			// 
			this.browseBtn.Location = new System.Drawing.Point(280, 8);
			this.browseBtn.Name = "browseBtn";
			this.browseBtn.Size = new System.Drawing.Size(56, 24);
			this.browseBtn.TabIndex = 2;
			this.browseBtn.Text = "Browse";
			this.browseBtn.Click += new System.EventHandler(this.browseBtn_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(344, 72);
			this.Controls.Add(this.browseBtn);
			this.Controls.Add(this.browseTxt);
			this.Controls.Add(this.convertBtn);
			this.Name = "Form1";
			this.Text = "PDF Convertor";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void browseBtn_Click(object sender, System.EventArgs e)
		{
			if(this.folderBrowserDialog.ShowDialog()== DialogResult.OK){				
				folderName = folderBrowserDialog.SelectedPath;
				browseTxt.Text = folderName;
				convertBtn.Enabled = true;
			}
		}

		private void convertBtn_Click(object sender, System.EventArgs e)
		{			
			string filename = "image2pdf.pdf";	
			Image imageFile = null;			
			Document pdfDoc = new Document();
			PdfWriter.GetInstance(pdfDoc, new System.IO.FileStream(filename, System.IO.FileMode.Create));
			pdfDoc.AddTitle("PDF Convertor");			
			pdfDoc.Open();
			try
			{
				foreach(string imgFile in Directory.GetFiles(folderName, "*.*"))
				{					
					imageFile = Image.GetInstance(imgFile);
					imageFile.Alignment = Image.MIDDLE_ALIGN;
					imageFile.ScalePercent(80);					
					pdfDoc.Add(imageFile);
					pdfDoc.NewPage();
					imageFile = null;
				}								
				MessageBox.Show("Process terminated. ","PDF Convertor",MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			catch(Exception ex)
			{				
				MessageBox.Show("Image File is not supported !! \n"+ex.ToString(),"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			} 
			finally{
				pdfDoc.Close();				
				System.GC.Collect();
			}
		}
	}
}

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.