How to print in UWP app?

前端 未结 2 772
清歌不尽
清歌不尽 2020-12-31 08:52

I am trying to print out something from my UWP app. Basically I\'ve used a WebViewBrush to draw some data on to some FrameworkElement\'s (Windows.UI.Xaml.Shapes

2条回答
  •  离开以前
    2020-12-31 09:06

    I've been struggling with this also. I've gone over and over the SDK example. This solution may be helpful to you. If there is some way that you can put what you want to print into a RichTextBox, you can build the paragraphs for it programmatically. And replace the RichTextBox in the SDK example with a blank StackPanel that you have laid out the grid requirements on, and it will solve your issue. Mine wasn't exactly the same issue, because I have done it with Creating an even 2 columned grocery list. The trick was to use a fixed width font, CourierNew, in my example.

    In your page that calls the print libraries, you need to have this in the XAML

    In the page that is substituting for PageToPrint, you can build this stuff right in the constructor. I pass a Collection into the instantiation of the page, and then calculate the layout right in the PageToPrint substitute, like this,,,

       private void MakeThePrintOut()
        {
    
    
            RichTextBlock gutOne = initBlock();
            PopulateBlock(gutOne);
            ContentStack.Children.Add(gutOne);
    
    
        }
        private RichTextBlock initBlock()
        {
    
            RichTextBlock gutInitBlock = new RichTextBlock();
            gutInitBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.Black);
            gutInitBlock.FontSize = 18;
            gutInitBlock.OverflowContentTarget = FirstLinkedContainer;
            gutInitBlock.FontFamily = new FontFamily("Courier New");
            gutInitBlock.VerticalAlignment = VerticalAlignment.Top;
            gutInitBlock.HorizontalAlignment = HorizontalAlignment.Left;
            return gutInitBlock;
    
        }
        private void PopulateBlock( RichTextBlock Blocker)
        {
    
    
            bool firstItem = true;
            int firstLength = 0;
            Paragraph paraItem = null;
            Run itemRun = null;
    
            string CurrentIsle = "None";
    
            foreach( Grocery j in Grocs)
            {
                if (j.Isle != CurrentIsle)
                {
                    if ((CurrentIsle != "None") && (!firstItem))
                    {
                        paraItem.Inlines.Add(itemRun);
                        Blocker.Blocks.Add(paraItem);
    
                    }
                    CurrentIsle = j.Isle;
                    firstItem = true;
                    Paragraph paraIsle = new Paragraph();
                    Run paraRan = new Run();
                    paraRan.Text = "     " + j.Isle;
                    paraIsle.Inlines.Add(paraRan);
                    Blocker.Blocks.Add(paraIsle);
    
    
                }
               if (firstItem)
                {
                    paraItem = new Paragraph();
                    itemRun = new Run();
                    itemRun.Text = "        [] " + j.Item;
                    firstLength = j.Item.Length;
                    firstItem = false;
                } else
                {
                    firstItem = true;
                    string s = new string(' ', 30 - firstLength);
                    itemRun.Text += s + "[] " +  j.Item;
                    paraItem.Inlines.Add(itemRun);
                    Blocker.Blocks.Add(paraItem);
    
                }
    
    
    
    
    
                }
            if (!firstItem)
            {
                paraItem.Inlines.Add(itemRun);
                Blocker.Blocks.Add(paraItem);
            }
        }
    

    It's not exactly what you are looking for, but I know how hard it is to find something that makes sense to answer the printing question. If you want to see the complete example it's on my GitHub.com/Gibbloggen GutenbergOne project was my prototyping that I developed this stuff. And I also have imported into my main project EssentialGrocer, this too is open source in the same location. That, just tonight, includes printing of the shopping list.

    Hope some of this helps, I've really struggled with this one also.

提交回复
热议问题