以编程方式提供文件路径作为“Microsoft Print to PDF"打印机的输入文件 [英] Programmatically provide a filepath as input file for “Microsoft Print to PDF” printer

查看:62
本文介绍了以编程方式提供文件路径作为“Microsoft Print to PDF"打印机的输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用默认安装的 Windows 10 打印机Microsoft Print to PDF"将文件打印到新的 PDF.

I want to print a file to a new PDF using the Windows 10 printer "Microsoft Print to PDF" which is installed by default.

当您选择此打印机作为默认打印机并使用文件的上下文菜单并选择打印 时,它只要求保存目录和名称.之后,它会立即转换为 PDF 并保存文件.

When you select this printer as your default printer and use your context menu on a file and select Print, it only asks for a save directory and name. After that, it immediately converts to PDF and saves the file.

只要安装了 MS Office,它就适用于 Word、Excel、PowerPoint 文件类型.但也适用于常见的图像类型和普通文本文件.

As long as MS Office is installed, this works for Word, Excel, PowerPoint file types. But also for common image types and normal text files.

我想通过提供默认路径来自动执行此操作.

I'd like to automate this by providing a default path.

Stackoverflow 已经有 this related question,但它没有解决我的具体问题,而且相当不完整且不起作用.

Stackoverflow already has this related question, but it does not address my specific problem and is rather incomplete and not working.

但是我想出了这个 C# 控制台程序,它使用 PDF 打印机在我的桌面上生成一个新的 PDF,其中Hello World"作为字符串

But I came up with this C# console program which uses the PDF printer to produce a new PDF on my desktop with "Hello World" as string

namespace PrintToPdf_Win10
{
    using System;
    using System.Drawing;
    using System.Drawing.Printing;

    class Program
    {
        public static void Main(string[] args)
        {
            PrintDocument printDoc = new PrintDocument
            {
                PrinterSettings = new PrinterSettings
                {
                    PrinterName = "Microsoft Print to PDF",
                    PrintToFile = true,
                    PrintFileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/test.pdf"
                }
            };

            printDoc.PrintPage += printDoc_PrintPage;
            printDoc.Print();
            Console.ReadKey(true);
        }

        static void printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawString("Hello World", new Font("Arial", 12), Brushes.Black, 50, 50);
        }
    }
}

问题

我如何将内容(比如说一个 Word 文件)设置为我的 printDoc 对象的输入?

有没有一种通用的方法来设置 printDoc 只为我想要打印的文件提供一个 filePath?或者我是否必须为每个可能的文件类型系列创建一个自定义函数,例如:

Is there a generic way to set printDoc by providing only a filePath to my file I want to print from? Or do I have to create a custom function for each possible filetype families like:

  • Office 文件类型(doc、docx、xls、xlsx、xlsm、ppt、pptx 等)
  • 图像文件类型(png、bmp、jpg)
  • 文本文件(txt、rtf、ini)

推荐答案

这里是如何打印图像或文本的简单解决方案(它可以帮助您处理 png、bmp、jpg、txt、ini 等格式)

Here is simple solution how to print image or text (it could help you with formats like png, bmp, jpg, txt, ini)

       private static StreamReader streamToPrint;

    static void Main(string[] args)
    {
        string printFormat;
        printFormat = "txt";

        try
        {
            streamToPrint = new StreamReader(@"D:TestText.txt");

            PrintDocument printDoc = new PrintDocument
            {
                PrinterSettings = new PrinterSettings
                {
                    PrinterName = "Microsoft Print to PDF",
                    PrintToFile = true,
                    PrintFileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/test.pdf"
                }
            };

            printDoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 210, 290);
            printDoc.PrinterSettings.DefaultPageSettings.Landscape = false;
            printDoc.PrinterSettings.DefaultPageSettings.Margins.Top = 0;
            printDoc.PrinterSettings.DefaultPageSettings.Margins.Left = 0;

            switch (printFormat)
            {
                case "jpg":
                    printDoc.PrintPage += printDoc_PrintImage;
                    break;
                case "txt":
                    printDoc.PrintPage += printDoc_PrintText;
                    break;
                default:
                    break;
            }
            printDoc.Print();


        }
        finally
        {
            streamToPrint.Close();
        }

        Console.ReadKey(true);

    }

    static void printDoc_PrintImage(object sender, PrintPageEventArgs e)
    {
        Image photo = Image.FromFile(@"D:TestImage.jpg");
        Point pPoint = new Point(0, 0);
        e.Graphics.DrawImage(photo, pPoint);
    }

    static void printDoc_PrintText(object sender, PrintPageEventArgs e)
    {

        Font printFont;
        printFont = new Font("Arial", 10);

        float linesPerPage = 0;
        // Calculate the number of lines per page.
        linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);

        float yPos = 0;
        int count = 0;
        float leftMargin = e.MarginBounds.Left;
        float topMargin = e.MarginBounds.Top;

        string line = null;

        while (count < linesPerPage &&
      ((line = streamToPrint.ReadLine()) != null))
        {
            yPos = topMargin + (count *
               printFont.GetHeight(e.Graphics));
            e.Graphics.DrawString(line, printFont, Brushes.Black,
               leftMargin, yPos, new StringFormat());
            count++;
        }

        // If more lines exist, print another page.
        if (line != null)
            e.HasMorePages = true;
        else
            e.HasMorePages = false;
    }

如您所知 docx,xl​​sx 就像 zip 文件,您可以解压缩并以 xml 格式获取内容.所以,如果你想打印它们,这是很多工作

As you know docx, xlsx are like zip files and you can unzip and get content as xml. So, it's much work to to if you want to print them

这篇关于以编程方式提供文件路径作为“Microsoft Print to PDF"打印机的输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆