Aspose.pdf 如何将pdf文件打印出来,感谢查看。


#1

源文件:
PDFLJ_1.PDF (16.7 KB)
PDFLJ_2.PDF (17.6 KB)
PDFLJ_3.PDF (20.4 KB)

打印机名字可以自己选择,打印A4纸大小,请技术大牛提供源代码,谢谢您。


#2

@fhn123456

感谢您与支持人员联系。

您可以使用.NET框架的System.Drawing.Printing.PageSettings 类。 然后,您可以将打印机和页面设置传递给 PdfViewer 类的 PrintDocumentWithSettings 方法。 以下是您的类型参考的代码段。

Document doc = new Document("Test.pdf");
PdfViewer viewer = new PdfViewer();
viewer.BindPdf(doc);
viewer.PrinterJobName = System.IO.Path.GetFileName(doc.FileName);
viewer.Resolution = 110;
// Set attributes for printing
viewer.AutoResize = true; // Print the file with adjusted size
viewer.AutoRotate = false; // Print the file with adjusted rotation
viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing
viewer.RenderingOptions.UseNewImagingEngine = true;
// Create objects for printer and page settings and PrintDocument
System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings();
// Set printer name
ps.PrinterName = "Microsoft Print to PDF";
pgs.PaperSize = new System.Drawing.Printing.PaperSize(paperTypeName, paperWidth, paperHeight);
pgs.Margins = new System.Drawing.Printing.Margins(margins.Left, margins.Right, margins.Top, margins.Bottom);
pgs.PaperSource =  GetPaperSource(printerName, trayName);
// Print document using printer and page settings
viewer.PrintDocumentWithSettings(pgs, ps);
    /// <summary>
    /// Return the PaperSource object for the provided printer and tray name.
    /// </summary>
    /// <param name="printerName"></param>
    /// <param name="trayName"></param>
    /// <returns></returns>
    public static System.Drawing.Printing.PaperSource GetPaperSource(string printerName, string trayName)
    {
        System.Drawing.Printing.PaperSource ps = null;
        System.Drawing.Printing.PrintDocument prtDoc = new System.Drawing.Printing.PrintDocument();
        prtDoc.PrinterSettings.PrinterName = printerName;
        for (int i = 0; i < prtDoc.PrinterSettings.PaperSources.Count; i++)
        {
            if (prtDoc.PrinterSettings.PaperSources[i].SourceName.ToLower().Equals(trayName.ToLower()))
            {
                ps = prtDoc.PrinterSettings.PaperSources[i];
                break;
            }
        }
        return ps;
    }

如果您需要任何进一步的帮助,请随时与我们联系。