Génération pdf avec iTextSharp
Bonjour mesdames et messieurs,
Je dois créer une petite application qui va, lorsqu'on clic sur un bouton générer un fichier pdf. Pour ce faire, après quelque recherche, j'utilise la librairie iTextSharp.
Voici le code qui nous intéresse:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
...
public Document nouveauDocument = new Document();
...
public Boolean creationPDF()
{
string createur = "Yannick Berthoud";
string societe = "société";
string title = "logiciel";
try
{
PdfWriter.GetInstance(nouveauDocument, new FileStream("client.pdf", FileMode.Create));
nouveauDocument.Open();
nouveauDocument.Add(new Phrase("Hello World"));
nouveauDocument.AddAuthor(createur);
nouveauDocument.AddCreationDate();
nouveauDocument.AddCreator(societe);
nouveauDocument.AddTitle(title);
nouveauDocument.AddTitle("Titre");
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(@"C:\Users\Yannick\Documents\Visual Studio 2008\Projects\connectDB\WindowsFormsApplication2\bin\Debug\5313.gif");
nouveauDocument.Add(logo);
toolStripStatusLabel2.Text = "PDF cr avec succs.";
return true;
}
catch (DocumentException dEx)
{
MessageBox.Show("Erreur: " + dEx.Message + " \nGetType erreur: " + dEx.GetType() + " .");
return false;
}
catch (System.IO.IOException iEx)
{
MessageBox.Show("Erreur: " + iEx.Message + " .");
return false;
}
finally
{
nouveauDocument.Close();
}
}
...
private void btnPDF_Click(object sender, EventArgs e)
{
creationPDF();
} |
Le pdf se cré effectivement. mais il fait 0 ko et est corrompu.
Voici l'erreur:
http://hiboox.com/lang-fr/resultat.p...3.png&error=0#
Il passe au catch lorsqu'il arrive ici:
nouveauDocument.AddAuthor(createur);
L'idéal, serait que je puisse insérer dans mon PDF les information qui sont dans mon datagridview1. qui utilise une select pour afficher les données depuis une base MySQL:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
public Boolean selectClient()
{
try //Procdure de test
{
//Cration de la commande
string selectCommand = "SELECT name, firstName FROM contact;";
maSelectCommand.SelectCommand = new MySqlCommand(selectCommand, ConnectionRessource);
maSelectCommand.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Refresh();
toolStripStatusLabel2.Text = "Liste des contacts charg avec succs"; //Informations a l'utilisateur du succs
return true;
}
catch (MySqlException myEx) //Erreur
{
toolStripStatusLabel2.Text = "Error " + myEx.GetType() + " : Impossible de lister les contacts"; //Information
l'utilisateur de l'erreur
return false;
}
} |
A savoir que mon datagridview affiche avec succès les informations.
Je vous remercie d'avance.
Kiwi