Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在UWP中打印多页_C#_Printing_Uwp - Fatal编程技术网

C# 如何在UWP中打印多页

C# 如何在UWP中打印多页,c#,printing,uwp,C#,Printing,Uwp,我有一个打印系统,在我的UWP项目中起作用 要准备打印内容,我使用以下方法: (来源:) 打印页面 public sealed partial class PrintPage : Page { public PrintPage() { this.InitializeComponent(); } public PrintPage(RichTextBlockOverflow textLinkContai

我有一个打印系统,在我的UWP项目中起作用

要准备打印内容,我使用以下方法:

(来源:)

打印页面

public sealed partial class PrintPage : Page
    {
        public PrintPage()
        {
            this.InitializeComponent();
        }

        public PrintPage(RichTextBlockOverflow textLinkContainer)
            : this()
        {
            textLinkContainer.OverflowContentTarget = continuationPageLinkedContainer;
        }

        internal void AddContent(Paragraph block)
        {
            this.textContent.Blocks.Add(block);
        }
    }
<RichTextBlock x:Name="PrintContent">
        <!-- Content -->
    </RichTextBlock>
按照我设置程序的方式,我需要能够在单独的页面上打印RichTextBox的每个段落

这可能吗

我需要能够在单独的页面上打印RichTextBox的每个段落

如果要在单独的页面上打印RichTextBox的每个段落,则需要为每个段落创建多个打印页,然后将打印页放入PrintingRoot和打印页列表。之后,在添加预览页面时,需要迭代打印页面并添加它们

我做了以下更改,这里是一个,你可以下载并检查它

private void PreparePrintContent()
{
    PrintingRoot.Children.Clear();
    // Create and populate print page.
    var printPage = Activator.CreateInstance(this.printPageType) as Page;

    printPage.DataContext = this.DataContext;

    var printPageRtb = printPage.Content as RichTextBlock;
    while (printPageRtb.Blocks.Count > 0)
    {
        PrintPage firstPage = new PrintPage();
        firstPage.AddContent(new Paragraph());
        var paragraph = printPageRtb.Blocks.First() as Paragraph;
        printPageRtb.Blocks.Remove(paragraph);

        firstPage.AddContent(paragraph);
        NeedToPrintPages.Add(firstPage);
        PrintingRoot.Children.Add(firstPage);
    };
}

private RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow lastRTBOAdded, PrintPageDescription printPageDescription,int index)
{
    ......
    if (lastRTBOAdded == null)
    {
        // If this is the first page add the specific scenario content
        page = NeedToPrintPages[index];
    }
    ......
}

private void PrintDocument_Paginate(object sender, PaginateEventArgs e)
{
    // Clear the cache of preview pages 
    printPreviewPages.Clear();
    this.pageNumber = 0;

    // Clear the printing root of preview pages
    PrintingRoot.Children.Clear();
    for (int i = 0; i < NeedToPrintPages.Count; i++) {
        // This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed
        RichTextBlockOverflow lastRTBOOnPage;
        // Get the PrintTaskOptions
        PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

        // Get the page description to deterimine how big the page is
        PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

        // We know there is at least one page to be printed. passing null as the first parameter to
        // AddOnePrintPreviewPage tells the function to add the first page.
        lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription,i);

        // We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview
        // page has extra content
        while (lastRTBOOnPage.HasOverflowContent && lastRTBOOnPage.Visibility == Windows.UI.Xaml.Visibility.Visible)
        {
            lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription,i);
        }

    }

    PrintDocument printDoc = (PrintDocument)sender;

    // Report the number of preview pages created
    printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
}
private void PreparePrintContent()
{
PrintingRoot.Children.Clear();
//创建并填充打印页面。
var printPage=Activator.CreateInstance(this.printPageType)作为页面;
printPage.DataContext=this.DataContext;
var printPageRtb=printPage.Content作为RichTextBlock;
while(printPageRtb.Blocks.Count>0)
{
PrintPage firstPage=新的PrintPage();
添加内容(新段落());
var paragration=printPageRtb.Blocks.First()作为段落;
printPageRtb.块。删除(段落);
首页。添加内容(段落);
需要打印页面。添加(第一页);
PrintingRoot.Children.Add(第一页);
};
}
私有RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow LastrBoaded,PrintPageDescription,PrintPageDescription,int索引)
{
......
if(lastRTBOAdded==null)
{
//如果这是第一页,请添加特定场景内容
页面=需要打印页面[索引];
}
......
}
私有void打印文档_Paginate(对象发送方,paginateventargs e)
{
//清除预览页面的缓存
printPreviewPages.Clear();
这个.pageNumber=0;
//清除预览页的打印根目录
PrintingRoot.Children.Clear();
对于(int i=0;i
我遇到了一个问题,但当我尝试一个接一个地打印两个单独的文档时,在尝试打印第二个文档时出现错误:“未检测到安装的组件。元素已经是另一个元素的子元素”。(在GetPrintPreviewPage函数中),这意味着什么?这意味着您尝试添加的元素已经添加到另一个父元素,您不能再次将此元素添加到其他元素。
<RichTextBlock x:Name="textContent"
                           Grid.Row="1"
                           Grid.ColumnSpan="2"
                           FontSize="18"
                           OverflowContentTarget="{Binding ElementName=continuationPageLinkedContainer}"
                           IsTextSelectionEnabled="True"
                           TextAlignment="Left"
                           FontFamily="Segoe UI"
                           VerticalAlignment="Top"
                           HorizontalAlignment="Left">
</RichTextBlock>

<RichTextBlockOverflow x:Name="continuationPageLinkedContainer" Grid.Row="2" />
<RichTextBlock x:Name="PrintContent">
        <!-- Content -->
    </RichTextBlock>
private void PreparePrintContent()
{
    PrintingRoot.Children.Clear();
    // Create and populate print page.
    var printPage = Activator.CreateInstance(this.printPageType) as Page;

    printPage.DataContext = this.DataContext;

    var printPageRtb = printPage.Content as RichTextBlock;
    while (printPageRtb.Blocks.Count > 0)
    {
        PrintPage firstPage = new PrintPage();
        firstPage.AddContent(new Paragraph());
        var paragraph = printPageRtb.Blocks.First() as Paragraph;
        printPageRtb.Blocks.Remove(paragraph);

        firstPage.AddContent(paragraph);
        NeedToPrintPages.Add(firstPage);
        PrintingRoot.Children.Add(firstPage);
    };
}

private RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow lastRTBOAdded, PrintPageDescription printPageDescription,int index)
{
    ......
    if (lastRTBOAdded == null)
    {
        // If this is the first page add the specific scenario content
        page = NeedToPrintPages[index];
    }
    ......
}

private void PrintDocument_Paginate(object sender, PaginateEventArgs e)
{
    // Clear the cache of preview pages 
    printPreviewPages.Clear();
    this.pageNumber = 0;

    // Clear the printing root of preview pages
    PrintingRoot.Children.Clear();
    for (int i = 0; i < NeedToPrintPages.Count; i++) {
        // This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed
        RichTextBlockOverflow lastRTBOOnPage;
        // Get the PrintTaskOptions
        PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

        // Get the page description to deterimine how big the page is
        PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

        // We know there is at least one page to be printed. passing null as the first parameter to
        // AddOnePrintPreviewPage tells the function to add the first page.
        lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription,i);

        // We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview
        // page has extra content
        while (lastRTBOOnPage.HasOverflowContent && lastRTBOOnPage.Visibility == Windows.UI.Xaml.Visibility.Visible)
        {
            lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription,i);
        }

    }

    PrintDocument printDoc = (PrintDocument)sender;

    // Report the number of preview pages created
    printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
}