Word dokumentum nyomtatása C#-ban

Ebben a lépésről lépésre bemutatott oktatóanyagban bemutatjuk, hogyan nyomtathatunk Word-dokumentumot C# nyelven interop és automatizálás nélkül. A Windows Forms alkalmazást használjuk a Word DOCX C# nyelven történő nyomtatásához egy nyomtató párbeszédpanel segítségével.

Lépések a Word-dokumentum nyomtatásához C#-ban, együttműködés nélkül

  1. Telepítse a Aspose.Words for .NET csomagot a NuGet.org webhelyről
  2. Adjon hozzá hivatkozást a Aspose.Words és a Aspose.Words.Rendering névterekhez
  3. Állítsa be az Aspose.Words for .NET licencét a SetLicense metódussal
  4. Hozzon létre egy új Document objektumot, és töltse be a DOCX dokumentumot
  5. Hozzon létre egy új PrintDialog objektumot, és állítsa be a nyomtató párbeszédpanel tulajdonságait
  6. Hozzon létre PrintPreviewDialog objektumot, és állítsa be a AsposeWordsPrintDocument objektumot és egyéb tulajdonságokat
  7. Csatoljon eseményt a nyomtató párbeszédpaneljének aktiválásához
  8. Végül mutasd meg a nyomtató párbeszédpanelt a felhasználónak

A fenti lépésekben betöltjük a Word dokumentumot a Dokumentum objektumba, beállítjuk a nyomtató párbeszédpanel tulajdonságait, majd átadjuk a dokumentumot a létrehozott nyomtató párbeszédablak példánynak. Ezek a lépések segítenek Word-dokumentumot nyomtatni C# nyelven a Windows nyomtató párbeszédpanel segítségével. Hozzáadunk egy külön útmutatót, amely bemutatja, hogyan nyomtathat Word-dokumentumot nyomtató párbeszédpanel nélkül.

Kód Word-dokumentum nyomtatásához C#-ban automatizálás nélkül

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();
}
}
}

A fenti kód C#-ban print Word document anélkül, hogy megnyitná magát a DOCX- vagy DOC-fájlt. Beállítja a nyomtatási párbeszédpanel tulajdonságait, magát a dokumentumot, majd hagyja, hogy a felhasználó kinyomtassa a dokumentumot.

 Magyar