2011-02-02 13 views
0

我想在報告服務報告的每個頁面上添加包含corperate標識信息和圖形的PDF文件。此報告是從Dynamics CRM的報告查看器中呈現的。我正在研究編寫一個渲染擴展,它調用現有的PDF導出渲染擴展,並使用PDF組件如ABCPdf。在將報告服務發送給客戶端之前,修改其提供的pdf

我知道如何讓PDF背景渲染完成,但我無法從新的實現中調用現有的渲染擴展。

現有的PDF渲染擴展是一個密封的類,它似乎不可能從另一個實現中使用它的渲染方法。

然後我試圖直接使用http請求調用報表服務器,但由於模擬問題,我無法使其工作。我收到以下錯誤消息:「沒有Microsoft Dynamics CRM用戶存在指定的域名和用戶ID」

我認爲我處於正確的軌道上,但我不知道如何從上下文中獲取安全憑證一個渲染擴展。

如果我只能得到PDF的內容作爲一個流,我將能夠添加一個背景。

public class PdfWithBackgroundRenderer : IRenderingExtension 
    { 


     public void GetRenderingResource(Microsoft.ReportingServices.Interfaces.CreateAndRegisterStream createAndRegisterStreamCallback, System.Collections.Specialized.NameValueCollection deviceInfo) 
     { 

     } 

     public bool Render(Report report, System.Collections.Specialized.NameValueCollection reportServerParameters, System.Collections.Specialized.NameValueCollection deviceInfo, System.Collections.Specialized.NameValueCollection clientCapabilities, ref System.Collections.Hashtable renderProperties, Microsoft.ReportingServices.Interfaces.CreateAndRegisterStream createAndRegisterStream) 
     { 
      //http://localhost/Reserved.ReportViewerWebControl.axd?ReportSession=3i414gm5fcpwm355u1ek0e3g&ControlID=417ac5edf4914b7a8e22cf8d4c7a3a8d&Culture=1043&UICulture=1033&ReportStack=1&OpType=Export&FileName=%7bF64F053A-5C28-E011-A676-000C2981D884%7d&ContentDisposition=OnlyHtmlInline&Format=PDF 
      string Url = "http://localhost/Reserved.ReportViewerWebControl.axd"; 
      Url += "?ExecutionID=" + reportServerParameters["SessionID"]; 
      Url += "&ControlID=" + Guid.Empty; 
      Url += "&Culture=1043"; 
      Url += "&UICulture=1033"; 
      Url += "&ReportStack=1"; 
      Url += "&OpType=Export"; 
      Url += "&FileName=" + report.Name; 
      Url += "&ContentDisposition=OnlyHtmlInline"; 
      Url += "&Format=PDF";    

      Stream outputStream = createAndRegisterStream(report.Name, "pdf", Encoding.UTF8, "application/pdf", true, Microsoft.ReportingServices.Interfaces.StreamOper.CreateAndRegister); 
      StreamWriter writer = new StreamWriter(outputStream);    
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 
      request.Credentials = System.Net.CredentialCache.DefaultCredentials;    

      request.Timeout = 20000; 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

      StreamReader reader = new StreamReader(response.GetResponseStream()); 
      writer.Write(reader.ReadToEnd());  
      writer.Flush(); 

      return false; 
     } 

     public bool RenderStream(string streamName, Report report, System.Collections.Specialized.NameValueCollection reportServerParameters, System.Collections.Specialized.NameValueCollection deviceInfo, System.Collections.Specialized.NameValueCollection clientCapabilities, ref System.Collections.Hashtable renderProperties, Microsoft.ReportingServices.Interfaces.CreateAndRegisterStream createAndRegisterStream) 
     { 
      return false; 
     } 

     public string LocalizedName 
     { 
      get { return "PDF Renderer with background"; } 
     } 

     public void SetConfiguration(string configuration) 
     { 

     } 
    } 

回答

1

有一種方法,它只需要幾行。 這個想法是從一個新的渲染擴展中調用現有的渲染擴展的渲染方法。這會將渲染結果作爲可修改的流。

我在網上找不到解決方案,但發現一些論壇的答案,聲稱這不能實現。

困難的部分是讓你的腦袋圍繞使用的委託函數。一旦你明白髮生了什麼,解決方案就變得簡單了。

此代碼已通過Reporting Services 2008測試。如果要修改現有SSRS呈現擴展創建的呈現結果,此方法非常有用。您可以進行字符串替換,根據您的需要更改CSV輸出,或者在我們的案例中籤名或更新PDF文檔。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.ReportingServices.OnDemandReportRendering; 
using Microsoft.ReportingServices.Rendering.ImageRenderer; 
using System.Globalization; 
using System.IO; 
using System.Net; 

namespace ReportRendering.PDF 
{ 

    public class PdfWithBackgroundRenderer : IRenderingExtension 
    { 
     //Add the existing rendering extension to the new class 
     //In this case PDfRenderer from the ImageRenderer namespace 
     private PDFRenderer pdfRenderer; 

     //Stream to which the intermediate report will be rendered 
     private Stream intermediateStream; 
     private string _name; 
     private string _extension; 
     private Encoding _encoding; 
     private string _mimeType; 
     private bool _willSeek; 
     private Microsoft.ReportingServices.Interfaces.StreamOper _operation; 

     //Inititate the existing renderer in the constructor 
     public PdfWithBackgroundRenderer() 
      : base() 
     { 
      pdfRenderer = new PDFRenderer(); 
     } 


     public void GetRenderingResource(Microsoft.ReportingServices.Interfaces.CreateAndRegisterStream createAndRegisterStreamCallback, 
      System.Collections.Specialized.NameValueCollection deviceInfo) 
     { 

     } 

     //New implementation of the CreateAndRegisterStream method 
     public Stream IntermediateCreateAndRegisterStream(string name, string extension, Encoding encoding, string mimeType, bool willSeek, Microsoft.ReportingServices.Interfaces.StreamOper operation) 
     { 
      _name = name; 
      _encoding = encoding; 
      _extension = extension; 
      _mimeType = mimeType; 
      _operation = operation; 
      _willSeek = willSeek; 
      intermediateStream = new MemoryStream(); 
      return intermediateStream; 
     } 

     public bool Render(Report report, 
      System.Collections.Specialized.NameValueCollection reportServerParameters, 
      System.Collections.Specialized.NameValueCollection deviceInfo, 
      System.Collections.Specialized.NameValueCollection clientCapabilities, 
      ref System.Collections.Hashtable renderProperties, 
      Microsoft.ReportingServices.Interfaces.CreateAndRegisterStream createAndRegisterStream) 
     { 

      //Call the render method of the intermediate rendering extension 
      pdfRenderer.Render(report, reportServerParameters, deviceInfo, clientCapabilities, ref renderProperties, new Microsoft.ReportingServices.Interfaces.CreateAndRegisterStream(IntermediateCreateAndRegisterStream)); 

      //Register stream for new rendering extension 
      Stream outputStream = createAndRegisterStream(_name, _extension, _encoding, _mimeType, _willSeek, _operation); 


      intermediateStream.Position = 0; 

      //put stream update code here 

      //Copy the stream to the outout stream 
      byte[] buffer = new byte[32768]; 

      while (true) { 
       int read = intermediateStream.Read(buffer, 0, buffer.Length); 
       if (read <= 0) break; 
       outputStream.Write(buffer, 0, read); 
      } 

      intermediateStream.Close(); 

      return false; 
     } 

     public bool RenderStream(string streamName, Report report, System.Collections.Specialized.NameValueCollection reportServerParameters, System.Collections.Specialized.NameValueCollection deviceInfo, System.Collections.Specialized.NameValueCollection clientCapabilities, ref System.Collections.Hashtable renderProperties, Microsoft.ReportingServices.Interfaces.CreateAndRegisterStream createAndRegisterStream) 
     { 
      return false; 
     } 

     public string LocalizedName 
     { 
      get { return "PDF Renderer with background"; } 
     } 

     public void SetConfiguration(string configuration) 
     { 
      pdfRenderer.SetConfiguration(configuration); 
     } 


    } 
} 
+0

威迪蒙丹請我的同事的博客對這一解決辦法:[鏈接](http://www.broes.nl/2011/02/pdf-watermarkbackground-rendering-extension-for-ssrs-part-1/) – jhoefnagels 2011-02-22 15:26:06

相關問題