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);
        }
        public Public.ImageStream Render()
        {
            Public.ImageStream renderedImage = RenderPdf();


            if (Correspondence.StoreImage)
            {
                // SAVE IMAGE TO DATABASE

                application.EnvironmentDatabase.BeginTransaction();

                String insertStatement = "IF NOT EXISTS (SELECT EntityCorrespondenceId FROM EntityCorrespondenceImage WHERE EntityCorrespondenceId = " + id.ToString() + ")";

                insertStatement += "  INSERT INTO dbo.EntityCorrespondenceImage (EntityCorrespondenceId, EntityCorrespondenceImageData, ";

                insertStatement += "    EntityCorrespondenceImageName, EntityCorrespondenceImageExtension, EntityCorrespondenceImageMimeType, EntityCorrespondenceImageIsCompressed)";

                insertStatement += " VALUES (" + id.ToString() + ", NULL, ";

                insertStatement += "'" + renderedImage.Name.Replace("'", "''") + "', ";

                insertStatement += "'" + renderedImage.Extension.Replace("'", "''") + "', ";

                insertStatement += "'" + renderedImage.MimeType.Replace("'", "''") + "', ";

                insertStatement += Convert.ToInt32(renderedImage.IsCompressed).ToString() + ")";

                application.EnvironmentDatabase.ExecuteSqlStatement(insertStatement);

                application.EnvironmentDatabase.BlobWrite("dbo.EntityCorrespondenceImage", "EntityCorrespondenceImageData", "EntityCorrespondenceId = " + id.ToString(), renderedImage.Image);

                application.EnvironmentDatabase.CommitTransaction();
            }


            return(renderedImage);
        }
        private Public.ImageStream RenderPdf()
        {
            Public.ImageStream renderedImage = new Public.ImageStream();

            System.IO.MemoryStream renderedContent = new System.IO.MemoryStream();


            PdfSharp.Pdf.PdfDocument renderedPdf = new PdfSharp.Pdf.PdfDocument();


            // 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)
            {
                PdfSharp.Pdf.PdfDocument pdfContent = null;


                switch (currentCorrespondenceContent.ContentType)
                {
                case Reference.Enumerations.CorrespondenceContentType.Report:

                    #region Generate Report Content

                    Public.ImageStream reportStream = RenderContentReport(currentCorrespondenceContent, "pdf");

                    pdfContent = PdfSharp.Pdf.IO.PdfReader.Open(reportStream.Image, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import);

                    #endregion

                    break;

                case Reference.Enumerations.CorrespondenceContentType.Attachment:

                    #region Load Attachment

                    pdfContent = PdfSharp.Pdf.IO.PdfReader.Open(currentCorrespondenceContent.Attachment, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import);

                    #endregion

                    break;
                }

                if (pdfContent != null)
                {
                    foreach (PdfSharp.Pdf.PdfPage currentPage in pdfContent.Pages)
                    {
                        renderedPdf.Pages.Add(currentPage);
                    }
                }
            }



            renderedPdf.Save(renderedContent, false);



            renderedImage.Image = renderedContent;

            renderedImage.Name = "EntityCorrespondence" + id.ToString() + ".pdf";

            renderedImage.Extension = "pdf";

            renderedImage.MimeType = "application/pdf";

            renderedImage.IsCompressed = false;

            return(renderedImage);
        }
        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 Boolean Fax(Faxing.FaxServer faxServer)
        {
            if (faxServer == null)
            {
                return(false);
            }

            Boolean sentSuccess = true;


            // SET AUTOMATION INFORMATION

            automationId = Guid.NewGuid();

            automationStatus = Automation.Enumerations.AutomationStatus.Open;

            automationDate = DateTime.Now;

            automationException = String.Empty;



            try {
                // SET UP FAX DOCUMENT

                Public.Faxing.IFaxServer faxServerObject;

                String faxAttention = ((!String.IsNullOrWhiteSpace(attention)) ? attention : application.EntityGet(entityId).Name);

                Public.Faxing.FaxSender faxSender = new Public.Faxing.FaxSender(application.Session.UserDisplayName, String.Empty, faxServer.SenderEmailAddress);

                faxSender.Email = faxSender.Email.Replace("%useraccountname%", application.Session.UserAccountName);

                Public.Faxing.FaxRecipient faxRecipient = new Public.Faxing.FaxRecipient(faxAttention, this.ContactFaxNumber);

                Public.Faxing.FaxDocument faxDocument;


                Public.ImageStream attachment = application.EntityCorrespondenceImageGet(id);

                if ((attachment.Image != null) ? (attachment.Image.Length == 0) : true)
                {
                    attachment = Render();
                }

                faxDocument = new Public.Faxing.FaxDocument(automationId.ToString(), correspondenceName, attachment);


                System.Reflection.Assembly faxServerAssembly = System.Reflection.Assembly.LoadFrom(faxServer.AssemblyReference);

                Type faxServerType = faxServerAssembly.GetType(faxServer.AssemblyClassName);

                if (faxServerType == null)
                {
                    throw new ApplicationException("Unable to find Class [" + faxServer.AssemblyClassName + "] in referenced Assembly [" + faxServer.AssemblyReference + "].");
                }

                faxServerObject = (Public.Faxing.IFaxServer)Activator.CreateInstance(faxServerType);

                faxServerObject.OnFaxCompleted += new EventHandler <Public.Faxing.FaxCompletedEventArgs> (Fax_OnFaxCompleted);

                faxServerObject.Fax(faxServer.FaxServerConfiguration, faxSender, faxRecipient, faxDocument, faxServer.ExtendedProperties);
            }

            catch (Exception applicationException) {
                application.SetLastException(applicationException);

                AutomationStatus = Automation.Enumerations.AutomationStatus.Critical;

                AutomationException = applicationException.Message;

                sentSuccess = false;
            }

            if (sentSuccess)
            {
                sentSuccess = Save();
            }

            return(sentSuccess);
        }
Exemple #6
0
        public Public.ImageStream Render(Public.WebService.WebServiceHostConfiguration webServiceHostConfiguration, String reportName, Dictionary <String, String> forReportParameters, String format, Dictionary <String, String> extendedProperties)
        {
            Public.ImageStream imageStream = new Public.ImageStream();

            Boolean deviceInfoHumanReadablePdf = ((extendedProperties.ContainsKey("DeviceInfoHumanReadablePdf")) ? Convert.ToBoolean(extendedProperties["DeviceInfoHumanReadablePdf"]) : false);


            #region Initialization

            // ENABLE IMPERSONATION FROM THREAD IDENTITY

            // AppDomain.CurrentDomain.SetPrincipalPolicy (System.Security.Principal.PrincipalPolicy.WindowsPrincipal);

            // System.Security.Principal.WindowsImpersonationContext impersonationContext = null;

            // impersonationContext = ((System.Security.Principal.WindowsIdentity)System.Threading.Thread.CurrentPrincipal.Identity).Impersonate ();


            // CONFIGURE REPORTING SERVICE HOST


            reportExecutionClient = new ReportExecution2005.ReportExecutionServiceSoapClient(webServiceHostConfiguration.BindingConfiguration.Binding, webServiceHostConfiguration.EndpointAddress);

            reportExecutionClient.ClientCredentials.Windows.AllowedImpersonationLevel = webServiceHostConfiguration.ClientCredentials.WindowsImpersonationLevel;

            reportExecutionClient.ClientCredentials.Windows.ClientCredential = webServiceHostConfiguration.ClientCredentials.Credentials;


            #region Old Connection Information

            //Server.Public.WebService.WebServiceHostConfiguration serviceHostReporting = new Server.Public.WebService.WebServiceHostConfiguration (

            //    "qstestmcm001", 80, "ReportServer", "ReportExecution2005.asmx"

            //    );

            //serviceHostReporting.ClientCredentials.WindowsImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

            //serviceHostReporting.BindingConfiguration = new Server.Public.WebService.BindingConfiguration ("BasicHttpBinding", Server.Public.WebService.Enumerations.WebServiceBindingType.BasicHttpBinding);

            //serviceHostReporting.BindingConfiguration.SecurityMode = System.ServiceModel.BasicHttpSecurityMode.TransportCredentialOnly;

            //serviceHostReporting.BindingConfiguration.TransportCredentialType = System.ServiceModel.HttpClientCredentialType.Ntlm;

            //serviceHostReporting.BindingConfiguration.MessageCredentialType = System.ServiceModel.MessageCredentialType.UserName;


            //System.ServiceModel.BasicHttpBinding binding = (System.ServiceModel.BasicHttpBinding)serviceHostReporting.BindingConfiguration.Binding;

            ////binding.AllowCookies = true;


            //reportExecutionClient = new ReportExecution2005.ReportExecutionServiceSoapClient (binding, serviceHostReporting.EndpointAddress);

            //reportExecutionClient.ClientCredentials.Windows.AllowedImpersonationLevel = serviceHostReporting.ClientCredentials.WindowsImpersonationLevel;

            //reportExecutionClient.ClientCredentials.Windows.ClientCredential = serviceHostReporting.ClientCredentials.Credentials;

            #endregion


            // OPEN UP THE MAX OBJECTS IN GRAPH TO MAXIMUM

            foreach (System.ServiceModel.Description.OperationDescription currentOperation in reportExecutionClient.Endpoint.Contract.Operations)
            {
                System.ServiceModel.Description.DataContractSerializerOperationBehavior dataContractSerializer =

                    (System.ServiceModel.Description.DataContractSerializerOperationBehavior)

                    currentOperation.Behaviors.Find <System.ServiceModel.Description.DataContractSerializerOperationBehavior> ();

                if (dataContractSerializer != null)
                {
                    dataContractSerializer.MaxItemsInObjectGraph = Int32.MaxValue;
                }
            }

            #endregion


            #region Render Report

            SqlServer.ReportExecution2005.ExecutionHeader executionHeader;

            SqlServer.ReportExecution2005.ServerInfoHeader serverInfoHeader;

            SqlServer.ReportExecution2005.ExecutionInfo executionInfo;

            SqlServer.ReportExecution2005.ParameterValue[] parameters = new ReportExecution2005.ParameterValue[forReportParameters.Count];

            Byte[] renderedReport;

            String reportExtension;

            String reportMimeType;

            String reportEncoding;

            SqlServer.ReportExecution2005.Warning[] reportWarnings;

            String[] reportStreamIds;


            executionHeader = reportExecutionClient.LoadReport(null, reportName, null, out serverInfoHeader, out executionInfo);


            // COPY PARAMETERS

            Int32 currentParameterIndex = 0;

            foreach (String currentParameterName in forReportParameters.Keys)
            {
                parameters[currentParameterIndex] = new ReportExecution2005.ParameterValue();

                parameters[currentParameterIndex].Label = currentParameterName;

                parameters[currentParameterIndex].Name = currentParameterName;

                parameters[currentParameterIndex].Value = forReportParameters[currentParameterName];

                currentParameterIndex = currentParameterIndex + 1;
            }

            reportExecutionClient.SetExecutionParameters(executionHeader, null, parameters, "en-us", out executionInfo);


            // SET UP DEVICE INFORMATION

            String deviceInfo = String.Empty;

            switch (format.ToLower())
            {
            case "pdf":

                deviceInfo = (deviceInfoHumanReadablePdf) ? @"<DeviceInfo><HumanReadablePDF>True</HumanReadablePDF></DeviceInfo>" : String.Empty;

                break;
            }


            serverInfoHeader = reportExecutionClient.Render(executionHeader, null, format, ((!String.IsNullOrEmpty(deviceInfo)) ? deviceInfo : null), out renderedReport, out reportExtension, out reportMimeType, out reportEncoding, out reportWarnings, out reportStreamIds);

            imageStream.Image = new System.IO.MemoryStream(renderedReport);

            imageStream.Name = reportName;

            imageStream.Extension = reportExtension;

            imageStream.MimeType = reportMimeType;

            #endregion


            return(imageStream);
        }