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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsAppDevis
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
generatePdf();
}
public void generatePdf()
{
string outfile = Environment.CurrentDirectory + "/devis.pdf";
// Création du document
Document doc = new Document();
PdfWriter.GetInstance(doc, new FileStream(outfile, FileMode.Create));
doc.Open();
// Palette de couleurs
BaseColor blue = new BaseColor(0, 75, 155);
BaseColor gris = new BaseColor(240, 240, 240);
BaseColor blanc = new BaseColor(255, 255, 255);
// Polices d'écriture
Font policeTitre = new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 20f, iTextSharp.text.Font.BOLD, blue);
Font policeTh = new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 16f, 1, blanc);
// Page Pdf
// Création de paragraphes
Paragraph p1 = new Paragraph("devis-test, " + mesInfos.Text + "\n\n", policeTitre);
p1.Alignment = Element.ALIGN_LEFT;
doc.Add(p1);
Paragraph p2 = new Paragraph("Le client, " + client.Text + "\n\n", policeTitre);
p2.Alignment = Element.ALIGN_RIGHT;
doc.Add(p2);
Paragraph p3 = new Paragraph("Devis : " + titre.Text + "\n\n", policeTitre);
p3.Alignment = Element.ALIGN_LEFT;
doc.Add(p3);
// Création du tableau des produits
PdfPTable tableau = new PdfPTable(3);
tableau.WidthPercentage = 100;
// Cel du tableau
AddCellToTab("Désignation", policeTh, blue, tableau);
AddCellToTab("Quantité", policeTh, blue, tableau);
AddCellToTab("Prix", policeTh, blue, tableau);
// Lister les produits dans le tableau
string[] infosProduit = new string[3];
infosProduit[0] = nom.Text;
infosProduit[1] = qte.Text;
infosProduit[2] = prix.Text;
foreach(string info in infosProduit)
{
PdfPCell cell = new PdfPCell(new Phrase(info));
cell.BackgroundColor = gris;
cell.Padding = 7;
cell.BorderColor = gris;
tableau.AddCell(cell);
}
doc.Add(tableau);
doc.Add(new Phrase("\n"));
int prixTotal = int.Parse(prix.Text) * int.Parse(qte.Text);
Paragraph p4 = new Paragraph("Total = " + prixTotal + "\n\n", policeTitre);
p4.Alignment = Element.ALIGN_RIGHT;
doc.Add(p4);
// Fermer le document
doc.Close();
Process.Start(@"cmd.exe ", @"/c " + outfile);
}
public void AddCellToTab(string str, Font f, BaseColor c, PdfPTable t)
{
PdfPCell cell1 = new PdfPCell(new Phrase(str, f));
cell1.BackgroundColor = c;
cell1.Padding = 7;
cell1.BorderColor = c;
// cell1.HorizontalAlignment = Element.ALIGN_CENTER;
t.AddCell(cell1);
}
}
} |