0

I'm creating a FixedDocument by adding FixedPages to PageContents, then adding them to the FixedDocument somehow like this

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

Printing them with a PrintDialog, like this

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

results in the correct number of pages. However, every page printed - e.g. to a PDF - is the content of the first page.

I tried testing the ImageSources I add to the FixedPages, those seem correct. I also tested the final FixedDocument with a DocumentViewer like so

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

This strangely shows the correct output I would expect. What's even stranger is that I get an IOException after wnd.Show(); (which is why I surrounded it with a try/catch). Even with the try catch I can only view it maybe 1-2 seconds before the same IOException thrown by my MainWindow. Something like "Wrong username or password" - which doesn't make sense, since the images I'm trying to print are local ones.

Putting the DocumentViewer aside, my problem with the Print() method only printing the first page n times (n being the number of actual pages it should be) still persists, just thought that the exception in the DocumentViewer may give someone an idea of an underlying problem.

This might be a possible duplicate of FixedDocument always print first page - however he doesn't mention problems with DocumentViewer and the question remains unanswered.

Thanks in advance for any help!

Kevin Suppan
  • 232
  • 2
  • 18

2 Answers2

1

I have had a similar issue, printing labels in a FixedDocument from a List of Data, that contains a List Of Image Sources (User Photo), and also dynamically creates a QRCode image from an integer for the users id.

The format for the image is created from a custom UserControl that I used to position The Text fields and images for each label. When I viewed the created document in the DocumentViewer control, it displayed perfectly. Correct photo image, correct QRCode image for each label. However, when I printed the document (or saved to PDF file or XPS File), Ever Label had only the first image in both the Photo and QRCode image positions on the label.

When I came across this post, I though that I would try saving then reloading the images as suggested, and this worked!! However the IO overhead for 30 labels per page, and many pages of labels meant that this wasn't a very useful workaround! I

Then found that simply converting the ImageSource to a ByteArray, and then back again, before adding to the FixedDocument worked also, but without the added IO overhead. Not massively elegant, but has been a real headache for me for a week now!!

Here is a snippet of code from the main body of the method that builds the labels:

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)
}

and here are the methods I used to "Fix" the Images

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;
   }
corn on the cob
  • 2,093
  • 3
  • 18
  • 29
  • Glad that my "solution" lead you to a more elegant way of solving this. Tried it with the ByteArray conversion, worked for me too, thanks! I can't shake my head around the idea that there really isn't a better solution to this, but hey, it's way better than my "temp file" way :) – Kevin Suppan Feb 21 '21 at 23:46
0

So, this isn't really the answer to why it happened, but I found at least the culprit: my image.

I'm loading a multipage LZW-compressed TIFF like so:

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

where Images is a collection of ImageSource. They display fine in the application, I can also save them again using a TiffBitmapEncoder, but printing them using WPF ends up with the in the question mentioned problem as well as - when using a DocumentViewer - an exception telling me about 'wrong username or password', which doesn't make sense.

The way I found out the image to be the problem was temporarily saving the individual ImageSources of the TIFF using a PngBitmapEncoder and immediately reloading the pages from the separate files with the same encoder into the same slot in my Images collection.

Since this works without any issues (no username/password exception in my DocumentViewer and my printing working correctly) I have to assume that he doesn't like something about the TIFF format.

This doesn't answer my underlying question of why it didn't work, but since this is at least a workaround that works, I'll just put that here and don't check the 'answered' mark just yet.

Maybe someone knows why my TIFF ImageSource produced those strange results?

Kevin Suppan
  • 232
  • 2
  • 18