I'm using aspnet Document.Reports to create a PDF for download. How do I attach a hyperlink to an image?
Thanks
Abbott
Hello AbbottF,
Thank you for your question.
I am not sure how you create the PDF document, I will assume that you use dynamic approach.
My suggestion is to use CellExporting server event, and from there to modify the cell element content.
To be more specific AddContent Method can accept image and hyperlink data, I think this is what you are searching.
Sample is attached.
Code snippet:
protected void dExporter_CellExporting(object sender, Infragistics.Web.UI.GridControls.DocumentCellExportingEventArgs e){ if (e.GridCell != null && e.GridCell.Column.Key == "Test") { if (e.GridCell.Row.Index == 2) { Infragistics.Documents.Reports.Report.Text.Style bluelink = new Infragistics.Documents.Reports.Report.Text.Style(new Font("Aria", 8, FontStyle.Underline), Brushes.Blue);
string reff = e.GridCell.Value.ToString(); var regexHref = new Regex("<a [^>]*href=(?:'(?<href>.*?)')|(?:\"(?<href>.*?)\")", RegexOptions.IgnoreCase); var regexSrc = new Regex("<img [^>]*src=(?:'(?<src>.*?)')|(?:\"(?<src>.*?)\")", RegexOptions.IgnoreCase); var urls = regexHref.Matches(reff).OfType<Match>().Select(m => m.Groups["href"].Value).SingleOrDefault(); var srcs = regexSrc.Matches(reff).OfType<Match>().Select(m => m.Groups["src"].Value).SingleOrDefault(); Infragistics.Documents.Reports.Graphics.Image img = new Infragistics.Documents.Reports.Graphics.Image(HttpRuntime.AppDomainAppPath + srcs); e.ReportCellElement.AddText().AddContent(img, new Size(70, 70), Infragistics.Documents.Reports.Report.ImageAlignment.Middle, new Hyperlink(urls)); }
.....
I used one of your samples (the generated pdf is attached).
The code I use to insert the image is:
IBand band = reportSection.AddBand(); band.Height = new FixedHeight(100); band.Margins.Vertical = 5; band.Paddings.All = 5; // Here you can see how to create a gradient color background band.Background = new Background(new LinearGradientBrush(new Color(198, 212, 232), Colors.White, 90)); // Header IText text = band.AddText(); text.Alignment.Horizontal = Alignment.Right; if (customer.EMail == "") { text.AddContent( "YOUR SHOPPING CART", invoiceHeaderFontStyle); } else { text.AddContent( "YOUR SHOPPING CART", invoiceHeaderFontStyle, new Hyperlink(shoppingCartUri)); } // Here we add the IG logo System.Drawing.Image nativeImage = System.Drawing.Image.FromFile( HttpContext.Current.Server.MapPath( "/images/mktg/KboPromoHeader Small.png")); Infragistics.Documents.Reports.Graphics.Image image = new Infragistics.Documents.Reports.Graphics.Image(nativeImage, Colors.White); band.AddImage(image);
An additional question is - how do I keep the transparency of the png image?
Currently there is limitation of PDF in general when it comes to Alpha channels and transparency. Most of the PDF images have the same issue and therefore IG is not able to do much about it. The document engine will convert any Alpha channel values to white.
As for image configuration like borders, paddings, background etc. there is a interface IImage which can be used in order to "play around" with the image. For more information, have a look at the snippet below:
Infragistics.Documents.Reports.Graphics.Image img = new Infragistics.Documents.Reports.Graphics.Image(HttpRuntime.AppDomainAppPath + srcs);
IImage theImage = e.ReportCellElement.AddImage(img);theImage.SetTransparentColor(Infragistics.Documents.Reports.Graphics.Colors.Magenta.ToHex());theImage.Borders = new Borders(new Pen(new Color(System.Drawing.Color.Red)));
//e.ReportCellElement.AddText().AddContent(img, new Size(70, 70), Infragistics.Documents.Reports.Report.ImageAlignment.Middle, new Hyperlink(urls));
Helpful links:
https://www.igniteui.com/help/infragistics.web.mvc.documents.reports~infragistics.documents.reports.report.iimage~settransparentcolor
https://www.igniteui.com/help/infragistics.web.mvc.documents.reports~infragistics.documents.reports.report.section.isection~addimage
https://es.infragistics.com/community/forums/f/ultimate-ui-for-asp-net/1676/document-images