简体   繁体   English

PrintDocument 正在打印文件名字符串而不是 C# 中的文档

[英]PrintDocument is printing filename string instead of document in c#

I am implementing PrintDocument in my asp.net project wherein i need to print specific pages of my document but instead of document being printed, its the string of "fileName" which is getting printed.我正在我的 asp.net 项目中实现PrintDocument ,其中我需要打印文档的特定页面,但不是打印文档,而是打印正在打印的“fileName”字符串。 Below is my code下面是我的代码

 string fileName = "C:\\DocToPrint\\Sample2018.pdf";

 protected void Page_Load(object sender, EventArgs e)
    {
      using (PrintDocument pd = new PrintDocument())
        {
            pd.PrinterSettings.FromPage = 1;
            pd.PrinterSettings.ToPage = 1;
            pd.PrinterSettings.PrintRange = PrintRange.SomePages;
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            pd.Print();
        }

 public void pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        ev.Graphics.DrawString(fileName, new Font("Arial", 10), Brushes.Black,
                             ev.MarginBounds.Left, 0, new StringFormat());
    }
    }

What is being missed here?这里错过了什么? Please suggest..Thanks请建议..谢谢

There's a nuget package called Spire.Pdf that's very simple to use.有一个名为 Spire.Pdf 的 nuget 包,使用起来非常简单。 The free version has a limit of 10 pages although, however, in my case it was the best solution once I don't want to depend on Adobe Reader and I don't want to install any other components.免费版本有 10 页的限制,但是,在我的情况下,当我不想依赖 Adob​​e Reader 并且不想安装任何其他组件时,它是最好的解决方案。

https://www.nuget.org/packages/Spire.PDF/ https://www.nuget.org/packages/Spire.PDF/

PdfDocument pdfdocument = new PdfDocument();
pdfdocument.LoadFromFile(pdfPathAndFileName);
pdfdocument.PrinterName = "My Printer";
pdfdocument.PrintDocument.PrinterSettings.Copies = 1;
pdfdocument.PrintFromPage = index;
pdfdocument.PrintToPage = index;
pdfdocument.PrintDocument.Print();
pdfdocument.Dispose();

Sample for printing some page.打印某些页面的示例。

https://www.e-iceblue.com/forum/print-one-page-from-pdf-t6586.html https://www.e-iceblue.com/forum/print-one-page-from-pdf-t6586.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM