C # to add, modify, delete hyperlinks in PPT

This article describes how to add C # programming through hyperlinks in PPT slide way, when you add links to text or images can add a hyperlink, the link objects to a web page address, mailing address, specified slide, in addition, can also refer to text edit, delete existing method slide hyperlinks.

Program uses the library : Free Spire.Presentation for .NET (Free)

dll acquisition and references:

Method 1 : by downloading the official website of the package , extracting installer Bin folder to the specified path; After the installation, the installation file path adding reference Spire.Presentation.dll Bin folder to the program, and add instructions using .

Method 2 : by mounting Nuget introduced .

Dll add a reference to the following figure:

 

 

C # code sample

1. Add a hyperlink to PPT Slide

the using Spire.Presentation;
 the using Spire.Presentation.Drawing;
 the using the System.Drawing; 


namespace AddHyperlink 
{ 
    class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            // Initialization Presentation Examples of 
            Presentation PPT = new new Presentation (); 

            // add a slides as the second slide (when you create a document, by default generate a slide) 
            ppt.Slides.Append (); 

            // get the first one slide, and add shapes 
            ISlide slide1 = ppt.Slides [ 0 ]; 
            IAutoShape the ShapeSlide1.Shapes.AppendShape = (ShapeType.Rectangle, new new RectangleF ( 100 , 100 , 450 , 200 is )); 
            shape.Fill.FillType = FillFormatType.Solid; 
            shape.Fill.SolidColor.Color = Color.LightYellow; 
            shape.ShapeStyle. LineColor.Color = Color.White; 

            // declare a string variable 
            string s1 = " BIDU " ;
             string s2 = " is the world's largest Chinese search engine, China's largest information and knowledge at the core of the Internet service company, the world's leading AI platform companies. " ;
             StringS3 = " refer to the second page Introduction " ; 

            // Get the shape of paragraphs (a default blank paragraph) 
            TextParagraph paragraph = shape.TextFrame.TextRange.Paragraph; 
            paragraph.Alignment = TextAlignmentType.Left; 

            // Create a character string s1 tr1, and add in the text link to the web page address 
            TextRange tr1 = new new TextRange (s1); 
            tr1.ClickAction.Address = " https://www.baidu.com/ " ;
             // tr1.ClickAction.Address = "mailto : [email protected] "; // point to e-mail address 


            // create tr2 according to the word s2 
            TextRange tr2 = new newThe TextRange (S2); 

            // Create a character string tr3 s3, and add a link in the text, point to the second slide 
            the TextRange tr3 = new new the TextRange (S3); 
            ClickHyperlink Link = new new ClickHyperlink (ppt.Slides [ . 1 ]); 
            tr3.ClickAction = Link; 

            // add to paragraph TextRange 
            paragraph.TextRanges.Append (Tr1); 
            paragraph.TextRanges.Append (Tr2 is); 
            paragraph.TextRanges.Append (Tr3); 

            // set the paragraph style font 
            the foreach (TextRange TR in paragraph.TextRanges) 
            { 
                tr.LatinFont = new newTextFont ( " Song (Body) " ); 
                tr.FontHeight = 20f; 
                tr.IsBold = TriState.True; 
                tr.Fill.FillType = FillFormatType.Solid; 
                tr.Fill.SolidColor.Color = Color.Black; 
            } 


            // Get the second slide, add shapes, and added to the picture shape, and links to web page address 
            ISlide slide2 ppt.Slides = [ . 1 ]; 
            RectangleF RECT = new new RectangleF ( 250 , 175 , 195 , 130. ); 
            IEmbedImage image= slide2.Shapes.AppendEmbedImage(ShapeType.Rectangle, @"tp.png", rect);
            ClickHyperlink hyperlink = new ClickHyperlink("https://www.baidu.com/");
            image.Click = hyperlink;


            //保存文档
            ppt.SaveToFile("AddHyperlink.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("AddHyperlink.pptx");
        }
    }
}

To see the effect of adding a hyperlink in a slide show.

Add effects to text hyperlink:

Picture Hyperlinks add effects:

 

2. Edit, Delete Hyperlink PPT slide

the using Spire.Presentation; 

namespace ModifyHyperlink 
{ 
    class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            // Initialization Presentation Examples of 
            Presentation PPT = new new Presentation (); 

            // load existing documents 
            ppt.LoadFromFile ( " AddHyperlink.pptx " ); 

            // Get a first slide 
            ISlide slide ppt.Slides = [ 0 ]; 

            // traverse Shape 
            the foreach (the IShape Shape in slide.Shapes) 
            {
                // determines whether the autoshape 
                IF (shape IS IAutoShape) 
                { 
                    // The shape is converted to the autoshape 
                    IAutoShape shape autoshape = AS IAutoShape; 

                    // traversing the autoshape paragraph of 
                    the foreach (TP TextParagraph in autoShape.TextFrame.Paragraphs) 
                    { 
                        // Analyzing paragraph if they contain at TextRange 
                        iF (tp.TextRanges! = null && tp.TextRanges.Count> 0 ) 
                        { 
                            // iterate TextRange 
                            for ( int tpcount = 0; Tpcount <tp.TextRanges.Count; tpcount ++ ) 
                            { 
                                // determines whether it contains text and links and containing ClickAction 
                                IF (tp.TextRanges [tpcount] .ClickAction =! Null &&! String .IsNullOrWhiteSpace (tp.TextRanges [tpcount] .ClickAction .Address) &&! String .IsNullOrWhiteSpace (tp.TextRanges [tpcount] .Text)) 
                                { 
                                    // determines whether contain http or https link link 
                                    iF (tp.TextRanges [tpcount] .ClickAction.Address.ToLower (). the contains ( " HTTP " ) || tp.TextRanges [tpcount] .ClickAction.Address.ToLower (). the Contains ( " HTTPS " ))
                                    { 
                                        // re-assigned link 
                                        tp.TextRanges [tpcount] .ClickAction.Address = " https://baike.baidu.com/ " ; 

                                        // reset the hyperlink text 
                                        tp.TextRanges [tpcount] .Text = " Baidu Encyclopedia " ; 

                                        // remove a hyperlink
                                         // tp.TextRanges [tpcount] .ClickAction = null; 
                                    } 
                                } 
                            } 
                        } 
                    } 

                } 

            }

             // save the document
            ppt.SaveToFile("ModifyHyperlink.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("ModifyHyperlink.pptx");
        }
    }
}

Hyperlinks modify the results:

Hyperlink Deletes effects:

 

(This article End)

Guess you like

Origin www.cnblogs.com/Yesi/p/12626100.html