0

I am having trouble writing multiple pages to an xps document. I have a loop that goes through my opened windows and saves the last window. this presents to a pdf fine, however it is just the last window in wpf.

MemoryStream lMemoryStream = new MemoryStream();
Package package = Package.Open(lMemoryStream, FileMode.Create);                      
XpsDocument doc = new XpsDocument(package);    
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
writer.Write(Report.reportWindow);
doc.Close();
package.Close();
var pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(lMemoryStream);
PdfSharp.Xps.XpsConverter.Convert(pdfXpsDoc, filename, 0);

I have tried changing the FileMode to Append. However, this comes up with an error "Append and Truncate not supported".

stuartd
  • 70,509
  • 14
  • 132
  • 163
Bish25
  • 606
  • 1
  • 10
  • 35
  • Are you doing that entire code block for each iteration of the loop? If so, you're overwriting the document each time. – Jon Feb 26 '16 at 22:30
  • @jon how do I add to the document? I loop around once and save the first window, how do I loop around and append? – Bish25 Feb 28 '16 at 19:55
  • The only portion of that code block that should be performed for each window is writer.Write(). Opening the stream, package, doc, and writer should be done once at the start of the process. Closing, creating pdfXpsDoc, and calling Convert should be done once at the very end. – Jon Feb 28 '16 at 22:21
  • @jon when the loop comes around for the second time i get this error " Document Writer is done writing and cannot process any more write requests." – Bish25 Feb 29 '16 at 09:06
  • Look into writer.CreateVisualsCollator, as described in the first answer here: http://stackoverflow.com/questions/5533456/wpf-printing-of-multiple-pages-with-preview – Jon Feb 29 '16 at 13:56

0 Answers0