Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Jan 31, 2022 3:33 pm

Hi, I am generating a PDF from a grid layout and need to link to external url's and external documents.

I have tried using find and replace which is finding the text in the PDF but it is not being replaced with the link in the output.

The document is being passed as a memory stream to the users web browser.

Code: Select all

                Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
                Spire.Pdf.PdfPageSettings setting = new Spire.Pdf.PdfPageSettings();
                setting.Size = Spire.Pdf.PdfPageSize.A4;
                setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);
                Spire.Pdf.PdfPageBase page = doc.Pages.Add();

                Spire.Pdf.Grid.PdfGrid grid = new Spire.Pdf.Grid.PdfGrid();
                grid.Style.CellPadding = new Spire.Pdf.PdfPaddings(3, 0, 0, 0);
                grid.Columns.Add(4);
                float rowwidth = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);

// Add Rows and Cells -> all work OK and generates as required

                grid.Draw(page, new System.Drawing.PointF(0, lineY));

                Spire.Pdf.General.Find.PdfTextFind[] result = null;
                result = page.FindText("instructions", Spire.Pdf.General.Find.TextFindParameter.IgnoreCase).Finds;

                foreach (Spire.Pdf.General.Find.PdfTextFind find in result)
                {                   
                        PdfLaunchAction launchAction = new PdfLaunchAction(@"\\ROLFS03\ROL Public\Brief Builder\Reference\Booking-stamp-instructions3.docx", PdfFilePathType.Absolute);
                        System.Drawing.RectangleF rect = new System.Drawing.RectangleF(find.Position.X - doc.PageSettings.Margins.Left,
                            find.Position.Y - doc.PageSettings.Margins.Top, find.Size.Width, find.Size.Height);

                        PdfActionAnnotation annotation = new PdfActionAnnotation(rect, launchAction);
                        annotation.Border = new PdfAnnotationBorder(0);
                        (page as Spire.Pdf.PdfPageWidget).AnnotationsWidget.Add(annotation);
                }
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                doc.SaveToStream(ms);

bbarty61
 
Posts: 5
Joined: Mon Jan 31, 2022 3:23 pm

Tue Feb 01, 2022 6:54 am

Hello,

Thank you for your inquiry.
Please use the following modified code to achieve your needs.

Code: Select all
                Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
                Spire.Pdf.PdfPageSettings setting = new Spire.Pdf.PdfPageSettings();
                setting.Size = Spire.Pdf.PdfPageSize.A4;
                setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);
                Spire.Pdf.PdfPageBase page = doc.Pages.Add();

                Spire.Pdf.Grid.PdfGrid grid = new Spire.Pdf.Grid.PdfGrid();
                grid.Style.CellPadding = new Spire.Pdf.PdfPaddings(3, 0, 0, 0);
                grid.Columns.Add(4);
                float rowwidth = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);

// Add Rows and Cells -> all work OK and generates as required

                grid.Draw(page, new System.Drawing.PointF(0, lineY));

                Spire.Pdf.General.Find.PdfTextFind[] result = null;
                result = page.FindText("instructions", Spire.Pdf.General.Find.TextFindParameter.IgnoreCase).Finds;

                foreach (Spire.Pdf.General.Find.PdfTextFind find in result)
                {                   
                        PdfLaunchAction launchAction = new PdfLaunchAction(@"\\ROLFS03\ROL Public\Brief Builder\Reference\Booking-stamp-instructions3.docx", PdfFilePathType.Absolute);
                        System.Drawing.RectangleF rect = new System.Drawing.RectangleF(find.Position.X, find.Position.Y, find.Size.Width, find.Size.Height);

                        PdfActionAnnotation annotation = new PdfActionAnnotation(rect, launchAction);
                        annotation.Border = new PdfAnnotationBorder(0);
                        (page as Spire.Pdf.PdfPageWidget).AnnotationsWidget.Add(annotation);
                }
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                doc.SaveToStream(ms);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Tue Feb 01, 2022 9:24 am

Thanks for your reply.

The code threw the following error: '(page as Spire.Pdf.PdfPageWidget).AnnotationsWidget' threw an exception of type 'System.NullReferenceException'

Changing
(page as Spire.Pdf.PdfPageWidget).AnnotationsWidget.Add(annotation);
to
page.AnnotationsWidget.Add(annotation);

runs but doesn't seem to add the link to the text

bbarty61
 
Posts: 5
Joined: Mon Jan 31, 2022 3:23 pm

Tue Feb 01, 2022 9:49 am

Hello,

Thanks for your feedback.
Please save the PDF file after drawing the grid, then load the file and do other things.
Code: Select all
            ...
            grid.Draw(page, new PointF(10, 30));
            MemoryStream stream = new MemoryStream();
            // save as stream
            doc.SaveToStream(stream);

            doc.LoadFromStream(stream);
            page = doc.Pages[0];
            Spire.Pdf.General.Find.PdfTextFind[] result = null;
            ...

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Tue Feb 01, 2022 10:41 am

Hi,

It is not possible to save the file on the server that is generating it. is there any other way?

Thanks

bbarty61
 
Posts: 5
Joined: Mon Jan 31, 2022 3:23 pm

Wed Feb 02, 2022 7:38 am

Yes, please refer to the following mofied code.
Code: Select all
            Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
             //Spire.Pdf.PdfPageSettings setting = new Spire.Pdf.PdfPageSettings();
            Spire.Pdf.PdfPageSettings setting = doc.PageSettings ;
            setting.Size = Spire.Pdf.PdfPageSize.A4;
            setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);
            Spire.Pdf.PdfPageBase page = doc.Pages.Add();

            Spire.Pdf.Grid.PdfGrid grid = new Spire.Pdf.Grid.PdfGrid();
            grid.Style.CellPadding = new Spire.Pdf.PdfPaddings(3, 0, 0, 0);
            grid.Columns.Add(4);
            float rowwidth = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);

            // Add Rows and Cells -> all work OK and generates as required

            Spire.Pdf.General.Find.PdfTextFind[] result = null;
            result = page.FindText("instructions", Spire.Pdf.General.Find.TextFindParameter.IgnoreCase).Finds;

            foreach (Spire.Pdf.General.Find.PdfTextFind find in result)
            {
                PdfLaunchAction launchAction = new PdfLaunchAction(@"\\ROLFS03\ROL Public\Brief Builder\Reference\Booking-stamp-instructions3.docx", PdfFilePathType.Absolute);
                System.Drawing.RectangleF rect = new System.Drawing.RectangleF(find.Position.X - setting.Margins.Left, find.Position.Y - setting.Margins.Top, find.Size.Width, find.Size.Height);

                PdfActionAnnotation annotation = new PdfActionAnnotation(rect, launchAction);
                annotation.Border = new PdfAnnotationBorder(0);
                page.AnnotationsWidget.Add(annotation);
            }
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            doc.SaveToStream(ms);


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Wed Feb 02, 2022 4:03 pm

Thanks for your reply.

I have now got it to show the pointer as a hand in Edge (and change color of the annotation) in edge, however this is not working in Chrome.

do you have any suggestions? changing away from Chrome is not an option.
Thanks

bbarty61
 
Posts: 5
Joined: Mon Jan 31, 2022 3:23 pm

Thu Feb 03, 2022 9:37 am

Hello

Thanks for your feedback.
You mean that the hyperlink added to the pdf file by using our Spire.PDF cannot be recognized by Chrome, right? I am sorry that our Spire.PDF does not support the featur. Apologize for the inconvenience caused.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Fri Feb 04, 2022 11:11 am

Hi Brian,

Yes that appears to be correct.

bbarty61
 
Posts: 5
Joined: Mon Jan 31, 2022 3:23 pm

Mon Feb 07, 2022 2:31 am

Thanks for your response.
If you encounter any issues related to our products in the future, please feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF