Resim’i Pdf’ye Aktar — Csharp
Resim’i Pdf’ye Aktar — Csharp
Not: Pdf yi C anadizini içine gorsel.pdf olarak oluşturuyor.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using iTextSharp.text.pdf; using iTextSharp.text; using System.IO; namespace pdf_aktar { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnPdfAktar_Click(object sender, EventArgs e) { iTextSharp.text.Document document = new iTextSharp.text.Document(); PdfWriter.GetInstance(document, new FileStream(@"C:\gorsel.pdf", FileMode.Create)); if (document.IsOpen() == false) { document.Open(); string text = "www.gorselprogramlama.com \n www.gorselprogramlama.com "; ; document.Add(new Paragraph(text)); iTextSharp.text.Image resim = iTextSharp.text.Image.GetInstance(@"C:\a.jpg"); document.Add(resim); document.Close(); } }//www.gorselprogramlama.com } }
Usta pdfte resmin yarısını gösteriyor.
fatih : problem yok çalışıyor
resime nasıl align verebilirim? pdf’nin ortasında durmasını istiyorum
hocam resim ve yazı arasında nasıl boşluk bırakabilirim yardımcı olursanız çok sevinirim
furkan:
Resim ortalama ve boşluk işlemi için sayfa genişliği kadar tablo oluşturulup tablonun cellspacing ve cellpadding özellikleri gerekli ortalama ve boşluk işlemleri uygulanabilir.Tablo ekle için aşağıdaki kodları inceleyebilirsin.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;
iTextSharp.text.Document pdfDosya = new iTextSharp.text.Document();
//PDF dosyamızı temsil edecek nesnemizi oluşturuyoruz
PdfWriter.GetInstance(pdfDosya, new FileStream(“C:CSharpPDF.pdf”,FileMode.Create));
//PDF dosyamızın yolu ” C:CSharpPDF.pdf” ve dosyanın açılış biçimi ‘Yeni yarat’
pdfDosya.Open();
pdfDosya.AddCreator(textBox2.Text); //Oluşturan kişinin isminin eklenmesi
pdfDosya.AddCreationDate();//Oluşturulma tarihinin eklenmesi
pdfDosya.AddAuthor(textBox3.Text); //Yazarın isiminin eklenmesi
pdfDosya.AddHeader(textBox4.Text, “PDF UYGULAMASI OLUSTUR”);
pdfDosya.AddTitle(textBox5.Text); //Başlık ve title eklenmesi
Paragraph eklenecekMetin = new Paragraph(textBox6.Text);
pdfDosya.Add(eklenecekMetin); //Eklenecek metinimizin Dosyaya eklenmesi
decimal satir = numericUpDown1.Value;
decimal sutun = numericUpDown2.Value; //Satır ve sütun bilgilerinin alınması
iTextSharp.text.Table tablo = new Table((int)sutun, (int)satir);
//Alınmış olan sütun ve satır sayılarına uygun tablo nesnemizin yaratılması
for (int i = 0; i < satir; i++) { for (int j = 0; j < sutun; j++) { Cell hucre = new Cell((i+1).ToString() + " " + (j+1).ToString()); //Tablomıza eklenecek hücrelerin ve içlerindeki yazının oluşturlması hucre.BackgroundColor = iTextSharp.text.Color.RED; //Hücrelerimizin arkaplan rengini kırmızı olarak ayarlanması tablo.AddCell(hucre, i, j); //Hücremizin tablomuza eklenmesi } } tablo.Cellspacing = 5; //Tabloda bulunan hücrelerin mesafelerinin ayarlanması pdfDosya.Add(tablo); //Son olarak Oluşturduğumuz tablomuzun dosyamıza eklenmesi if (textBox1.Text != "") { Uri yol = new Uri(textBox1.Text); //Eklenecek resmimizin yolunun Uri tipinde nesne şeklinde elde edilmesi iTextSharp.text.Jpeg resim = new iTextSharp.text.Jpeg(yol); //resim nesnemizin Oluşturulması resim.ScalePercent((int)numericUpDown3.Value); //Resim nesnemizin ölçeklendirilmesi pdfDosya.Add(resim); //Resim Nesnemizi Dosyaya eklenmesi } pdfDosya.Close();