【问题标题】:Previewing FixedDocument in DocumentViewer looks ok, printing it always prints the first page在 DocumentViewer 中预览 FixedDocument 看起来不错,打印它总是打印第一页
【发布时间】:2020-06-04 15:24:02
【问题描述】:

我通过将FixedPages 添加到PageContents 来创建FixedDocument,然后以某种方式将它们添加到FixedDocument

FixedDocument fd = new FixedDocument();
// add FixedPages in PageContent to fd

PrintDialog 打印它们,像这样

pdialog.PrintDocument(fd.DocumentPaginator, "Test");

产生正确的页数。但是,打印的每一页 - 例如到 PDF - 是第一页的内容。

我尝试测试添加到FixedPagesImageSources,这些似乎是正确的。我还用DocumentViewer 测试了最终的FixedDocument,就像这样

Window wnd = new Window();
DocumentViewer viewer = new DocumentViewer();
viewer.Document = fd;
wnd.Content = viewer;
try
{
    wnd.Show();
}
catch(Exception e)
{
    Console.WriteLine(e.ToString());
}

这奇怪地显示了我期望的正确输出。更奇怪的是,我在wnd.Show(); 之后得到了一个IOException(这就是我用try/catch 包围它的原因)。即使使用try catch,我也只能在MainWindow 抛出的相同IOException 之前1-2 秒查看它。诸如“错误的用户名或密码”之类的东西 - 这没有意义,因为我要打印的图像是本地图像。

DocumentViewer放在一边,我对Print()方法的问题只打印第一页n次(n是它应该是的实际页数)仍然存在,只是认为DocumentViewer中的异常可能会让某人了解潜在的问题。

这可能与 FixedDocument always print first page 重复 - 但是他没有提到 DocumentViewer 的问题,并且问题仍未得到解答。

提前感谢您的帮助!

【问题讨论】:

  • 如果您能提供一个可重现的示例供我测试,我或许可以帮助调试。
  • @Keith Stein 感谢您的回复,但至少我找到了根本问题(请参阅下面的答案),而不是为什么会发生这种情况。

标签: c# wpf printing fixeddocument


【解决方案1】:

我遇到了类似的问题,从数据列表打印 FixedDocument 中的标签,其中包含图像源列表(用户照片),并且还从用户 ID 的整数动态创建 QRCode 图像。

图像的格式是从我用来定位每个标签的文本字段和图像的自定义用户控件创建的。当我在 DocumentViewer 控件中查看创建的文档时,它显示得很完美。正确的照片图像,每个标签的正确 QRCode 图像。但是,当我打印文档(或保存为 PDF 文件或 XPS 文件)时,Ever Label 在标签上的 Photo 和 QRCode 图像位置都只有第一个图像。

当我看到这篇文章时,我认为我会尝试保存然后按照建议重新加载图像,这很有效!然而,每页 30 个标签的 IO 开销以及许多标签页意味着这不是一个非常有用的解决方法!我

然后发现只需将 ImageSource 转换为 ByteArray,然后再返回,在添加到 FixedDocument 之前也可以工作,但不会增加 IO 开销。不是很优雅,但已经让我头疼了一个星期了!!

这是构建标签的方法主体中的一段代码:

var qr = GetQRCodeImage(playerId);  // Gets ImageSource
var ph = LoadImage(data[dataIndex].Photo); // Gets ImageSource
var qrCode = FixDocumentCacheImageBugFix(qr); // Gets ImageSource
if (ph != null) {
    var photo = FixDocumentCacheImageBugFix(ph);
    label = new AveryBarcodeLabel(line1, line2, line3, qrCode, photo); // Calls constructor to instantiate new Label with new ImageSources
}
else {
    label = new AveryBarcodeLabel(line1, line2, line3, qrCode); // Calls constructor to instantiate new Label with new ImageSources (where photo is null)
}

这是我用来“修复”图像的方法

public static ImageSource FixDocumentCacheImageBugFix(ImageSource image) {
    var bytes = ImageSourceToBytes(image);
    return ByteToImage(bytes);
}
public static ImageSource ByteToImage(byte[] imageData) {
    var biImg = new BitmapImage();
    var ms = new MemoryStream(imageData);
    biImg.BeginInit();
    biImg.StreamSource = ms;
    biImg.EndInit();
    
    ImageSource imgSrc = biImg;

    return imgSrc;
}
public static byte[] ImageSourceToBytes(ImageSource imageSource) {
    byte[] bytes = null;
    var bitmapSource = imageSource as BitmapSource;

    if (bitmapSource != null) {
        var encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
        
        using (var stream = new MemoryStream()) {
                encoder.Save(stream);
                bytes = stream.ToArray();
            }
        }

        return bytes;
   }

【讨论】:

  • 很高兴我的“解决方案”引导您以更优雅的方式解决此问题。尝试使用 ByteArray 转换,对我也有用,谢谢!对于确实没有更好的解决方案的想法,我无法摇头,但是嘿,这比我的“临时文件”方式要好:)
【解决方案2】:

所以,这并不是为什么发生的真正答案,但我至少找到了罪魁祸首:我的形象。

我正在加载一个多页 LZW 压缩的 TIFF,如下所示:

TiffBitmapEncoder encoder = new TiffBitmapEncoder();
foreach (ImageSource frame in encoder.Frames)
{
    frame.Freeze();
    Images.Add(frame);
}

其中ImagesImageSource 的集合。它们在应用程序中显示良好,我也可以使用 TiffBitmapEncoder 再次保存它们,但是使用 WPF 打印它们最终会出现问题中提到的问题以及 - 当使用 DocumentViewer 时 - 一个异常告诉我“错误的用户名或密码”,这没有意义。

我发现图像存在问题的方法是使用PngBitmapEncoder 临时保存 TIFF 的单个 ImageSources,并立即使用相同编码器将单独文件中的页面重新加载到我的 @ 中的同一插槽中987654328@收藏。

由于这可以正常工作(我的DocumentViewer 中没有用户名/密码异常并且我的打印工作正常)我不得不假设他不喜欢 TIFF 格式的某些内容。

这并不能回答我为什么它不起作用的基本问题,但由于这至少是一种有效的解决方法,我将把它放在这里而不检查'已回答' 标记。

也许有人知道为什么我的 TIFF ImageSource 会产生这些奇怪的结果?

【讨论】:

    猜你喜欢
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多