c# - Windows 通用应用程序 (UWP) 中的 HTML 到 PDF

标签 c# pdf uwp

是否可以在 UWP 应用中将 HTML 字符串转换为 PDF 文件? 我已经看到了许多不同的方法可以在常规 .NET 应用程序中完成它(似乎有很多第三方库),但我还没有看到可以在通用/UWP 应用程序中完成它的方法。有谁知道如何做到吗?

如果没有纯代码解决方案,也许有某种方法可以连接到“Microsoft Print to PDF”选项?

或者是否有一种迂回的方式来做到这一点,也许像某种方式使用 Javascript 和 https://github.com/MrRio/jsPDF在 C# UWP 应用程序中?我不确定,捕获救命稻草......

编辑

我已将 Grace Feng - MSFT 提供的解决方案标记为正确的,以证明可以通过使用打印对话框中的 Microsoft 打印到 PDF 选项将 HTML 转换为 PDF。谢谢

最佳答案

Perhaps there is some way to hook into the "Microsoft Print to PDF" option, if there is no pure code solution?

是的,但是使用这种方式,您需要首先在 RichEditBoxTextBlock 等控件中显示 HTML 字符串,只有 UIElement 可以是可打印的内容。

您也可以自己创建PDF文件,以下是PDF中使用的基本语法:

enter image description here

您可以使用BTET来创建段落:

enter image description here

这是 C# 示例:

StringBuilder sb = new StringBuilder();
sb.AppendLine("BT"); // BT = begin text object, with text-units the same as userspace-units
sb.AppendLine("/F0 40 Tf"); // Tf = start using the named font "F0" with size "40"
sb.AppendLine("40 TL"); // TL = set line height to "40"
sb.AppendLine("230.0 400.0 Td"); // Td = position text point at coordinates "230.0", "400.0"
sb.AppendLine("(Hello World)'");
sb.AppendLine("/F2 20 Tf");
sb.AppendLine("20 TL");
sb.AppendLine("0.0 0.2 1.0 rg"); // rg = set fill color to  RGB("0.0", "0.2", "1.0")
sb.AppendLine("(This is StackOverflow)'");
sb.AppendLine("ET");

然后您可以创建一个 PDF 文件并将其保存到该文件中。但由于您想将 HTML 转换为 PDF,这可能是一项艰巨的工作,我认为您不想这样做。

Or is there a roundabout way of doing it, maybe like somehow using Javascript and https://github.com/MrRio/jsPDF inside a C# UWP app? I'm not sure, clutching at straws...

说实话,使用Libs或Web服务将HTML转换为PDF也是一种方法,有很多,我只是搜索了一下,但我找不到任何可以在WinRT中使用的免费方法。所以我认为这里最实用的方法是第一种,连接到 Microsoft Print to PDF。为此,您可以查看官方Printing sample .

更新: 使用@Jerry Nixon - MSFT 的代码 How do I print WebView content in a Windows Store App? ,这是一个很好的样本。我刚刚在 WebViewNavigationCompleted 事件中添加了一些用于添加打印页面的代码:

private async void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
    MyWebViewRectangle.Fill = await GetWebViewBrush(webView);
    MyPrintPages.ItemsSource = await GetWebPages(webView, new Windows.Foundation.Size(842, 595));
}

然后在 printDoc.AddPages += PrintDic_AddPages; 事件中(printDoc 是 PrintDocument 的实例):

private void PrintDic_AddPages(object sender, AddPagesEventArgs e)
{
    foreach (var item in MyPrintPages.Items)
    {
        var rect = item as Rectangle;
        printDoc.AddPage(rect);
    }
    printDoc.AddPagesComplete();
}

其他代码可以引用官方打印样例。

关于c# - Windows 通用应用程序 (UWP) 中的 HTML 到 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39009600/

相关文章:

javascript - pdf.js 查看器错误地存储图像数据

java - Internet Explorer 中的 PDF 导出打印

javascript - Google Chrome - iFrame 中的 PDF 下载不工作

c# - 如何从以指定访问模式运行的 UWP 应用程序关闭 Windows 平板电脑?

ios - Xamarin 表格 : How to remove the blue box in ios when select that page from navigation drawer?

c# - 多次抛出 "Missing compiler required member"错误,几乎没有更改代码

c# - 无论如何使 IList.Contains() 更像通配符包含?

c# - 布娃娃关节角度约束

c# - 我可以在 WPF 中即时创建图标吗?

使用 FileOpenPicker 进行 C# UWP LiveSKD 和 OneDrive 访问