Last active
April 23, 2021 16:14
-
-
Save aspose-com-gists/ff9cec834d5741e5153f0d4abfb128bd to your computer and use it in GitHub Desktop.
C#以编程方式打印PDF文档
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Aspose.Pdf.Facades.PdfViewer pdfv = new Aspose.Pdf.Facades.PdfViewer(); | |
pdfv.PdfQueryPageSettings += PdfvOnPdfQueryPageSettings; | |
System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings(); | |
System.Drawing.Printing.PrinterSettings prin = new System.Drawing.Printing.PrinterSettings(); | |
pdfv.BindPdf(dataDir + "Print-PageRange.pdf"); | |
prin.PrinterName = "HP LaserJet M9050 MFP PCL6"; | |
prin.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169); | |
Aspose.Pdf.Facades.PdfPageEditor pageEditor = new Aspose.Pdf.Facades.PdfPageEditor(); | |
pageEditor.BindPdf(dataDir + "input.pdf"); | |
pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0); | |
pgs.PaperSize = prin.DefaultPageSettings.PaperSize; | |
pdfv.PrintDocumentWithSettings(pgs, prin); | |
pdfv.Close(); | |
private static void PdfvOnPdfQueryPageSettings(object sender, System.Drawing.Printing.QueryPageSettingsEventArgs queryPageSettingsEventArgs, PdfPrintPageInfo currentPageInfo) | |
{ | |
bool isOdd = currentPageInfo.PageNumber % 2 != 0; | |
System.Drawing.Printing.PrinterSettings.PaperSourceCollection paperSources = queryPageSettingsEventArgs.PageSettings.PrinterSettings.PaperSources; | |
if (isOdd) | |
queryPageSettingsEventArgs.PageSettings.PaperSource = paperSources[0]; | |
else | |
queryPageSettingsEventArgs.PageSettings.PaperSource = paperSources[1]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 创建PdfViewer对象 | |
PdfViewer viewer = new PdfViewer(); | |
// 打开输入的PDF文件 | |
viewer.BindPdf(dataDir + "Test.pdf"); | |
// 打印PDF文件 | |
viewer.PrintDocument(); | |
// 关闭PDF文件 | |
viewer.Close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 实例化PdfViewer对象 | |
PdfViewer viewer = new PdfViewer(); | |
// 绑定源PDF文件 | |
viewer.BindPdf(dataDir + "Sample Document with Bookmark.pdf"); | |
viewer.AutoResize = true; | |
// 隐藏打印对话框 | |
viewer.PrintPageDialog = false; | |
// 创建打印机设置对象 | |
System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings(); | |
System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings(); | |
System.Drawing.Printing.PrintDocument prtdoc = new System.Drawing.Printing.PrintDocument(); | |
// 指定打印机名称 | |
//ps.PrinterName = "Microsoft XPS Document Writer"; | |
ps.PrinterName = "Microsoft Print to PDF"; | |
// 结果打印输出名称 | |
//ps.PrintFileName = "ResultantPrintout.xps"; | |
ps.PrintFileName = "ResultantPrintout.pdf"; | |
// 将输出打印到文件 | |
ps.PrintToFile = true; | |
ps.FromPage = 1; | |
ps.ToPage = 2; | |
ps.PrintRange = System.Drawing.Printing.PrintRange.SomePages; | |
// 指定打印输出的页面大小 | |
pgs.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169); | |
ps.DefaultPageSettings.PaperSize = pgs.PaperSize; | |
pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0); | |
// 使用上面指定的设置打印文档 | |
viewer.PrintDocumentWithSettings(pgs, ps); | |
// 检查打印状态 | |
if (viewer.PrintStatus != null) | |
{ | |
// 引发异常 | |
Exception ex = viewer.PrintStatus as Exception; | |
if (ex != null) | |
{ | |
// Get exception message | |
} | |
} | |
else | |
{ | |
// 找不到错误。 打印作业已成功完成 | |
Console.WriteLine("printing completed without any issue.."); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var files = new List<string>(); | |
files.Add(dataDir + "First.pdf"); | |
files.Add(dataDir + "Second.pdf"); | |
foreach (String file in files) | |
{ | |
// 创建PdfViewer对象 | |
PdfViewer viewer = new PdfViewer(); | |
// 打开输入的PDF文件 | |
viewer.BindPdf(file); | |
// 打印PDF文件 | |
viewer.PrintDocument(); | |
// 关闭PDF文件 | |
viewer.Close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 在指定用户或所有者密码的同时加载安全的PDF文档 | |
Document document = new Document(dataDir + "Password.pdf" , "userORowner"); | |
// 创建PdfViewer对象 | |
PdfViewer viewer = new PdfViewer(); | |
// 打开输入的PDF文件 | |
viewer.BindPdf(document); | |
// 打印PDF文件 | |
viewer.PrintDocument(); | |
// 关闭PDF文件 | |
viewer.Close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string inPdf = dataDir + "Test.pdf"; | |
string output = dataDir; | |
IList<PrintingJobSettings> printingJobs = new List<PrintingJobSettings>(); | |
PrintingJobSettings printingJob1 = new PrintingJobSettings(); | |
printingJob1.FromPage = 2; | |
printingJob1.ToPage = 3; | |
printingJobs.Add(printingJob1); | |
PrintingJobSettings printingJob2 = new PrintingJobSettings(); | |
printingJob2.FromPage = 5; | |
printingJob2.ToPage = 7; | |
printingJobs.Add(printingJob2); | |
{ | |
for (var printingJobIndex = 0; printingJobIndex < printingJobs.Count; printingJobIndex++) | |
{ | |
System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings(); | |
System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings(); | |
ps.PrinterName = "Microsoft Print to PDF"; | |
ps.PrintRange = System.Drawing.Printing.PrintRange.SomePages; | |
ps.FromPage = printingJobs[printingJobIndex].FromPage; | |
ps.ToPage = printingJobs[printingJobIndex].ToPage; | |
System.Console.WriteLine(ps.FromPage); | |
System.Console.WriteLine(ps.ToPage); | |
System.Console.WriteLine(printingJobIndex); | |
using (var theViewer = new Aspose.Pdf.Facades.PdfViewer()) | |
{ | |
// Document printing code goes here | |
// Print document using printer and page settings | |
theViewer.BindPdf(inPdf); | |
theViewer.AutoResize = true; | |
theViewer.AutoRotate = true; | |
theViewer.PrintPageDialog = false; | |
theViewer.PrintDocumentWithSettings(pgs, ps); | |
theViewer.Close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment