private Public.ImageStream RenderContentReport(Reference.CorrespondenceContent correspondenceContent, String format)
        {
            Public.ImageStream reportImage = null;


            Reporting.ReportingServer reportingServer = application.ReportingServerGet(correspondenceContent.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());


            reportImage = reportingServerObject.Render(reportingServer.WebServiceHostConfiguration, correspondenceContent.ReportName, reportParameters, format, reportingServer.ExtendedProperties);

            return(reportImage);
        }
        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);
        }