1

我在 sqlite 数据库中有一个表,其中包含一些书页。每本书包含大约 300-400 页。数据库中的每条记录都有一个flowDocument格式的页面内容字段。

现在我想在 documentRaader(如 Adob​​e pdf 阅读器)中加载每本书,由于页面数量众多,速度非常慢。

这是我的代码:

public void LoadBook()
{
    FlowDocumentReader ViewContent = new FlowDocumentReader()
    FixedDocument fd = new FixedDocument();
    ViewContent.Document = fd;

    DataTable dt = libraryclass.GetBookPages();
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        string pagecontent = ReplaceText(dt.Rows[i]["Content"].ToString());

        FlowDocument flow = ContentView.ConvertForView(pagecontent);

        flow.TextAlignment = TextAlignment.Justify;
        flow.FontSize = 18;
        PageContent page = new PageContent();

        RichTextBox rich = new RichTextBox();
        //rich.LineDown();
        rich.IsReadOnly = true;
        rich.Document = flow;
        rich.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

        rich.Margin = new Thickness(20, 70, 20, 70);
        //rich.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
        rich.Width = 710;
        rich.BorderBrush = null;
        rich.BorderThickness = new Thickness(0);
        rich.Height = 840;

        fd.Pages.Add(page);
        page.Child = new FixedPage();
        page.Child.Width = 750;
        page.Child.Height = 970;

        page.Child.Children.Add(rich);
    }
}

我只能加载当前每次显示的书页和下一页/前页吗?

WPF中是否有像android ViewPager这样的控件?

提前致谢。

编辑 :

我更改了代码,现在我将 xpsDocument 与此代码一起使用:

private void LoadBook()
{
    DataTable dt = db.Select("Select Content From PageTBl where BookID = 3");
    FlowDocument flow = new FlowDocument();
    foreach (DataRow row in dt.Rows)
    {
        String content = row["Content"].ToString();
        content = String.IsNullOrEmpty(content.Trim()) ? "<FlowDocument xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Name=\"flowducument21\"><Section/></FlowDocument>" : content;
        FlowDocument fd = XamlReader.Load(new MemoryStream(Encoding.GetEncoding("utf-8").GetBytes(content))) as FlowDocument;
        flow.Blocks.Add(fd.Blocks.FirstBlock);   
    }

    DocumentPaginator dpaginator = new CustomDocumentPaginator(((IDocumentPaginatorSource)flow).DocumentPaginator, new Size(793.5987, 1122.3987), new Size(96, 96), "");

    MemoryStream stream = new MemoryStream();

    Package package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite);

    var uri = new Uri(@"memorystream://myXps.xps");

    PackageStore.AddPackage(uri, package);
    var xpsDoc = new XpsDocument(package);
    xpsDoc.Uri = uri;
    XpsDocument.CreateXpsDocumentWriter(xpsDoc).Write(dpaginator);
    FixedDocumentSequence fds = xpsDoc.GetFixedDocumentSequence();

    DViewer.Document = fds;
}

但加载大约需要 140 秒。如何更快地加载它?

4

0 回答 0