private Public.ImageStream RenderXps() { Public.ImageStream renderedImage = new Public.ImageStream(); System.IO.MemoryStream renderedContent = new System.IO.MemoryStream(); String contentUriName = ("memorystream://rendered" + Guid.NewGuid().ToString().Replace("-", "") + ".xps"); Uri contentUri = new Uri(contentUriName, UriKind.Absolute); // FOR TESTING, OUTPUT TO DISK FILE BY CHANGING OPEN FROM MEMORY STREAM TO FILE LOCATION System.IO.Packaging.Package xpsPackage = System.IO.Packaging.Package.Open(renderedContent, System.IO.FileMode.Create); System.IO.Packaging.PackageStore.AddPackage(contentUri, xpsPackage); System.Windows.Xps.Packaging.XpsDocument renderedXpsDocument = new System.Windows.Xps.Packaging.XpsDocument(xpsPackage, System.IO.Packaging.CompressionOption.Normal, contentUriName); System.Windows.Xps.XpsDocumentWriter xpsWriter = System.Windows.Xps.Packaging.XpsDocument.CreateXpsDocumentWriter(renderedXpsDocument); System.Windows.Xps.Packaging.IXpsFixedDocumentSequenceWriter contentSequenceWriter; contentSequenceWriter = renderedXpsDocument.AddFixedDocumentSequence(); List <System.Windows.Xps.Packaging.XpsDocument> xpsContents = new List <System.Windows.Xps.Packaging.XpsDocument> (); // LOAD CORRESPONDENCE RECORD TO SEE IF THERE IS ANY CONTENT AVAILABLE Reference.Correspondence renderCorrespondence = application.CorrespondenceGet(correspondenceId); if (renderCorrespondence == null) { throw new ApplicationException("Unable to load base Correspondence to Render Content."); } if (renderCorrespondence.Content.Count == 0) { throw new ApplicationException("No Content to Render for Correspondence."); } renderCorrespondence.LoadContentAttachments(); foreach (Reference.CorrespondenceContent currentCorrespondenceContent in renderCorrespondence.Content.Values) { System.Windows.Xps.Packaging.XpsDocument xpsContent = null; switch (currentCorrespondenceContent.ContentType) { case Reference.Enumerations.CorrespondenceContentType.Report: #region Generate Report Content Reporting.ReportingServer reportingServer = application.ReportingServerGet(currentCorrespondenceContent.ReportingServerId); if (reportingServer == null) { throw new ApplicationException("Unable to load Reporting Server to Render Content."); } System.Reflection.Assembly reportingServerAssembly = System.Reflection.Assembly.LoadFrom(reportingServer.AssemblyReference); Type reportingServerType = reportingServerAssembly.GetType(reportingServer.AssemblyClassName); if (reportingServerType == null) { throw new ApplicationException("Unable to find Class [" + reportingServer.AssemblyClassName + "] in referenced Assembly [" + reportingServer.AssemblyReference + "]."); } Public.Reporting.IReportingServer reportingServerObject = (Public.Reporting.IReportingServer)Activator.CreateInstance(reportingServerType); Dictionary <String, String> reportParameters = new Dictionary <String, String> (); reportParameters.Add("entityCorrespondenceId", id.ToString()); // SSRS RENDER TIFF, CONVERT TO XPS Public.ImageStream imageStream = reportingServerObject.Render(reportingServer.WebServiceHostConfiguration, currentCorrespondenceContent.ReportName, reportParameters, "image", reportingServer.ExtendedProperties); xpsContent = imageStream.TiffToXps(); xpsContents.Add(xpsContent); #endregion break; case Reference.Enumerations.CorrespondenceContentType.Attachment: #region Load Attachment contentUriName = ("memorystream://attachment" + Guid.NewGuid().ToString().Replace("-", "") + ".xps"); contentUri = new Uri(contentUriName, UriKind.Absolute); System.IO.MemoryStream attachmentStream = new System.IO.MemoryStream(); System.IO.Packaging.Package attachmentPackage = System.IO.Packaging.Package.Open(currentCorrespondenceContent.Attachment); System.IO.Packaging.PackageStore.AddPackage(contentUri, attachmentPackage); xpsContent = new System.Windows.Xps.Packaging.XpsDocument(attachmentPackage, System.IO.Packaging.CompressionOption.Normal, contentUriName); xpsContents.Add(xpsContent); #endregion break; } } #region Merge XPS Contents foreach (System.Windows.Xps.Packaging.XpsDocument currentContentDocument in xpsContents) { foreach (System.Windows.Xps.Packaging.IXpsFixedDocumentReader currentContentDocumentReader in currentContentDocument.FixedDocumentSequenceReader.FixedDocuments) { System.Windows.Xps.Packaging.IXpsFixedDocumentWriter contentDocumentWriter = contentSequenceWriter.AddFixedDocument(); foreach (System.Windows.Xps.Packaging.IXpsFixedPageReader currentContentPageReader in currentContentDocumentReader.FixedPages) { System.Windows.Xps.Packaging.IXpsFixedPageWriter contentPageWriter = contentDocumentWriter.AddFixedPage(); System.Xml.XmlWriter xmlPageWriter = contentPageWriter.XmlWriter; String pageContent = CommonFunctions.XmlReaderToString(currentContentPageReader.XmlReader); #region Resource Dictionaries foreach (System.Windows.Xps.Packaging.XpsResourceDictionary currentXpsDictionary in currentContentPageReader.ResourceDictionaries) { System.Windows.Xps.Packaging.XpsResourceDictionary xpsDictionary = contentPageWriter.AddResourceDictionary(); System.IO.Stream xpsDictionaryStream = xpsDictionary.GetStream(); // GET DESTINATION STREAM TO COPY TO currentXpsDictionary.GetStream().CopyTo(xpsDictionaryStream); // REMAP SOURCE URI pageContent = pageContent.Replace(currentXpsDictionary.Uri.ToString(), xpsDictionary.Uri.ToString()); xpsDictionary.Commit(); } #endregion #region Color Contexts foreach (System.Windows.Xps.Packaging.XpsColorContext currentXpsColorContext in currentContentPageReader.ColorContexts) { System.Windows.Xps.Packaging.XpsColorContext xpsColorContext = contentPageWriter.AddColorContext(); System.IO.Stream xpsColorContextStream = xpsColorContext.GetStream(); // GET DESTINATION STREAM TO COPY TO currentXpsColorContext.GetStream().CopyTo(xpsColorContextStream); // REMAP SOURCE URI pageContent = pageContent.Replace(currentXpsColorContext.Uri.ToString(), xpsColorContext.Uri.ToString()); xpsColorContext.Commit(); } #endregion #region Fonts foreach (System.Windows.Xps.Packaging.XpsFont currentXpsFont in currentContentPageReader.Fonts) { System.Windows.Xps.Packaging.XpsFont xpsFont = contentPageWriter.AddFont(false); xpsFont.IsRestricted = false; System.IO.MemoryStream deobfuscatedStream = new System.IO.MemoryStream(); currentXpsFont.GetStream().CopyTo(deobfuscatedStream); String fontResourceName = currentXpsFont.Uri.ToString(); fontResourceName = fontResourceName.Split('/')[fontResourceName.Split('/').Length - 1]; if (fontResourceName.Contains(".odttf")) { fontResourceName = fontResourceName.Replace(".odttf", String.Empty); Guid fontGuid = new Guid(fontResourceName); deobfuscatedStream = CommonFunctions.XmlFontDeobfuscate(currentXpsFont.GetStream(), fontGuid); } System.IO.Stream xpsFontStream = xpsFont.GetStream(); // GET DESTINATION STREAM TO COPY TO deobfuscatedStream.CopyTo(xpsFontStream); // REMAP SOURCE URI pageContent = pageContent.Replace(currentXpsFont.Uri.ToString(), xpsFont.Uri.ToString()); xpsFont.Commit(); } #endregion #region Images foreach (System.Windows.Xps.Packaging.XpsImage currentXpsImage in currentContentPageReader.Images) { // FILE EXTENSION TO DETERMINE IMAGE TYPE System.Windows.Xps.Packaging.XpsImageType imageType = System.Windows.Xps.Packaging.XpsImageType.TiffImageType; String fileExtension = currentXpsImage.Uri.ToString().Split('.')[1]; switch (fileExtension.ToLower()) { case "jpeg": case "jpg": imageType = System.Windows.Xps.Packaging.XpsImageType.JpegImageType; break; case "png": imageType = System.Windows.Xps.Packaging.XpsImageType.PngImageType; break; case "wdp": imageType = System.Windows.Xps.Packaging.XpsImageType.WdpImageType; break; case "tif": case "tiff": default: imageType = System.Windows.Xps.Packaging.XpsImageType.TiffImageType; break; } System.Windows.Xps.Packaging.XpsImage xpsImage = contentPageWriter.AddImage(imageType); System.IO.Stream xpsImageStream = xpsImage.GetStream(); // GET DESTINATION STREAM TO COPY TO currentXpsImage.GetStream().CopyTo(xpsImageStream); // REMAP SOURCE URI pageContent = pageContent.Replace(currentXpsImage.Uri.ToString(), xpsImage.Uri.ToString()); // xpsImage.Uri = currentXpsImage.Uri; xpsImage.Commit(); } #endregion // COPY XAML CONTENT xmlPageWriter.WriteRaw(pageContent); contentPageWriter.Commit(); } contentDocumentWriter.Commit(); } } #endregion contentSequenceWriter.Commit(); renderedXpsDocument.Close(); xpsPackage.Close(); renderedImage.Image = renderedContent; renderedImage.Name = "EntityCorrespondence" + id.ToString() + ".xps"; renderedImage.Extension = "xps"; renderedImage.MimeType = "application/vnd.ms-xpsdocument"; renderedImage.IsCompressed = false; return(renderedImage); }
public System.Windows.Xps.Packaging.XpsDocument TiffToXps() { System.Drawing.Image[] tiffPages = TiffPages(); // INITIALIZE XPS System.IO.MemoryStream xpsStream = new System.IO.MemoryStream(); System.IO.Packaging.Package xpsPackage = System.IO.Packaging.Package.Open(xpsStream, System.IO.FileMode.Create); System.Windows.Xps.Packaging.XpsDocument xpsDocument = new System.Windows.Xps.Packaging.XpsDocument(xpsPackage); xpsDocument.Uri = new Uri("http://www.quebesystems.com", UriKind.Absolute); System.Windows.Xps.XpsDocumentWriter xpsWriter = System.Windows.Xps.Packaging.XpsDocument.CreateXpsDocumentWriter(xpsDocument); // SET UP XPS DOCUMENT AND FIRST FIXED DOCUMENT System.Windows.Xps.Packaging.IXpsFixedDocumentSequenceWriter xpsFixedDocumentSequenceWriter = xpsDocument.AddFixedDocumentSequence(); System.Windows.Xps.Packaging.IXpsFixedDocumentWriter xpsFixedDocumentWriter = xpsFixedDocumentSequenceWriter.AddFixedDocument(); // WRITE TIFF IMAGES AS PAGES IN XPS foreach (System.Drawing.Image currentTiffPage in tiffPages) { // ADD A NEW PAGE, THEN EMBED IMAGE TO THE PAGE System.Windows.Xps.Packaging.IXpsFixedPageWriter xpsFixedPageWriter = xpsFixedDocumentWriter.AddFixedPage(); System.Windows.Xps.Packaging.XpsImage xpsImage = xpsFixedPageWriter.AddImage(System.Windows.Xps.Packaging.XpsImageType.TiffImageType); System.IO.Stream xpsImageStream = xpsImage.GetStream(); // GET DESTINATION STREAM TO COPY TO // COPY TIFF IMAGE TO XPS IMAGE currentTiffPage.Save(xpsImageStream, System.Drawing.Imaging.ImageFormat.Tiff); xpsImage.Commit(); // IMAGE IS EMBED, BUT PAGE HAS NOT BEEN CREATED, CREATE PAGE System.Xml.XmlWriter xmlPageWriter = xpsFixedPageWriter.XmlWriter; xmlPageWriter.WriteStartElement("FixedPage"); // XPS PAGE STARTS WITH A FIXED PAGE TAG xmlPageWriter.WriteAttributeString("xmlns", "http://schemas.microsoft.com/xps/2005/06"); xmlPageWriter.WriteAttributeString("xml:lang", "en-US"); xmlPageWriter.WriteAttributeString("Width", currentTiffPage.Width.ToString()); xmlPageWriter.WriteAttributeString("Height", currentTiffPage.Height.ToString()); xmlPageWriter.WriteStartElement("Path"); xmlPageWriter.WriteAttributeString("Data", "M 0,0 H " + currentTiffPage.Width.ToString() + " V " + currentTiffPage.Height.ToString() + " H 0 z"); xmlPageWriter.WriteStartElement("Path.Fill"); xmlPageWriter.WriteStartElement("ImageBrush"); xmlPageWriter.WriteAttributeString("TileMode", "None"); xmlPageWriter.WriteAttributeString("ViewboxUnits", "Absolute"); xmlPageWriter.WriteAttributeString("ViewportUnits", "Absolute"); xmlPageWriter.WriteAttributeString("Viewbox", "0, 0, " + currentTiffPage.Width.ToString() + ", " + currentTiffPage.Height.ToString()); xmlPageWriter.WriteAttributeString("Viewport", "0, 0, " + currentTiffPage.Width.ToString() + ", " + currentTiffPage.Height.ToString()); xmlPageWriter.WriteAttributeString("ImageSource", xpsImage.Uri.ToString()); xmlPageWriter.WriteEndElement(); // IMAGE BRUSH xmlPageWriter.WriteEndElement(); // PATH.FILL xmlPageWriter.WriteEndElement(); // PATH // PAGE END ELEMENT xmlPageWriter.WriteEndElement(); // FIXED PAGE // PAGE COMMIT xpsFixedPageWriter.Commit(); } // COMMIT DOCUMENT WRITER xpsFixedDocumentWriter.Commit(); xpsFixedDocumentSequenceWriter.Commit(); xpsDocument.Close(); xpsPackage.Close(); //System.IO.FileStream fileStream = new System.IO.FileStream (@"C:\MERCURY\TEST7.XPS", System.IO.FileMode.Create); //fileStream.Write (xpsStream.ToArray (), 0, Convert.ToInt32 (xpsStream.Length)); //fileStream.Flush (); //fileStream.Close (); String contentUriName = ("memorystream://content" + Guid.NewGuid().ToString().Replace("-", "") + ".xps"); Uri contentUri = new Uri(contentUriName, UriKind.Absolute); System.IO.Packaging.Package attachmentPackage = System.IO.Packaging.Package.Open(xpsStream); System.IO.Packaging.PackageStore.AddPackage(contentUri, attachmentPackage); System.Windows.Xps.Packaging.XpsDocument xpsContent = new System.Windows.Xps.Packaging.XpsDocument(attachmentPackage, System.IO.Packaging.CompressionOption.Normal, contentUriName); return(xpsContent); }