//<SnippetCreateAndWriteToXpsDocument> // ---------------------------- Create() ------------------------------ /// <summary> /// Creates an XpsDocument using the Xps.Packaging APIs.</summary> /// <param name="xpsDocument"> /// The XpsDocument to create.</param> /// <remarks> /// The Xps.Packaging APIs are used to create the DocumentSequence, /// FixedDocument, and FixedPage "PackageParts" of an XpsDocument. /// The applicationt is responsible for using the XmlWriter to /// serialize the page markup and for supplying the streams for any /// font or image resources.</remarks> public void Create(XpsDocument xpsDocument) { // Create the document sequence IXpsFixedDocumentSequenceWriter docSeqWriter = xpsDocument.AddFixedDocumentSequence(); // Create the document IXpsFixedDocumentWriter docWriter = docSeqWriter.AddFixedDocument(); // Create the Page IXpsFixedPageWriter pageWriter = docWriter.AddFixedPage(); // Get the XmlWriter XmlWriter xmlWriter = pageWriter.XmlWriter; // Write the mark up according the XPS Specifications BeginFixedPage(xmlWriter); AddGlyphRun(pageWriter, xmlWriter, "This is a photo of the famous Notre Dame in Paris", 16, 50, 50, @"C:\Windows\fonts\arial.ttf"); AddImage(pageWriter, xmlWriter, "ParisNotreDame.jpg", XpsImageType.JpegImageType, 100, 100, 600, 1100); // End the page. EndFixedPage(xmlWriter); // Close the page, document, and document sequence. pageWriter.Commit(); docWriter.Commit(); docSeqWriter.Commit(); _fontDictionary.Clear(); }// end:Create()
private void btnXmlWriter_Click(object sender, RoutedEventArgs e) { using (XpsDocument xpsDoc = new XpsDocument("Out.xps", FileAccess.Write)) { IXpsFixedDocumentSequenceWriter sequenceWriter = xpsDoc.AddFixedDocumentSequence(); IXpsFixedDocumentWriter docWriter = sequenceWriter.AddFixedDocument(); IXpsFixedPageWriter pageWriter = docWriter.AddFixedPage(); XmlWriter xmlWriter = pageWriter.XmlWriter; xmlWriter.WriteStartElement("FixedPage"); xmlWriter.WriteAttributeString("xmlns", "http://schemas.microsoft.com/xps/2005/06"); xmlWriter.WriteAttributeString("Width", "600"); xmlWriter.WriteAttributeString("Height", "400"); xmlWriter.WriteAttributeString("xml:lang", "ko-KR"); xmlWriter.WriteStartElement("Path"); xmlWriter.WriteAttributeString("Data", "M 10,10 L 150,200 500,300 555,387 z"); xmlWriter.WriteAttributeString("Fill", "#ffff0000"); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); pageWriter.Commit(); docWriter.Commit(); sequenceWriter.Commit(); } }
void ConvertToXps(IEnumerable <FixedDocument> fixedDocuments, Stream outputStream) { var package = Package.Open(outputStream, FileMode.Create); var xpsDoc = new XpsDocument(package, CompressionOption.Normal); var xpsWriter = xpsDoc.AddFixedDocumentSequence(); var fixedDocSeq = xpsDoc.GetFixedDocumentSequence(); // A4 = 210 x 297 mm = 8.267 x 11.692 inches = 793.632 * 1122.432 dots fixedDocSeq.DocumentPaginator.PageSize = new Size(793.632, 1122.432); foreach (var fixedDocument in fixedDocuments) { var docWriter = xpsWriter.AddFixedDocument(); var pageWriter = docWriter.AddFixedPage(); var image = pageWriter.AddImage(XpsImageType.JpegImageType); Stream imageStream = image.GetStream(); //Write your image to stream //Write the rest of your document based on the fixedDocument object } }
}// end:Run() //<SnippetXpsCreateAddPkgContent> // ------------------------- AddPackageContent ---------------------------- /// <summary> /// Adds a predefined set of content to a given XPS document.</summary> /// <param name="xpsDocument"> /// The package to add the document content to.</param> /// <param name="attachPrintTicket"> /// true to include a PrintTicket with the /// document; otherwise, false.</param> private void AddPackageContent( XpsDocument xpsDocument, bool attachPrintTicket) { try { PrintTicket printTicket = GetPrintTicketFromPrinter(); // PrintTicket is null, there is no need to attach one. if (printTicket == null) { attachPrintTicket = false; } // Add a FixedDocumentSequence at the Package root IXpsFixedDocumentSequenceWriter documentSequenceWriter = xpsDocument.AddFixedDocumentSequence(); // Add the 1st FixedDocument to the FixedDocumentSequence. - - - - - IXpsFixedDocumentWriter fixedDocumentWriter = documentSequenceWriter.AddFixedDocument(); // Add content to the 1st document AddDocumentContent(fixedDocumentWriter); // Commit the 1st Document fixedDocumentWriter.Commit(); // Add a 2nd FixedDocument to the FixedDocumentSequence. - - - - - - fixedDocumentWriter = documentSequenceWriter.AddFixedDocument(); // Add content to the 2nd document. AddDocumentContent(fixedDocumentWriter); // If attaching PrintTickets, attach one at the FixedDocument level. if (attachPrintTicket) { fixedDocumentWriter.PrintTicket = printTicket; } // Commit the 2nd document. fixedDocumentWriter.Commit(); // If attaching PrintTickets, attach one at // the package FixedDocumentSequence level. if (attachPrintTicket) { documentSequenceWriter.PrintTicket = printTicket; } // Commit the FixedDocumentSequence documentSequenceWriter.Commit(); } catch (XpsPackagingException xpsException) { throw xpsException; } }// end:AddPackageContent()
/// <summary> /// Adds a predefined set of content to a given XPS document. /// </summary> /// <param name="xpsDocument"> /// The package to add the document content to. /// </param> private static void AddPackageContent(XpsDocument xpsDocument, XmlNodeList selectedThumbnails) { // Add a FixedDocumentSequence at the Package root. IXpsFixedDocumentSequenceWriter documentSequenceWriter = xpsDocument.AddFixedDocumentSequence(); // Add a FixedDocument to the FixedDocumentSequence. IXpsFixedDocumentWriter fixedDocumentWriter = documentSequenceWriter.AddFixedDocument(); // Add content to the document. AddDocumentContent(fixedDocumentWriter, selectedThumbnails); // Commit the Document. fixedDocumentWriter.Commit(); // Commit the FixedDocumentSequence. documentSequenceWriter.Commit(); }
private static FixedDocumentSequence CreateDocument(XpsDocument document) { var docSeqWriter = document.AddFixedDocumentSequence(); var docWriter = docSeqWriter.AddFixedDocument(); var pageWriter = docWriter.AddFixedPage(); var xmlWriter = pageWriter.XmlWriter; xmlWriter.WriteStartElement("FixedPage"); xmlWriter.WriteAttributeString("xmlns", "http://schemas.microsoft.com/xps/2005/06"); xmlWriter.WriteStartElement("StackPanel"); xmlWriter.WriteAttributeString("Margin", "20"); xmlWriter.WriteStartElement("TextBlock"); xmlWriter.WriteString("Dock of the Bay"); xmlWriter.WriteEndElement(); var ellipse = new Ellipse { Width = 50, Height = 50, Fill = Brushes.LightBlue, Stroke = Brushes.Gray, StrokeThickness = 2, ToolTip = "Wow", Margin = new Thickness(0, 20, 0, 0) }; XamlWriter.Save(ellipse, xmlWriter); xmlWriter.WriteEndElement(); // StackPanel xmlWriter.WriteEndElement(); // FixedPage pageWriter.Commit(); docWriter.Commit(); docSeqWriter.Commit(); return(document.GetFixedDocumentSequence()); }
public int SaveAsXps(string fileName) { object doc; FileInfo fileInfo = new FileInfo(fileName); using (FileStream file = fileInfo.OpenRead()) { System.Windows.Markup.ParserContext context = new System.Windows.Markup.ParserContext(); context.BaseUri = new Uri(fileInfo.FullName, UriKind.Absolute); doc = System.Windows.Markup.XamlReader.Load(file, context); } if (!(doc is IDocumentPaginatorSource)) { Console.WriteLine("DocumentPaginatorSource expected"); return(-1); } using (Package container = Package.Open(fileName + ".xps", FileMode.Create)) { using (XpsDocument xpsDoc = new XpsDocument(container, CompressionOption.Maximum)) { xpsDoc.AddFixedDocumentSequence(); FixedDocumentSequence seq = xpsDoc.GetFixedDocumentSequence(); DocumentReference reference = new DocumentReference(); //reference.SetDocument(doc); seq.References.Add(reference); FixedDocument abc = new FixedDocument(); PageContent pagecnt = new PageContent(); abc.Pages.Add(pagecnt); XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false); xpsDoc.AddFixedDocumentSequence(); FixedDocumentSequence aaa = xpsDoc.GetFixedDocumentSequence(); DocumentPaginator paginator = ((IDocumentPaginatorSource)doc).DocumentPaginator; // 8 inch x 6 inch, with half inch margin //paginator = new DocumentPaginatorWrapper(paginator, new Size(21*96/2.54, 29.7*96/2.54), new Size(0*96/2.54, 0*96/2.54)); //PrintDialog ptdlg = new PrintDialog(); //ptdlg.ShowDialog(); //((IDocumentPaginatorSource)doc).DocumentPaginator.PageSize = new Size(ptdlg.PrintableAreaWidth, ptdlg.PrintableAreaHeight); //paginator = new DocumentPaginatorWrapper(paginator, new Size(ptdlg.PrintableAreaWidth, ptdlg.PrintableAreaHeight), new Size(0 * 96 / 2.54, 0 * 96 / 2.54)); rsm.SaveAsXaml(paginator); rsm.Commit(); xpsDoc.Close(); container.Close(); XpsDocument tempdoc = new XpsDocument(fileName + ".xps", FileAccess.Read); //viewer.Document = tempdoc.GetFixedDocumentSequence(); } } Console.WriteLine("{0} generated.", fileName + ".xps"); return(0); }
public bool CreateNewXPSFromSource(string sourceXpsPath, string destinationXpsPath, ReplaceCallBackType cb) { this.m_CallBack = cb; bool blnResult = false; try { // Delete the old file generated by our code. if (File.Exists(destinationXpsPath)) { File.Delete(destinationXpsPath); } // Creating the output XPS file. XpsDocument document = new XpsDocument(destinationXpsPath, FileAccess.ReadWrite, System.IO.Packaging.CompressionOption.NotCompressed); IXpsFixedDocumentSequenceWriter docSeqWriter = document.AddFixedDocumentSequence(); // Loading the source xps files to list to read them for edit. List <string> sourceFiles = new List <string>(); sourceFiles.Add(sourceXpsPath); // Looping each source files. foreach (string sourceFile in sourceFiles) { XpsDocument docToRead = new XpsDocument(sourceFile, FileAccess.ReadWrite); IXpsFixedDocumentSequenceReader docSequenceToRead = docToRead.FixedDocumentSequenceReader; foreach (IXpsFixedDocumentReader fixedDocumentReader in docSequenceToRead.FixedDocuments) { IXpsFixedDocumentWriter fixedDocumentWriter = docSeqWriter.AddFixedDocument(); AddStructure(fixedDocumentReader, fixedDocumentWriter); foreach (IXpsFixedPageReader fixedPageReader in fixedDocumentReader.FixedPages) { IXpsFixedPageWriter pageWriter = fixedDocumentWriter.AddFixedPage(); AddImages(fixedPageReader, pageWriter); AddFonts(fixedPageReader, pageWriter); AddContent(fixedPageReader, pageWriter); // Commmit all changes. pageWriter.Commit(); } fixedDocumentWriter.Commit(); } // Close the current source before openeing next one. docToRead.Close(); } docSeqWriter.Commit(); // Close newly created XPS document. document.Close(); blnResult = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } return(blnResult); }
public bool WriteDocument(List <byte[]> images, string file_out) { XpsDocument doc = null; IXpsFixedDocumentSequenceWriter docSeqWriter = null; IXpsFixedDocumentWriter docWriter = null; if (LogHelper.CanDebug()) { LogHelper.Begin("XpsHelper.WriteDocument"); } try { // xps doc does not manage overwrite, so delete before if exist if (File.Exists(file_out)) { File.Delete(file_out); } // create the document doc = new XpsDocument(file_out, FileAccess.ReadWrite, CompressionOption.NotCompressed); // Create the document sequence docSeqWriter = doc.AddFixedDocumentSequence(); // Create the document docWriter = docSeqWriter.AddFixedDocument(); for (int i = 0; i < images.Count; ++i) { // Create the Page IXpsFixedPageWriter pageWriter = docWriter.AddFixedPage(); // Get the XmlWriter XmlWriter xmlWriter = pageWriter.XmlWriter; //create the xps image resource XpsImage xpsImage = pageWriter.AddImage(XpsImageType.JpegImageType); using (Stream dstImageStream = xpsImage.GetStream()) { dstImageStream.Write(images[i], 0, images[i].Length); xpsImage.Commit(); } //this is just to get the real WPF image size as WPF display units and not image pixel size !! using (MemoryStream ms = new MemoryStream(images[i])) { BitmapImage myImage = new BitmapImage(); myImage.BeginInit(); myImage.CacheOption = BitmapCacheOption.OnLoad; myImage.StreamSource = ms; myImage.EndInit(); //write the page WritePageContent(xmlWriter, xpsImage, myImage.Width, myImage.Height); } //with the first page, create the thumbnail of the xps document if (i == 0) { XpsThumbnail thumb = doc.AddThumbnail(XpsImageType.JpegImageType); using (MemoryStream ms = new MemoryStream(images[i])) { BitmapImage thumbImage = new BitmapImage(); thumbImage.BeginInit(); thumbImage.DecodePixelWidth = 256; thumbImage.CacheOption = BitmapCacheOption.OnLoad; thumbImage.StreamSource = ms; thumbImage.EndInit(); using (Stream xpsThumbStream = thumb.GetStream()) { JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(thumbImage)); encoder.Save(xpsThumbStream); } } thumb.Commit(); } xmlWriter.Flush(); // Close the page pageWriter.Commit(); } return(true); } catch (Exception err) { LogHelper.Manage("XpsHelper.WriteDocument", err); return(false); } finally { if (docWriter != null) { docWriter.Commit(); } if (docSeqWriter != null) { docSeqWriter.Commit(); } if (doc != null) { doc.Close(); } LogHelper.End("XpsHelper.WriteDocument"); } }
/// <summary> /// Creates a new XPS Diagram from scratch /// </summary> /// <param name="fileName">Name of the file. Important: If the file already exists, it will be deleted</param> /// <param name="format">Format of the page</param> public XpsDocumentHelper(string fileName, PageFormat format) { // Initialize teh Dictionaries for fonts and images _embeddedFonts = new Dictionary<string, Uri>(); _embeddedImages = new Dictionary<string, ImageInfo>(); // The file is created from scratch so if it already exists it will be removed. if (File.Exists(fileName)) File.Delete(fileName); // Create the new package with all internal things that are required _package = Package.Open(fileName); _document = new XpsDocument(_package); _xpsDocSeqWriter = _document.AddFixedDocumentSequence(); _xpsDocWriter = _xpsDocSeqWriter.AddFixedDocument(); _xpsPageWriter = _xpsDocWriter.AddFixedPage(); _xmlWriter = _xpsPageWriter.XmlWriter; // Write the document header, I fixed everything to the the en-US culture here. StartPage(format); }
private bool CreateNewXPSFromSource() { bool blnResult = false; try { // Delete the old file generated by our code. if (File.Exists(destinationXpsPath)) { File.Delete(destinationXpsPath); } // Creating the output XPS file. XpsDocument document = new XpsDocument(destinationXpsPath, FileAccess.ReadWrite, System.IO.Packaging.CompressionOption.Maximum); IXpsFixedDocumentSequenceWriter docSeqWriter = document.AddFixedDocumentSequence(); // Loading the source xps files to list to read them for edit. List <string> sourceFiles = new List <string>(); sourceFiles.Add(sourceXPS); // Looping each source files. foreach (string sourceFile in sourceFiles) { XpsDocument docToRead = new XpsDocument(sourceFile, FileAccess.ReadWrite); IXpsFixedDocumentSequenceReader docSequenceToRead = docToRead.FixedDocumentSequenceReader; foreach (IXpsFixedDocumentReader fixedDocumentReader in docSequenceToRead.FixedDocuments) { IXpsFixedDocumentWriter fixedDocumentWriter = docSeqWriter.AddFixedDocument(); AddStructure(fixedDocumentReader, fixedDocumentWriter); foreach (IXpsFixedPageReader fixedPageReader in fixedDocumentReader.FixedPages) { IXpsFixedPageWriter pageWriter = fixedDocumentWriter.AddFixedPage(); AddImages(fixedPageReader, pageWriter); AddFonts(fixedPageReader, pageWriter); AddContent(fixedPageReader, pageWriter); // Commmit all changes. pageWriter.Commit(); } fixedDocumentWriter.Commit(); } // Close the current source before openeing next one. docToRead.Close(); } docSeqWriter.Commit(); // Show the modified content in a DocumentViewer. dvModifiedXPS.Document = document.GetFixedDocumentSequence(); ModifiedGrid.Visibility = System.Windows.Visibility.Visible; // Close newly created XPS document. document.Close(); blnResult = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } return(blnResult); }