public static void ConvertToXps(FixedDocument fixedDoc, Stream outputStream) { var package = Package.Open(outputStream, FileMode.Create); var xpsDoc = new XpsDocument(package, CompressionOption.Normal); XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc); // xps documents are built using fixed document sequences var fixedDocSeq = new FixedDocumentSequence(); var docRef = new DocumentReference(); docRef.BeginInit(); docRef.SetDocument(fixedDoc); docRef.EndInit(); ((IAddChild)fixedDocSeq).AddChild(docRef); // write out our fixed document to xps xpsWriter.Write(fixedDocSeq.DocumentPaginator); xpsDoc.Close(); package.Close(); }
private void PrintImage(string fileName, Int32Rect crop, int quantity) { Console.WriteLine(" Print image " + fileName + " in " + quantity.ToString() + " copies."); PrintTicket ticket = _printer.UserPrintTicket; Size paper = new Size(Math.Max(ticket.PageMediaSize.Width.Value, ticket.PageMediaSize.Height.Value), Math.Min(ticket.PageMediaSize.Width.Value, ticket.PageMediaSize.Height.Value)); Size format = new Size(Math.Max(_width, _height) * 96 / 25.4, Math.Min(_width, _height) * 96 / 25.4); double printableWidth; double printableHeight; if (format.Width <= paper.Width && format.Height <= paper.Height) { printableWidth = format.Width; printableHeight = format.Height; } else if (format.Width <= paper.Height && format.Height <= paper.Width) { printableWidth = format.Height; printableHeight = format.Width; } else { printableWidth = paper.Width; printableHeight = paper.Height; } // Swap printable width and height if page orientation is portarait if (ticket.PageOrientation == PageOrientation.Portrait || ticket.PageOrientation == PageOrientation.ReversePortrait) { double temp = printableWidth; printableWidth = printableHeight; printableHeight = temp; } fileName = _orderFolder + fileName; // Create FixedDocument containing the image var fixedDocument = new FixedDocument(); var pageContent = new PageContent(); var fixedPage = new FixedPage(); var image = CreateImage(fileName, printableWidth, printableHeight, crop); fixedPage.Width = printableWidth; fixedPage.Height = printableHeight; fixedPage.Children.Add(image); ((IAddChild)pageContent).AddChild(fixedPage); fixedDocument.Pages.Add(pageContent); // Save FixedDocument to XPS file string xpsFilePath = fileName + _width.ToString() + "x" + _height.ToString() + ".xps"; using (FileStream outputStream = File.Create(xpsFilePath)) { var package = Package.Open(outputStream, FileMode.Create); var xpsDoc = new XpsDocument(package, CompressionOption.Normal); var xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc); var fixedDocSeq = new FixedDocumentSequence(); var docRef = new DocumentReference(); docRef.BeginInit(); docRef.SetDocument(fixedDocument); docRef.EndInit(); ((IAddChild)fixedDocSeq).AddChild(docRef); xpsWriter.Write(fixedDocSeq.DocumentPaginator); xpsDoc.Close(); package.Close(); } // Add the XPS file to print queue for (int i = 0; i < quantity; i++) { _printer.AddJob("Photo Kiosk", xpsFilePath, false); _success = true; } }