【问题标题】:Microsoft Print to PDF not working when used comma(,) in file name在文件名中使用逗号 (,) 时,Microsoft Print to PDF 不起作用
【发布时间】:2018-02-23 12:10:01
【问题描述】:
    public void RenderCanvasImage(int maxRight, int maxBottom, Canvas surface, Transform transform)
{
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap(maxRight, maxBottom, 96d, 96d, PixelFormats.Pbgra32);

            renderBitmap.Render(surface);

            PrintDialog dialog = new PrintDialog();

            if (dialog.ShowDialog() != true)
            {
                return;
            }

            Grid grid = new Grid();
            Image myImage = new Image();
            myImage.Source = renderBitmap;
            myImage.Stretch = Stretch.Uniform;
            grid.Children.Add(myImage);
            Grid.SetColumn(myImage, 0);
            Grid.SetRow(myImage, 1);

            grid.Measure(new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight));
            grid.Arrange(new Rect(new Point(0, 0), grid.DesiredSize));
            dialog.PrintVisual(grid, "My Canvas");
            surface.LayoutTransform = transform;
        }

我正在使用上述代码通过 Microsoft Print to PDF 打印我的画布。它在所有情况下都可以正常工作,但是当我在文件名中输入逗号时,例如 (Print,Test) 在打印时。它不会保存我的文件,也不会提供任何 错误/异常 代码,我可以通过这些代码提示用户更改文件名。

请帮我解决我的问题。

【问题讨论】:

    标签: c# wpf printing


    【解决方案1】:

    简而言之,不要这样做

    这是打印到 PDF 时的一个已知错误

    使用不同的驱动程序或不要在文件名中添加逗号

    它不仅来自 WPF,通常在某些情况下也是如此。

    Bug in "Print to PDF" and "Print to XPS" in Windows 10? comma in filename results in zero-byte file

    Microsoft Print to PDF not working

    Found a bug in Microsoft Print to PDF

    Microsoft Print to PDF in Windows 10

    【讨论】:

    • 感谢@Michael 的回复。但我不是最终用户,当用户将逗号放在文件名中时会出现此问题。我们怎么能限制一个人不使用逗号保存文件名。我们可以做的是让他显示一条错误消息,但在我们的例子中,代码没有返回任何错误代码。所以我们别无选择。
    • @Praveensharma 检查逗号并抛出或显示对话框
    • 我在 System.Windows.Controls.PrintDialog 中找不到任何可以为我们提供文件路径/名称的属性。那么我们如何向最终用户显示任何错误消息。我们可以获得文件名的任何建议。
    • 你可以检查文件是否有0字节我猜
    • 打开打印对话框后,我们从可用打印机列表中选择 Microsoft Print to PDF,然后显示另一个 FileSave 弹出窗口,但我们没有可以控制它所以我的问题是我没有关于最终用户选择的路径或文件名的任何信息。有什么方法可以获取用户输入的路径或文件名?
    猜你喜欢
    • 2019-10-10
    • 2017-08-16
    • 2022-01-10
    • 2023-04-08
    • 2021-06-06
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    相关资源
    最近更新 更多