【问题标题】:Printing on a paper form with preview在带有预览的纸质表单上打印
【发布时间】:2013-03-13 15:32:16
【问题描述】:

我们有大量需要填写的纸质表格。手工做这个非常乏味,所以我们正在构建一个应用程序。它应该提供一个表格来填写数据,能够显示打印预览,在纸质表格上打印数据,并保留历史记录。

目前,我们有一个FixedPage,我们打印如下:

var dlg = new PrintDialog();
if (dlg.ShowDialog() == true)
{
    var doc = new FixedDocument();
    doc.DocumentPaginator.PageSize = new Size(11.69 * 96, 8.27 * 96); // A4 Landscape

    var fp = Application.LoadComponent(new Uri("/FixedPage.xaml", UriKind.Relative)) as FixedPage;
    fp.DataContext = this;
    fp.UpdateLayout();

    var pc = new PageContent();
    ((IAddChild)pc).AddChild(fp);
    doc.Pages.Add(pc);

    dlg.PrintTicket.PageOrientation = System.Printing.PageOrientation.Landscape;
    dlg.PrintDocument(doc.DocumentPaginator, string.Format("Form #{0}", FormNumber));
}

对于打印预览,我们有一个自定义UserControl,背景是纸质表格的扫描图像,前景是数据。基本上,它是在重复FixedPage 布局,这一切都让我们认为我们的设计存在缺陷。

有没有更好的方法来做我们想做的事?

【问题讨论】:

  • 我认为您可以从stackoverflow.com/questions/404010/… 中受益,我认为最好的方法是将其用于打印和预览。我自己从未使用过 PrintDialog。但是应该有打印来预览而不是纸张。它可能只是另一种设备或类似的东西。
  • @Dzyann,谢谢,它看起来不错!但是,我不太确定如何在预览中显示背景图像,而不将其打印在纸上。如果你提供一个样本,我很乐意接受它作为答案。
  • 您用于布局的用户控件是否可能只包含一个 Canvas,背景是扫描的图像,在您直接使用 FixedPage.xmal 的 cambas 内部?
  • @Dzyann,它可能会,但这就是现在拥有的:两个控件,它们仅在图像上有所不同。我想避免这种情况,以及构建一个预览窗口(我想像 .NET 这样的框架必须准备好)。

标签: c# wpf mvvm printing


【解决方案1】:

我的任务是同样的问题,并希望避免编写自己的模板系统以节省时间、单元测试和我的理智。

我最终写了一个混合体来做一些很酷的事情。首先,我使用 HTML 和 CSS 编写模板。在我们的营销部门进行细微调整时,这很容易做到并且具有很大的灵活性。

我用我自己的标签(例如 [code_type/]、[day_list]...[/day_list])填充了模板,并用一个可以是单值或多值标签的字典替换了文本。

生成 html 后,我会使用我发现的一个 html 到 pdf 库,它使用开源 webkit 引擎来呈现和创建生成的 pdf。结果非常稳定,编写初始程序花了大约 2 周时间。每个人都非常高兴,测试变得轻而易举。

如果您想了解更多详细信息,请给我发消息或回复。

【讨论】:

  • 您是如何显示打印预览的?我想我仍然需要两个不同的模板 - 一个有背景图像(用于预览),一个没有(用于打印)。
  • 我没有。我只是在视图中呈现了 html。 :)
【解决方案2】:

我们已经设法找到了一个解决方案,它可以让我们丢弃一堆多余的代码。还是很丑:

public class CustomDocumentViewer : DocumentViewer
{
    public static readonly DependencyProperty BackgroundImageProperty =
        DependencyProperty.Register("BackgroundImage", typeof(Image), typeof(CustomDocumentViewer), new UIPropertyMetadata(null));

    public Image BackgroundImage
    {
        get { return GetValue(BackgroundImageProperty) as Image; }
        set { SetValue(BackgroundImageProperty, value); }
    }

    protected override void OnDocumentChanged()
    {
        (Document as FixedDocument).Pages[0].Child.Children.Insert(0, BackgroundImage);
        base.OnDocumentChanged();
    }

    protected override void OnPrintCommand()
    {
        var printDialog = new PrintDialog();
        if (printDialog.ShowDialog() == true)
        {
            (Document as FixedDocument).Pages[0].Child.Children.RemoveAt(0);
            printDialog.PrintDocument(Document.DocumentPaginator, "Test page");
            (Document as FixedDocument).Pages[0].Child.Children.Insert(0, BackgroundImage);
        }
    }
}

...

<local:CustomDocumentViewer x:Name="viewer" BackgroundImage="{StaticResource PaperFormImage}"/>

...

InitializeComponent();
viewer.Document = Application.LoadComponent(new Uri("/PaperFormDocument.xaml", UriKind.Relative)) as IDocumentPaginatorSource;

我们使用Application.LoadComponent 而不是绑定的原因是一个五年前的错误:http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=293646

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 2022-07-04
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多