Aspose.Pdf.Examples.CSharp.AsposePDF.Text.AddText.AddHyperlinkToTextSegment C# (CSharp) Method

AddHyperlinkToTextSegment() public static method

public static AddHyperlinkToTextSegment ( ) : void
return void
        public static void AddHyperlinkToTextSegment()
        {
            // ExStart:AddHyperlinkToTextSegment
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_Text();
            // Create document instance
            Document doc = new Document();
            // Add page to pages collection of PDF file
            Page page1 = doc.Pages.Add();
            // Create TextFragment instance
            TextFragment tf = new TextFragment("Sample Text Fragment");
            // Set horizontal alignment for TextFragment
            tf.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
            // Create a textsegment with sample text
            TextSegment segment = new TextSegment(" ... Text Segment 1...");
            // Add segment to segments collection of TextFragment
            tf.Segments.Add(segment);
            // Create a new TextSegment 
            segment = new TextSegment("Link to Google");
            // Add segment to segments collection of TextFragment
            tf.Segments.Add(segment);
            // Set hyperlink for TextSegment
            segment.Hyperlink = new Aspose.Pdf.WebHyperlink("www.google.com");
            // Set forground color for text segment
            segment.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;
            // Set text formatting as italic
            segment.TextState.FontStyle = FontStyles.Italic;
            // Create another TextSegment object
            segment = new TextSegment("TextSegment without hyperlink");
            // Add segment to segments collection of TextFragment
            tf.Segments.Add(segment);
            // Add TextFragment to paragraphs collection of page object
            page1.Paragraphs.Add(tf);

            dataDir = dataDir + "AddHyperlinkToTextSegment_out.pdf";

            // Save resulting PDF document.
            doc.Save(dataDir);

            // ExEnd:AddHyperlinkToTextSegment
            Console.WriteLine("\nHyperlink to text segment added successfully.\nFile saved at " + dataDir);
        }