运行时流程文档中的分页

发布于 2024-11-06 06:09:43 字数 1108 浏览 2 评论 0原文

我能够在流文档上获取文本文件,但现在我必须在运行时将内容划分为适当的分页符,即如果内容很大,它们在运行时也会获得同样的页数。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TextOnFlowDoc
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
            Paragraph paragraph = new Paragraph();

            paragraph.Inlines.Add(System.IO.File.ReadAllText(@"C:\Lis.txt"));
            paragraph.FontFamily = new FontFamily("CourierNew");

            FlowDocument document = new FlowDocument(paragraph);
            // FlowDocumentReader rdr = new FlowDocumentReader();
            FlowDocScl.Document = document;
        }
    }
}

现在这个“FlowDocScl”现在是一个流文档,需要在运行时分成页面。

I AM able to get a text file on a flow document but now I have to divide the contents in proper pagebreaks at runtime i.e if contents are huge they shud get itself in number of pages that too at runtime.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TextOnFlowDoc
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
            Paragraph paragraph = new Paragraph();

            paragraph.Inlines.Add(System.IO.File.ReadAllText(@"C:\Lis.txt"));
            paragraph.FontFamily = new FontFamily("CourierNew");

            FlowDocument document = new FlowDocument(paragraph);
            // FlowDocumentReader rdr = new FlowDocumentReader();
            FlowDocScl.Document = document;
        }
    }
}

Now this "FlowDocScl" is now a flow document and needs to be breaked into pages AT RUNTIME.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不寐倦长更 2024-11-13 06:09:43

我不知道为什么你想要自定义分页符,如果你在 FlowDocumentPageViewer 例如,如果内容对于查看者来说太大,您会自动中断。

如果必须按需插入分隔符,则需要在 ,它们具有名为BreakPageBefore 当设置为 true 时,显然会在该块之前插入分页符。

像这样的东西(未经测试):

private void BreakAndAddText(string text)
{
    var pages = text.Split(new string[] { "\\f" }, StringSplitOptions.None);
    foreach (var page in pages)
    {
        document.Blocks.Add(new Paragraph(new Run(page)) { BreakPageBefore = true });
    }
}

I am not sure why you want custom page-breaks, if you display it in a FlowDocumentPageViewer for example you get automatic breaks if the content is too large for the viewer.

If you must insert breaks on demand you need to split the document in Blocks, those have a property called BreakPageBefore which when set to true inserts a page break before that block obviously.

Something like this (untested):

private void BreakAndAddText(string text)
{
    var pages = text.Split(new string[] { "\\f" }, StringSplitOptions.None);
    foreach (var page in pages)
    {
        document.Blocks.Add(new Paragraph(new Run(page)) { BreakPageBefore = true });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文