Bu adım adım öğreticide, birlikte çalışma ve otomasyon olmadan Word belgesinin C# ile nasıl yazdırılacağını ayrıntılı olarak ele alacağız. Bir yazıcı iletişim kutusu kullanarak Word DOCX‘i C# ile yazdırmak için Windows Forms uygulamasını kullanacağız.
Interop Olmadan C# ile Word Belgesi Yazdırma Adımları
- NuGet.org’dan Aspose.Words for .NET paketini yükleyin
- Aspose.Words ve Aspose.Words.Rendering ad alanlarına referans ekleyin
- Aspose.Words for .NET lisansını SetLicense yöntemini kullanarak ayarlayın
- Yeni bir Document nesnesi oluşturun ve DOCX belgesini yükleyin
- Yeni bir PrintDialog nesnesi oluşturun ve yazıcı iletişim özelliklerini ayarlayın
- PrintPreviewDialog nesnesi oluşturun ve AsposeWordsPrintDocument nesnesini ve diğer özellikleri ayarlayın
- Yazıcı iletişim kutusunu etkinleştirmek için olay ekle
- Son olarak, yazıcı iletişim kutusunu kullanıcıya gösterin
Yukarıdaki adımlarda, Word belgesini Belge nesnesine yüklüyoruz, yazıcı iletişim kutusunun özelliklerini ayarlıyoruz ve ardından belgeyi oluşturulan yazıcı iletişim kutusu örneğine geçiriyoruz. Bu adımlar, bir Windows yazıcı iletişim kutusu kullanılarak Word belgesinin C# ile yazdırılmasına yardımcı olur. Yazıcı iletişim kutusu olmadan Word belgesinin nasıl yazdırılacağını göstermek için ayrı bir nasıl yapılır konusu ekleyeceğiz.
Otomasyon Olmadan C# ile Word Belgesi Yazdırma Kodu
using System; | |
using System.Windows.Forms; | |
//Add reference to Aspose.Words for .NET API | |
//Use following namespaces to print word document on a printer | |
using Aspose.Words; | |
using Aspose.Words.Rendering; | |
namespace PrintWordDocumentWithoutInterop | |
{ | |
public partial class PrintWordDocumentWithoutInterop : Form | |
{ | |
public PrintWordDocumentWithoutInterop() | |
{ | |
InitializeComponent(); | |
//Set license before printing word document to printer | |
Aspose.Words.License AsposeWordsLicense = new Aspose.Words.License(); | |
AsposeWordsLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
} | |
private void PrintWordDocumentButton_Click(object sender, EventArgs e) | |
{ | |
//load the Microsoft Word document to print in Windows application | |
Document WordDocumentToPrint = new Document("WordDocumentToPrint.docx"); | |
//create Windows Forms print dialog | |
PrintDialog WindowsPrintDialog = new PrintDialog(); | |
//Set properties of the Windows Print Dialog | |
WindowsPrintDialog.PrinterSettings.MinimumPage = 1; | |
WindowsPrintDialog.PrinterSettings.MaximumPage = WordDocumentToPrint.PageCount; | |
WindowsPrintDialog.PrinterSettings.FromPage = 1; | |
WindowsPrintDialog.PrinterSettings.ToPage = WordDocumentToPrint.PageCount; | |
WindowsPrintDialog.AllowSomePages = true; | |
//Do not proceed if user didn't accept printer settings | |
if (WindowsPrintDialog.ShowDialog() != DialogResult.OK) | |
return; | |
//Create PrintDocument boject and pass printer settings | |
AsposeWordsPrintDocument PrintDocument = new AsposeWordsPrintDocument(WordDocumentToPrint); | |
PrintDocument.PrinterSettings = WindowsPrintDialog.PrinterSettings; | |
//Create print preview dialog object and pass on Word document | |
PrintPreviewDialog WindowsPrintPreviewDialog = new PrintPreviewDialog(); | |
WindowsPrintPreviewDialog.Document = PrintDocument; | |
//Set print preview dialog settings | |
WindowsPrintPreviewDialog.Document.DocumentName = WordDocumentToPrint.OriginalFileName; | |
WindowsPrintPreviewDialog.PrintPreviewControl.Zoom = 1; | |
WindowsPrintPreviewDialog.WindowState = FormWindowState.Maximized; | |
WindowsPrintPreviewDialog.MinimizeBox = true; | |
WindowsPrintPreviewDialog.ShowInTaskbar = true; | |
//Attach even to activate print dialog and bring it to front. | |
WindowsPrintPreviewDialog.Shown += WindowsPrintPreviewDialog_Shown; | |
//Display the print preview dialog so user can print | |
WindowsPrintPreviewDialog.ShowDialog(); | |
} | |
private static void WindowsPrintPreviewDialog_Shown(object sender, EventArgs e) | |
{ | |
//activate print view dialog for printing by bringging to the front | |
((PrintPreviewDialog)sender).Activate(); | |
} | |
} | |
} |
DOCX veya DOC dosyasının kendisini açmadan C# print Word document içindeki yukarıdaki kod. Belgeyi geçirmenin yanı sıra yazdırma iletişim kutusunun özelliklerini ayarlar ve ardından kullanıcının belgeyi yazdırmasına izin verir.