如何适合打印固定文件?

How to fit a fixed document for printing?

我目前正在使用 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);
    }

当我从 PrintDialog 中选择 XPS printer 时,创建的文件在预览中完美呈现。但是,当我从 Adob​​e 选择 PDF printer 时,文档的缩放比例不佳,例如顶部边距过多而左侧边距不足。

我该如何解决这个问题。谢谢。 PS。请明确。

我终于可以使用 Acrobat 打印机将 FixedDocument 打印成 PDF。我只需要将我的 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, "");
}