WPF 打印 FlowDocument 未居中

发布于 2024-10-03 22:40:23 字数 1784 浏览 9 评论 0原文

我想打印带有小标题的一些文本的页面。 我希望所有文本都在页面上居中,但我不知道该怎么做。

这是我的代码。它是轨道类型,它只是一个包含艺术家姓名、专辑名称、歌曲等信息的对象标题和歌词。

 PrintDialog dialog = new PrintDialog();
            if (dialog.ShowDialog() != true)
            { return; }
            FlowDocument doc = new FlowDocument();
            Section sec = new Section();
            Paragraph header = new Paragraph();
            Paragraph body = new Paragraph();
            Bold boldSong = new Bold();
            boldSong.Inlines.Add(new Run(t.Song));
            header.Inlines.Add(boldSong);
            header.Inlines.Add(new LineBreak());
            Bold boldArtist = new Bold();
            if (!string.IsNullOrWhiteSpace(t.Artist))
            {
                boldArtist.Inlines.Add(new Run(t.Artist));
                header.Inlines.Add(boldArtist);
                header.Inlines.Add(new LineBreak());
            }
            Bold boldAlbum = new Bold();
            if (!string.IsNullOrWhiteSpace(t.Album))
            {
                boldAlbum.Inlines.Add(new Run(t.Album));
                header.Inlines.Add(boldAlbum);
                header.Inlines.Add(new LineBreak());
            }
            header.TextAlignment = TextAlignment.Center;
            body.Inlines.Add(t.iTunesFileTrack.Lyrics);
            body.TextAlignment = TextAlignment.Center;
            doc.Blocks.Add(header);
            doc.Blocks.Add(body);
            doc.Name = "FlowDoc";
            IDocumentPaginatorSource idpSource = doc;
            DocumentPaginator holder = idpSource.DocumentPaginator;
            holder.PageSize = new Size(dialog.PrintableAreaWidth,
        dialog.PrintableAreaHeight);
            dialog.PrintDocument(holder, "Lyrics");

页面打印得很好,除了打印时整个内容紧贴在文档的左侧...我知道有一些属性我没有正确设置或根本没有设置..

I want to print a page from some text I have with a small header.
I wanted all the text to be centered on the page but I'm not sure how to do it..

Here is my code.. t is of type track which is just an object that holds information like artist name, album name, song title, and lyrics.

 PrintDialog dialog = new PrintDialog();
            if (dialog.ShowDialog() != true)
            { return; }
            FlowDocument doc = new FlowDocument();
            Section sec = new Section();
            Paragraph header = new Paragraph();
            Paragraph body = new Paragraph();
            Bold boldSong = new Bold();
            boldSong.Inlines.Add(new Run(t.Song));
            header.Inlines.Add(boldSong);
            header.Inlines.Add(new LineBreak());
            Bold boldArtist = new Bold();
            if (!string.IsNullOrWhiteSpace(t.Artist))
            {
                boldArtist.Inlines.Add(new Run(t.Artist));
                header.Inlines.Add(boldArtist);
                header.Inlines.Add(new LineBreak());
            }
            Bold boldAlbum = new Bold();
            if (!string.IsNullOrWhiteSpace(t.Album))
            {
                boldAlbum.Inlines.Add(new Run(t.Album));
                header.Inlines.Add(boldAlbum);
                header.Inlines.Add(new LineBreak());
            }
            header.TextAlignment = TextAlignment.Center;
            body.Inlines.Add(t.iTunesFileTrack.Lyrics);
            body.TextAlignment = TextAlignment.Center;
            doc.Blocks.Add(header);
            doc.Blocks.Add(body);
            doc.Name = "FlowDoc";
            IDocumentPaginatorSource idpSource = doc;
            DocumentPaginator holder = idpSource.DocumentPaginator;
            holder.PageSize = new Size(dialog.PrintableAreaWidth,
        dialog.PrintableAreaHeight);
            dialog.PrintDocument(holder, "Lyrics");

The page prints just fine except for the fact that the whole thing clings to the left of the document when printed... I know there is some property I'm not setting correctly or not setting at all..

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

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

发布评论

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

评论(1

故人爱我别走 2024-10-10 22:40:23

增加 ColumnWidth 属性的大小或将其设置为“999999”

increase the size of the ColumnWidth property or just make it '999999'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文