如何通过“ Microsoft打印为PDF”以编程方式将文件和网页打印为PDF?

问题描述 投票:2回答:2

我正在使用C#编写的UWP应用程序,我需要转换为pdf:-办公文件(doc,docx,xl​​s,xlsx,ppt,pptx)- 图片-网页

该应用程序必须能在x86,x64,ARM,ARM64体系结构中工作。

我知道市场上有第三方转换器库,但是只有少数几个在ARM64上下文或UWP中工作。

我的想法是(在Windows 10中使用)“ Microsoft PDF打印机”,该打印机允许用户将大多数文件格式保存为PDF。

我发现很多帖子都问同样的问题,但没有一个确实包含有用的答案。

我找到并测试的代码如下:

PrintDocument doc = new PrintDocument()
{
//DocumentName = safeDir + fileName,
    PrinterSettings = new PrinterSettings()
    {
        // set the printer to 'Microsoft Print to PDF'
        PrinterName = "Microsoft Print to PDF",

        // tell the object this document will print to file
        PrintToFile = true,


        // set the filename to whatever you like (full path)
        PrintFileName = safeDir + fileName,
    }
};
doc.Print();

以上代码生成有效但为空的pdf。我应该如何设置原始文件内容?

例如,如果我有一个名为myreport.docx

的Word文件,是否应该将其转换为字节数组并将其设置在某个位置?

谢谢你。

我正在使用C#编写的UWP应用程序,我需要转换为pdf:-办公文档(doc,docx,xl​​s,xlsx,ppt,pptx)-图像-网页应用程序必须在x86,x64,...

c# pdf uwp converters virtual-printer
2个回答
0
投票

[使用PrintDocument类时,您应按照以下示例将要打印的文件读入文件流:https://docs.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument?view=netframework-4.8


0
投票

在UWP中,我们使用PrintDocument打印PrintDocument。这是官方UIElement。这是代码tutorial

© www.soinside.com 2019 - 2024. All rights reserved.