I am an intermediate developer using .NET v3.5 C# (VS 2008). For my project, I have been working on Reportviewer with RDLC and ASHX control files. I use a print-preview page with iframe, it loads reportviewer, and it lets user decide whether he wants to output the file into PDF or Excel format.
Now my boss wants me to add watermark with text & image attached to the background of PDF file. I've written a small GetWatermark.ashx file that will read the current login-user info, turn it into hash, and draw into an image(MIMEType:image/gif). However I am having trouble linking this file to the RDLC report
I've read thru and tried all combinations with the
Microsoft Developer Network: Adding Images to a Report (Visual Studio Report Designer)
In GetWatermark.ashx: (I've tested by opening it up in IE and it shows a gif image just fine~)
public class GetWatermark : IHttpHandler, IRequiresSessionState {
public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "image/gif";
Bitmap b = new Bitmap(w, h);
Graphic c = Graphic.FromImage(b);
...
(some drawing with c skipped)
...
b.Save(context.Response.OutputStream, ImageFormat.Gif);
}
}
In Report.rdlc:
- Source = External
- Value = "../GetWatermark.ashx"
[PROBLEM SOLVED BY using actual Http://localhost:port/GetWatermark.ashx]
- MIMEType = image/gif
- BackgroundRepeat = Repeat
In Report.ashx
public void ProcessRequest(Http context) {
ReportFormat reportFormat = PageParam.ExistParam(context, "excel") ?
ReprotFormat.Excel : ReportReportFormat.PDF;
ExportReport(context,
PageParam.GetParam<string>(context, "id"),
reportFormat);
}
public void ExportReport(HttpContext context, string userID, ReportFormat) {
ReportViewer viewer = context.CreatViewer("Report.rdlc");
viewer.LocalReport.EnableExternalImage = true;
...
string name;
object datasourceValue;
(some code here that grabs stuff from the database based on userID)
...
viewer.LocalReport.DataSource.Add(new ReportDatasource(name, new object[] { dataSourceValue } ));
...
(some declarations skipped)
...
byte[] bytes = viewer.LocalReportRender(reportFormat.ToString(), null, out mimeType, out encoding, out extension, out streamIDs, out warnings);
...
(some pre-defined method that reads bytes and download the file into PDF/Excel)
}
public bool IsReusable { get { return false; } }
The report is working fine, but I cannot get the image to show... Need help finding out how to link the image or another way to draw watermark~ thanks in advance~