繁体   English   中英

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

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

我通过将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());
}

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

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

这可能是FixedDocument 总是打印第一页的可能副本 - 但是他没有提到DocumentViewer的问题,并且问题仍未得到解答。

提前感谢您的帮助!

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

图像的格式是从我使用的自定义 UserControl 创建的 position 每个 label 的文本字段和图像。 当我在 DocumentViewer 控件中查看创建的文档时,它显示得很完美。 每个 label 的正确照片图像、正确 QRCode 图像。 但是,当我打印文档(或保存到 PDF 文件或 XPS 文件)时,曾经 Label 在 ZD304BA20E96D87414Z1588EEABAC850E3 上的照片和二维码图像位置都只有第一张图像

当我看到这篇文章时,我想我会尝试保存然后按照建议重新加载图像,这很有效,! 然而,每页 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;
   }

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

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

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

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

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

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

这并不能回答我为什么它不起作用的根本问题,但是由于这至少是一种可行的解决方法,所以我将把它放在这里并且暂时不要检查“已回答”标记。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM