Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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~)
C#
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
C#
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~
Posted
Updated 12-Apr-15 21:18pm
v2

1 solution

 
Share this answer
 
Comments
Danny828656 27-Mar-15 2:00am    
Thanks for the reply, but my company does not allow the use of any third party libraries even if it's shareware/freeware. I wish it was that simple... >_<
Krishna Rpr 7-Apr-15 2:57am    
iTextSharp is free only. its act as like dll. So no prblm for that. Otherwise if it's Web application just use css
Danny828656 13-Apr-15 2:45am    
thanks again for the reply, but my developing station is a separate computer that runs on its own separate physical network disconnected to the internet and monitored/recorded by a remote admin. it cannot be access from internet/usb/or anything other than a mouse and keyboard (all files must be approved by manager and provide by the admin). If I absolutely have to use iTextSharp, then it'd probably be easier to reverse engineer it into my own codes.

Anywayz, I've already found the solution to my problem... I just had to use actual "http://localhost:port/GetWatermark.ashx" and not the "../GetWatermark.ashx" because RDLC doesn't recognize relative address codes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900