We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

a cell with a link in a table

Thread ID:

Created:

Updated:

Platform:

Replies:

125514 Aug 22,2016 07:42 AM UTC Sep 9,2016 12:42 PM UTC Windows Forms 8
loading
Tags: PDF
Brian Baart
Asked On August 22, 2016 07:42 AM UTC

Hello,

I would like to have a table with a column set as a link to an external pdf file.
The code I have:

PdfLoadedDocument doc = new PdfLoadedDocument(_BeginBestand);

//Get first page from document
PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;

//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;

PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);

//Draw the text.
graphics.DrawString(Debiteur.ADRES, font, PdfBrushes.Black, new PointF(10, 120));

// Create a PdfLightTable.
PdfLightTable pdfLightTable = new PdfLightTable();

//Assign data source.
pdfLightTable.DataSource = Table;

# region Cell Styles

//Create Pdf pen for drawing broder
PdfPen borderPen = new PdfPen(PdfBrushes.DarkBlue);
borderPen.Width = 0;

//Create cell styles
PdfCellStyle altStyle = new PdfCellStyle();
altStyle.Font = font;
altStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(192, 201, 219));
altStyle.BorderPen = borderPen;

PdfCellStyle defStyle = new PdfCellStyle();
defStyle.Font = font;
defStyle.BackgroundBrush = PdfBrushes.White;
defStyle.BorderPen = borderPen;
defStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

PdfCellStyle headerStyle = new PdfCellStyle(font, PdfBrushes.White, PdfPens.DarkBlue);
headerStyle.BackgroundBrush = new PdfSolidBrush(Color.FromArgb(33, 67, 126));
headerStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

# endregion

# region Format Table
//Set table alternate row style
pdfLightTable.Style.AlternateStyle = altStyle;

//Set default style
pdfLightTable.Style.DefaultStyle = defStyle;

//Set header row style
pdfLightTable.Style.HeaderStyle = headerStyle;

//Show the header row
pdfLightTable.Style.ShowHeader = true;

//Repeate header in all the pages
pdfLightTable.Style.RepeatHeader = true;

//Set header data from column caption
pdfLightTable.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;

pdfLightTable.Style.BorderPen = borderPen;
pdfLightTable.Style.CellPadding = 2;
pdfLightTable.Columns[1].Width = 12;

# endregion


//Draw PdfLightTable.
pdfLightTable.Draw(graphics, new PointF(0,200));

//Save the document.
doc.Save(_EindBestand);

//Close the document
doc.Close(true);





Table is a datatable with a link in it:
column_01 = "A"
Column_02 = "http://google.nl"

Is it possible to tell the pdfLightTable that the second column is a clickable link?

Thank you in advance.
Brian


Sasi Kumar Sekar [Syncfusion]
Replied On August 25, 2016 09:51 AM UTC

Hi Brian, 
 
Thanks for contacting Syncfusion support, 
 
We have analyzed your query, we can able to achieve your requirements by using PdfTextWebLink in BeginCell Event. Kindly refer the below code snippet and sample for your requirements attached same in below link. 
 
Code Snippet: 
 
PdfLightTable pdfLightTable = new PdfLightTable(); 
pdfLightTable.DataSource = GetProductsDataSet(); 
pdfLightTable.BeginCellLayout += new BeginCellLayoutEventHandler(pdfLightTable_BeginCellLayout); 
 
BeginCellEvent: 
 
void pdfLightTable_BeginCellLayout(object sender, BeginCellLayoutEventArgs args) 
{ 
// Create the Text Web Link 
 PdfTextWebLink textLink = new PdfTextWebLink(); 
 textLink.Text = "Know more..."; 
 textLink.Brush = PdfBrushes.Black; 
 textLink.Font = smallFont; 
 textLink.DrawTextWebLink(args.Graphics, new PointF(X,Y)); 
} 
 
 
 
Please check through it and let me know your feedback, 
 
Regards, 
Sasi Kumar S. 


Brian Baart
Replied On August 26, 2016 12:48 PM UTC

Dear Sir,

Thank you for your help.
I have implemented the extra methode and called the eventhandler from the example you have provided.

            float x = args.Bounds.X;
            float y = args.Bounds.Y;
            float width = args.Bounds.Right;
            float height = args.Bounds.Bottom;


            try
            {
                if (args.Value.Contains("http"))
                {
                    // Create the Text Web Link
                    PdfTextWebLink textLink = new PdfTextWebLink();
                    textLink.Url = @"http://www.syncfusion.com/products/reporting-edition/pdf";
                    textLink.Text = "Know more...";
                    textLink.Brush = PdfBrushes.Black;

                    textLink.DrawTextWebLink(args.Graphics, new PointF(x, y));
                    //textLink.DrawTextWebLink(args.Graphics, new PointF(args.Bounds.X + 2 * (args.Bounds.Width / 3), args.Bounds.Y));

                }
            }
            catch (System.Exception ex)
            {
                ErrorDescription = ex.Message;
            }


The only problem I have now is that the line:

textLink.DrawTextWebLink(args.Graphics, new PointF(x, y));

throws a Object reference not set to an instance of an object.

Could I ask for your help, again?

Thanks in advance.

Brian

Chinnu Muniyappan [Syncfusion]
Replied On August 29, 2016 06:52 AM UTC

Hi Brain, 

Thank you for your update. 

The problem occurs due to the PdfTextWebLink font was not initialized. Could you please try the below code snippet to resolve this issue. 
                 PdfTextWebLink textLink = new PdfTextWebLink(); 
                 textLink.Url = args.Value; 
                 textLink.Text = "Know more..."; 
                 textLink.Brush = PdfBrushes.Black; 
                 PdfFont smallFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 5f); 
                 textLink.Font = smallFont; 
                 textLink.DrawTextWebLink(args.Graphics, new PointF(x, y)); 
 
Please let us know if you have any concern. 

Regards, 
Chinnu 


Brian Baart
Replied On August 29, 2016 07:53 AM UTC

Dear Sir,

Thank you for your time. But the font wasn't / isn't the problem, I already solved that problem.
The error is in the line:

textLink.DrawTextWebLink(args.Graphics, new PointF(x, y));

Which completely baffles me, there are no objects used which haven't been initialized


This is the method, I am using:

        private void pdfLightTable_BeginCellLayout(object sender, BeginCellLayoutEventArgs args)
        {

            float x = args.Bounds.X;
            float y = args.Bounds.Y;
            float width = args.Bounds.Right;
            float height = args.Bounds.Bottom;


            try
            {
                if (args.Value.Contains("http"))
                {
                    // Create the Text Web Link
                    PdfTextWebLink textLink = new PdfTextWebLink();
                    textLink.Url = @"http://www.syncfusion.com/products/reporting-edition/pdf";
                    textLink.Text = "Know more...";
                    textLink.Brush = PdfBrushes.Black;

                    textLink.DrawTextWebLink(args.Graphics, new PointF(x, y));
                    //textLink.DrawTextWebLink(args.Graphics, new PointF(args.Bounds.X + 2 * (args.Bounds.Width / 3), args.Bounds.Y));

                }
            }
            catch (System.Exception ex)
            {
                ErrorDescription = ex.Message;
                Debug.Print(ex.Message);
            }

        }

Again thank you for your time.
Brian

Praveenkumar H [Syncfusion]
Replied On August 30, 2016 02:27 PM UTC

Hi Brian, 

As we said earlier we have to set the font to the PdfTextWebLink, without font initialization it throws exception when draw the text web link annotation  textLink.DrawTextWebLink(args.Graphics, new PointF(x, y));  
Please try this and let me know your feedback. 

With Regards, 
Praveen 


Brian Baart
Replied On August 30, 2016 02:43 PM UTC

Dear Sir,

Thank you, if I had read you answer better I wouldn't have made this mistake. I removed the font line, which I shouldn't have done.
I see that know.
Again thank you for your time and patience.

Brian

Brian Baart
Replied On September 8, 2016 02:13 PM UTC

Dear Sir,

I keep having problems with the link.
If I use the table lay-out code:

                #region Cell Styles

                //Create Pdf pen for drawing broder
                PdfPen borderPen = new PdfPen(PdfBrushes.DarkBlue);
                borderPen.Width = 0;

                //Create cell styles
                PdfCellStyle altStyle = new PdfCellStyle();
                altStyle.Font = font;
                altStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(192, 201, 219));
                altStyle.BorderPen = borderPen;

                PdfCellStyle defStyle = new PdfCellStyle();
                defStyle.Font = font;
                defStyle.BackgroundBrush = PdfBrushes.White;
                defStyle.BorderPen = borderPen;
                defStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

                PdfCellStyle headerStyle = new PdfCellStyle(font, PdfBrushes.White, PdfPens.DarkBlue);
                headerStyle.BackgroundBrush = new PdfSolidBrush(Color.FromArgb(33, 67, 126));
                headerStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

                #endregion

                #region Format Table
                //Set table alternate row style
                pdfLightTable.Style.AlternateStyle = altStyle;

                //Set default style
                pdfLightTable.Style.DefaultStyle = defStyle;

                //Set header row style        
                pdfLightTable.Style.HeaderStyle = headerStyle;

                //Show the header row
                pdfLightTable.Style.ShowHeader = true;

                //Repeate header in all the pages
                pdfLightTable.Style.RepeatHeader = true;

                //Set header data from column caption
                pdfLightTable.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;

                pdfLightTable.Style.BorderPen = borderPen;
                pdfLightTable.Style.CellPadding = 2;
                pdfLightTable.Columns[1].Width = 12;

                #endregion

And then your code,
pdfLightTable.BeginCellLayout += new BeginCellLayoutEventHandler(pdfLightTable_BeginCellLayout);

the text doesn't become a link.
But if I comment the cell and table lay-out code the link get printed in the table but with a strange lay-out.

Is it possible to have both?
A clean table and a cell with a link?

Thank you in advance.
Brian

PS This is the complete code I am using:


        #region Methods


        public void AanmakenPDF()
        {

            try
            {

                PdfLoadedDocument doc = new PdfLoadedDocument(_BeginBestand);

                //Get first page from document
                PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;

                //Create PDF graphics for the page
                PdfGraphics graphics = page.Graphics;

                PdfFont fontAdres = new PdfStandardFont(PdfFontFamily.Helvetica, 12);

                //Draw the text.
                graphics.DrawString(Debiteur.ADRES, fontAdres, PdfBrushes.Black, new PointF(10, 120));


                PdfFont fontDJ = new PdfStandardFont(PdfFontFamily.Helvetica, 10);

                string DearJohn = "Beste " + Debiteur.PERSOON;
                //Draw the text.
                graphics.DrawString(DearJohn, fontDJ, PdfBrushes.Black, new PointF(10, 180));

                PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10);

                string Body = "Sinds " + _BeginDatum + " heeft U de volgende artikelen gekocht:";
                //Draw the text.
                graphics.DrawString(Body, font, PdfBrushes.Black, new PointF(20, 200));

                //Draw the text.
                graphics.DrawString(_AutodocForm, font, PdfBrushes.White, new PointF(1, 1));
                graphics.DrawString(Debiteur.EMAIL, font, PdfBrushes.White, new PointF(1, 10));
                graphics.DrawString(Debiteur.NAAM, font, PdfBrushes.WhiteSmoke, new PointF(1, 20));

                // Create a PdfLightTable.
                PdfLightTable pdfLightTable = new PdfLightTable();

                //Assign data source.

                DataTable AndereVolgorde = new DataTable();
                AndereVolgorde.Columns.Add("TOELATINGSNR");
                AndereVolgorde.Columns.Add("OMSCHRIJVING");
                AndereVolgorde.Columns.Add("LINK");

                foreach (DataRow rij in Table.Rows)
                {
                    DataRow _rij = AndereVolgorde.NewRow();
                    _rij["TOELATINGSNR"] = rij["TOELATINGSNR"];
                    _rij["OMSCHRIJVING"] = rij["OMSCHRIJVING"];
                    _rij["LINK"] = rij["LINK"];
                    AndereVolgorde.Rows.Add(_rij);
                   
                }

                pdfLightTable.DataSource = AndereVolgorde;


                #region Cell Styles

                //Create Pdf pen for drawing broder
                PdfPen borderPen = new PdfPen(PdfBrushes.DarkBlue);
                borderPen.Width = 0;

                //Create cell styles
                PdfCellStyle altStyle = new PdfCellStyle();
                altStyle.Font = font;
                altStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(192, 201, 219));
                altStyle.BorderPen = borderPen;

                PdfCellStyle defStyle = new PdfCellStyle();
                defStyle.Font = font;
                defStyle.BackgroundBrush = PdfBrushes.White;
                defStyle.BorderPen = borderPen;
                defStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

                PdfCellStyle headerStyle = new PdfCellStyle(font, PdfBrushes.White, PdfPens.DarkBlue);
                headerStyle.BackgroundBrush = new PdfSolidBrush(Color.FromArgb(33, 67, 126));
                headerStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

                #endregion

                #region Format Table
                //Set table alternate row style
                pdfLightTable.Style.AlternateStyle = altStyle;

                //Set default style
                pdfLightTable.Style.DefaultStyle = defStyle;

                //Set header row style        
                pdfLightTable.Style.HeaderStyle = headerStyle;

                //Show the header row
                pdfLightTable.Style.ShowHeader = true;

                //Repeate header in all the pages
                pdfLightTable.Style.RepeatHeader = true;

                //Set header data from column caption
                pdfLightTable.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;

                pdfLightTable.Style.BorderPen = borderPen;
                pdfLightTable.Style.CellPadding = 2;
                pdfLightTable.Columns[1].Width = 12;

                #endregion

                pdfLightTable.BeginCellLayout += new BeginCellLayoutEventHandler(pdfLightTable_BeginCellLayout);


                //Draw PdfLightTable.
                pdfLightTable.Draw(graphics, new PointF(10, 200));

                //Save the document.
                doc.Save(_EindBestand);

                //Close the document
                doc.Close(true);

            }
            catch (System.Exception ex)
            {
                FoutOmschrijving = "Er is een fout opgetreden in de functie AanmakenPDF()." + System.Environment.NewLine + ex.Message;
                Debug.Print(ex.Message);
            }

        }


        private void pdfLightTable_BeginCellLayout(object sender, BeginCellLayoutEventArgs args)
        {

            float x = args.Bounds.X;
            float y = args.Bounds.Y;
            float width = args.Bounds.Right;
            float height = args.Bounds.Bottom;


            try
            {
                if (args.Value.Contains("http"))
                {
                    //// Create the Text Web Link
                    //PdfTextWebLink textLink = new PdfTextWebLink();
                    //textLink.Url = @"http://www.syncfusion.com/products/reporting-edition/pdf";
                    //textLink.Text = "Know more...";
                    //textLink.Brush = PdfBrushes.Black;

                    //PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
                    //textLink.Font = font;

                    //textLink.DrawTextWebLink(args.Graphics, new PointF(x, y));
                    ////textLink.DrawTextWebLink(args.Graphics, new PointF(args.Bounds.X + 2 * (args.Bounds.Width / 3), args.Bounds.Y));


                    PdfTextWebLink textLink = new PdfTextWebLink();
                    textLink.Url = args.Value;
                    textLink.Text =  " Klik hier: " + args.Value;
                    textLink.Brush = PdfBrushes.Black;
                    PdfFont smallFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 5f);
                    textLink.Font = smallFont;
                    textLink.DrawTextWebLink(args.Graphics, new PointF(x, y));
                   
                }
            }
            catch (System.Exception ex)
            {
                FoutOmschrijving = "Er is een fout opgetreden in de functie pdfLightTable_BeginCellLayout()." + System.Environment.NewLine + ex.Message;
                Debug.Print(ex.Message);
            }





        }


        #endregion

Sasi Kumar Sekar [Syncfusion]
Replied On September 9, 2016 12:42 PM UTC

Hi Brain, 
 
Thank you for contacting Syncfusion support, 
 
We have check your sample in our side, and we can able to reproduce the issue with” Web link is not enabled when adding PdfTextWeblink to PdfTable ” and we suspect this to be defect and created new incident for this. Please follow up the further updates in incident and find the below link to login the support account. 
 
 
Regards, 
Sasi Kumar S. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;