【问题标题】:Print to file in C# using Print Document使用打印文档在 C# 中打印到文件
【发布时间】:2016-12-16 10:54:13
【问题描述】:

我正在创建 C# 应用程序,我在其中使用外部类打印 dataGridView。通过这门课,我可以打印文档 pdf,但我无法将文件保存到磁盘。我知道这可以通过printDocument1.PrinterSettings.PrintToFile=trueprintDocument1.PrinterSettings.PrintFileName = path 来实现,但正如我提到的,我正在使用外部类,我无法做到这一点。我正在发布用于打印网格的代码,并且还发布了指向外部类的链接。请帮助我如何根据需要进行调整

DGVPrinter printer = new DGVPrinter();
        //if (tbReportSchoolName.Text == string.Empty)
        //{ 
            printer.Title = "The Punjab School"; 
        //}
        //else { printer.Title = tbReportSchoolName.Text.Trim(); } //school name
        //if (tbReportHeader.Text == string.Empty)
        //{ 
            printer.SubTitle = "Absent Student Report | " + dtpTo.Value.ToShortDateString(); 
        //}
        //else { printer.SubTitle = tbReportHeader.Text.Trim() + "|" + dtpTo.Value.ToShortDateString(); }


        printer.SubTitleFormatFlags = StringFormatFlags.LineLimit | StringFormatFlags.NoClip;

        printer.PageNumbers = true;
        printer.PageNumberInHeader = false;
        printer.PorportionalColumns = true;
        printer.HeaderCellAlignment = StringAlignment.Near;
        //if (tbReportFooter.Text == string.Empty)
        //{ 
            printer.Footer = "Allied School, A Project of Punjab Group of Colleges."; 
        //}
        //else { printer.Footer = tbReportFooter.Text.Trim(); }
        printer.FooterSpacing = 15;
        printer.PrintDataGridView(reportGrid);

类名是 DGVPrinter,网格名是 dgv 课程链接:

https://drive.google.com/file/d/0BwBxpfm3kmmgci1aSnZ6T0RudGc/view

【问题讨论】:

  • 可以安装pdf打印机。
  • 感谢@lordkain 的回复,但我正在创建一个自动化应用程序。这样会自动将文件保存在磁盘上,无需用户点击
  • 我只需要帮助粘贴这 2 行代码,我不知道在课堂上我必须在哪里发布来解决我的问题

标签: c# printing printdocument


【解决方案1】:

我没有看到在 DGVPrinter 类中编写了任何代码来打印到 PDF 或文件。它具有 AllowPrintToFile 属性,但在类中没有使用它。您应该通过扩展此类自己编写它,或者您可以尝试其他插件,如下所示:

How to export PDF from data of datagrid?

http://www.aspsnippets.com/Articles/Export-Windows-Forms-DataGridView-to-PDF-using-iTextSharp-C-and-VBNet.aspx

【讨论】:

  • 是的,没有用于 pdf 文件的代码,但有用于使用 printDocument 类打印的代码,并且您知道 printDocument 具有名为 printDocument.PrintToFile 的属性,可将文件保存在磁盘中。请帮我使用该属性
  • 好的,我进一步阅读了 MSDN 文档,发现不可能以编程方式执行此操作。请参考以下链接:msdn.microsoft.com/en-us/library/… PrintToFile 属性仅供 PrintDialog 使用,不能以编程方式设置。打印到文件选项仅在 PrintDialog.AllowPrintToFile 属性设置为 true 时出现在 PrintDialog 上。
猜你喜欢
  • 2015-08-07
  • 2016-10-09
  • 2012-07-27
  • 1970-01-01
  • 2014-11-21
  • 2020-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多