Pdf worde çevir , WORD TO PDF C#, how convert word to pdf ,c# code word to pdf

Merhaba,
Bu yazımızda sıkça ihtiyaç duyduğumuz word dokümanın pdf’e çevirme hadisesini kendi imkanlarımızla(3. bir uygulama kullanmadan ) nasıl yaparız , işleyeceğiz.
Öncelikle .Net geliştirme ortamımız olan Visual Studiomuzu açalım yeni proje oluşturalım.
- Projemizin ismini ve kayıt yerini verdikten sonra oluşturuyoruz.
- Ardından Xaml porjemize bir adet grid ,2 adet text box 2 adet buton atıyoruz.
Kod;
Convert etmek istediğimiz word dosyasını seçmek için ilgili butonun click eventine
private void Goruntule_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = “.docx”;
dlg.Filter = “Word documents (.doc)|*.docx;*.doc”;
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
SecilecekDosya.Text = filename;
}
- Convert işlemini yapacak butonun click eventine
private void ConverButon_Click(object sender, RoutedEventArgs e)
{
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
wordDocument = appWord.Documents.Open(SecilecekDosya.Text);
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = “PDF Documents|*.pdf”;
try
{
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string ext = System.IO.Path.GetExtension(sfd.FileName);
switch (ext)
{
case “.pdf”:
wordDocument.ExportAsFixedFormat(sfd.FileName, WdExportFormat.wdExportFormatPDF);
break;
case “.docx”:
wordDocument.ExportAsFixedFormat(sfd.FileName, WdExportFormat.wdExportFormatPDF);
break;
}
KaydedilecekYer.Text = sfd.FileName;
}
System.Windows.Forms.MessageBox.Show(“Oğuz ALTUNTAŞ © 2017”);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
System.Diagnostics.Process.Start(sfd.FileName);
}
ve wordDocument entity için ;
public Microsoft.Office.Interop.Word.Document wordDocument { get; set; }
yazıp projemizi derleyelim muhtemelen
Microsoft.Office.Interop.Word.Document referans isteyecektir.
Package Manager dan (Nuget)
Install-Package Microsoft.Office.Interop.Word -Version 15.0.4797.1003 komutu yazarsanız referans otomatik indirip ekleyecektir.
İlgili solution’ı github adresim üzerinden; https://github.com/oguzaltuntas/wordtopdf ulaşabilirsiniz.
Oğuz ALTUNTAŞ © 2017