在 DocumentViewer 中预览 FixedDocument 看起来不错,打印它总是打印第一页

Previewing FixedDocument in DocumentViewer looks ok, printing it always prints the first page

我正在创建一个 FixedDocument,方法是将 FixedPages 添加到 PageContents,然后以某种方式将它们添加到 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 秒查看它。像 "Wrong username or password" 这样的东西 - 这没有意义,因为我要打印的图像是本地图像。

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

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

在此先感谢您的帮助!

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

我正在加载多页 LZW-compressed TIFF,如下所示:

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

其中 ImagesImageSource 的 collection。它们在应用程序中显示良好,我也可以使用 TiffBitmapEncoder 再次保存它们,但使用 WPF 打印它们最终会出现问题中提到的问题以及 - 使用 DocumentViewer 时 - 异常告诉我 'wrong username or password',这没有意义。

我发现图像有问题的方法是使用 PngBitmapEncoder 临时保存 TIFF 的单个 ImageSources,然后立即使用相同的编码器从单独的文件中重新加载页面我的 Images collection.

中的相同插槽

因为这没有任何问题(在我的 DocumentViewer 我的打印工作正常中没有 username/password 异常)我必须假设他没有不喜欢 TIFF 格式。

这并没有回答我的基本问题 为什么 它不起作用,但由于这至少是一个可行的解决方法,我将把它放在这里暂时不要检查 'answered' 标记。

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

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

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

当我遇到这个 post 时,我想我会尝试保存然后按照建议重新加载图像,这成功了!!然而,每页 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;
   }