c# - 如何适合打印的固定文档?
问题描述
我目前正在使用 C# 语言和 .Net Framework 4.8 开发 WPF 应用程序在MainWindow
我有Print
菜单按钮来生成Fixed Document
这样的
private void clPrintMenu_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
/* Margins is a User-defined structure to set Top, Right, Bottom and Left values
in Cm, Inches and Pixels */
Margins margins = new Margins(21, 29.7);
margins.Unit = Units.Pixel;
Size sz = new Size(margins.Left.Value, margins.Top.Value);
FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = sz;
/* CashJournal is a UserControl designed as an A4 sheet the list below contains several
Cashjournal which represent multiple pages */
List<CashJournal> journals = CashJournal.PrintJournals;
foreach (CashJournal jrl in journals)
{
FixedPage page = new FixedPage();
page.Width = margins.Left.Value;
page.Height = margins.Top.Value;
FixedPage.SetLeft(jrl, 0);
FixedPage.SetTop(jrl, -20);
page.Children.Add(jrl);
PageContent content = new PageContent();
((IAddChild)content).AddChild(page);
document.Pages.Add(content);
}
/* The document is then passed to a window for preview */
CashPrintPreview dialog = new CashPrintPreview(selectedTab, document);
dialog.ShowDialog();
}
在 CashPrintPreview 中,文档显示在一个DocumentViewer
有一个Print
按钮的地方。我修改了Print() CommandBinding
将函数绑定到我的自定义函数PrintPreview
private void PrintView(object sender, ExecutedRoutedEventArgs e)
{
PrintDialog dialog = new PrintDialog();
bool? rslt = dialog.ShowDialog();
if (rslt != true)
return;
/* This block is my problem */
PrintQueue queue = dialog.PrintQueue;
PrintCapabilities capabilities = queue.GetPrintCapabilities();
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
document.DocumentPaginator.PageSize = sz;
XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(queue);
writer.Write(document);
}
当我从 中选择 时XPS printer
,PrintDialog
创建的文件会完美呈现在预览中。但是当我PDF printer
从 Adobe 中选择时,文档的缩放比例不好,比如顶部的边距太多而左边的边距不够。
我该如何解决这个问题。谢谢。PS。请明确。
解决方案
我终于可以FixedDocument
使用 Acrobat 打印机打印 PDF 文件了。我只需要将 my 传递document
给PrintDocument
所选打印机的功能:
private void PrintView(object sender, ExecutedRoutedEventArgs e)
{
PrintDialog dialog = new PrintDialog();
bool? rslt = dialog.ShowDialog();
if (rslt != true)
return;
dialog.PrintDocument(((IDocumentPaginatorSource)document).DocumentPaginator, "");
}
推荐阅读
- android - 如何将数字数组从高到低排序(反向/递减顺序)?
- javascript - react-select isMulti 选择所有过滤的选项
- python - 动态更新 pandas 列名以避免代码更改
- python - 为什么我们使用大括号内置类.__init__([])
- python - 如何使用经过训练的生成器生成不同的样本
- django - 您如何翻译页面的 slug 值?
- ios - 从 REST API 加载数据后执行 segue
- angular - 有没有办法以“角度方式”为每列使用带有过滤器的角度数据表?
- javascript - 测试所有已终止的功能
- python - Python MATPLOT 散点图