Maison >développement back-end >C++ >Comment automatiser l'impression PDF à partir d'applications .NET ?

Comment automatiser l'impression PDF à partir d'applications .NET ?

Susan Sarandon
Susan Sarandonoriginal
2025-01-23 22:17:12432parcourir

How to Automate PDF Printing from .NET Applications?

Comment automatiser l'impression de documents PDF

L'automatisation de l'impression de documents PDF est une tâche courante dans diverses applications. Dans cet article, nous explorons comment envoyer efficacement des fichiers PDF à la file d'attente de l'imprimante et les imprimer directement à partir de votre code.

Approche .NET Windows Forms

Pour une Application Windows Forms .NET 4 exécutée sur Windows XP, une approche consiste à utiliser une ligne de commande pour imprimer le fichier PDF. Voici comment mettre en œuvre cela :

using System.Diagnostics;
using System.IO;

public void SendToPrinter(string filePath)
{
    // Convert full file path to short path for command line use
    string shortPath = Path.GetShortPathName(filePath);

    // Prepare the command line arguments
    string args = $"/p \"{shortPath}\"";

    // Create a new Process object
    Process process = new Process();
    process.StartInfo.FileName = "cmd.exe";
    process.StartInfo.Arguments = "/C start " + args;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = true;

    // Execute the command line
    process.Start();
}

Solution améliorée avec PdfiumViewer

Une autre option, particulièrement adaptée à l'impression PDF, consiste à exploiter la bibliothèque Google Pdfium et son . NET, PdfiumViewer. Cette bibliothèque open source offre une capacité d'impression PDF robuste :

using PdfiumViewer;

public bool PrintPDF(
    string printer,
    string paperName,
    string filePath,
    int copies)
{
    try {
        // Create printer settings
        var printerSettings = new PrinterSettings {
            PrinterName = printer,
            Copies = (short)copies,
        };

        // Create page settings for paper size
        var pageSettings = new PageSettings(printerSettings) {
            Margins = new Margins(0, 0, 0, 0),
        };
        foreach (PaperSize paperSize in printerSettings.PaperSizes) {
            if (paperSize.PaperName == paperName) {
                pageSettings.PaperSize = paperSize;
                break;
            }
        }

        // Print PDF document
        using (var document = PdfDocument.Load(filePath)) {
            using (var printDocument = document.CreatePrintDocument()) {
                printDocument.PrinterSettings = printerSettings;
                printDocument.DefaultPageSettings = pageSettings;
                printDocument.PrintController = new StandardPrintController();
                printDocument.Print();
            }
        }
        return true;
    } catch {
        return false;
    }
}

Cette approche améliorée offre plus de contrôle sur le processus d'impression et permet une impression silencieuse sans interaction de l'utilisateur.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn