【问题标题】:Not able to set the width and height property in UWP print preview page无法在 UWP 打印预览页面中设置宽度和高度属性
【发布时间】:2020-01-22 08:39:52
【问题描述】:

我正在开发一个 UWP 应用程序,需要实现打印功能。我也关注了thisdocs。这些链接帮助我设置了所有打印功能,我可以在这些功能中为要打印的内容生成打印预览页面。

但是,预览页面仅在一个小块内显示要打印的图像。我基本上无法设置高度和宽度(或任何类似属性)以使其适合页面。我已附上screenshot 和代码 sn-ps。

我已经坚持了很长一段时间。任何帮助将不胜感激。谢谢。

要打印的打印面板和 UI 元素的 XAML 代码:

                           <Grid>
                                <Image x:Name="myImage"

                                       Height="594"
                                       Width="432"
                                       Margin="50,0,0,0"></Image>
                                <Border x:Name="inkContainer"
                                        Grid.Column="2"
                                        Grid.Row="1"
                                        BorderBrush="Black"
                                        BorderThickness="1"
                                        HorizontalAlignment="Center"
                                        VerticalAlignment="Center"
                                        Margin="50,0,0,0"
                                        Height="594"
                                        Width="432">
                                    <InkCanvas x:Name="inkCanvas"
                                               Height="594"
                                               Width="432" />
                                </Border>
                            </Grid>

                 <RelativePanel x:Name="PrintView"
                               BorderThickness="1"
                               Visibility="Collapsed"
                               Background="#eaedf1"
                               BorderBrush="Black"
                               CornerRadius="2">

                    <Image x:Name="PrintImage"
                           Height="594"
                           Width="432"
                           Margin="50,20,0,0"></Image>

                    <Button Name="btnPrint"
                            Content="Print"
                            Background="#1bcfb4"
                            Foreground="white"
                            CornerRadius="2"
                            IsEnabled="True"
                            Width="100"
                            Margin="160, 630, 0, 0"
                            Click="PrintButtonClick"/>

                    <Button Name="btnClosePrintPreview"
                            Content="Close"
                            Background="#5c5151"
                            Foreground="white"
                            CornerRadius="2"
                            IsEnabled="True"
                            Width="100"
                            Margin="270, 630, 0, 0"
                            Click="btnClosePrintPreview_Click"/>
                </RelativePanel>

后面的代码:

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Register for PrintTaskRequested event
            printMan = PrintManager.GetForCurrentView();
            printMan.PrintTaskRequested += PrintTaskRequested;

            // Build a PrintDocument and register for callbacks
            printDoc = new PrintDocument();
            printDocSource = printDoc.DocumentSource;
            printDoc.Paginate += Paginate;
            printDoc.GetPreviewPage += GetPreviewPage;
            printDoc.AddPages += AddPages;
        }

        private async void PrintButtonClick(object sender, RoutedEventArgs e)
        {
            if (PrintManager.IsSupported())
            {
                try
                {
                    // Show print UI
                    await PrintManager.ShowPrintUIAsync();
                }
                catch
                {
                    // Printing cannot proceed at this time
                    ContentDialog noPrintingDialog = new ContentDialog()
                    {
                        Title = "Printing error",
                        Content = "\nSorry, printing can' t proceed at this time.",
                        PrimaryButtonText = "OK"
                    };
                    await noPrintingDialog.ShowAsync();
                }
            }
            else
            {
                // Printing is not supported on this device
                ContentDialog noPrintingDialog = new ContentDialog()
                {
                    Title = "Printing not supported",
                    Content = "\nSorry, printing is not supported on this device.",
                    PrimaryButtonText = "OK"
                };
                await noPrintingDialog.ShowAsync();
            }
        }

        private void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
        {
            // Create the PrintTask.
            // Defines the title and delegate for PrintTaskSourceRequested
            var printTask = args.Request.CreatePrintTask("Print", PrintTaskSourceRequrested);

            // Handle PrintTask.Completed to catch failed print jobs
            printTask.Completed += PrintTaskCompleted;
        }

        private void btnClosePrintPreview_Click(object sender, RoutedEventArgs e)
        {
            PrintView.Visibility = Visibility.Collapsed;
        }

        private void PrintTaskSourceRequrested(PrintTaskSourceRequestedArgs args)
        {
            // Set the document source.
            args.SetSource(printDocSource);
        }

        private void Paginate(object sender, PaginateEventArgs e)
        {
            // As I only want to print one Rectangle, so I set the count to 1
            printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
        }

        private void GetPreviewPage(object sender, GetPreviewPageEventArgs e)
        {
            // Provide a UIElement as the print preview.
            printDoc.SetPreviewPage(e.PageNumber, this.PrintImage);
        }

        private void AddPages(object sender, AddPagesEventArgs e)
        {
            printDoc.AddPage(this.PrintImage);

            // Indicate that all of the print pages have been provided
            printDoc.AddPagesComplete();
        }

        private async void PrintTaskCompleted(PrintTask sender, PrintTaskCompletedEventArgs args)
        {
            // Notify the user when the print operation fails.
            if (args.Completion == PrintTaskCompletion.Failed)
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                {
                    ContentDialog noPrintingDialog = new ContentDialog()
                    {
                        Title = "Printing error",
                        Content = "\nSorry, failed to print.",
                        PrimaryButtonText = "OK"
                    };
                    await noPrintingDialog.ShowAsync();
                });
            }
            if (args.Completion == PrintTaskCompletion.Submitted)
            {
                this.PrintView.Visibility = Visibility.Collapsed;
            }
        }

【问题讨论】:

    标签: c# uwp visual-studio-2019


    【解决方案1】:

    我检查了你的代码。在 XAML 中,您已设置 PrintImage 的宽度/高度。这意味着在打印的时候,PrintManager会按照你设置的图片尺寸进行打印。

    打印时可以尝试去掉图片的大小限制,或者设置更大的Width/Height

    最好的问候。

    【讨论】:

    • 没有帮助@Richard
    • 嗨,你能提供一个最小的可运行演示吗?用你的代码测试后发现,去掉PrintImage的大小限制后,图片可以按原来的大小打印了。如果取消限制后图片大小没有变化,可能是图片分辨率的问题。
    • 我的坏@Richard。按照您的指示解决问题。谢谢你。我面临一个新问题。使用上面共享的相同代码 sn-p,我正在尝试打印图像。打印机接收到打印命令,但令我惊讶的是,没有打印任何内容。有什么帮助吗?
    • 你修改了什么吗?请详细描述您的复制步骤。
    猜你喜欢
    • 2020-04-19
    • 1970-01-01
    • 2017-11-03
    • 2013-02-09
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    相关资源
    最近更新 更多