Hyperlinks in Word documents are clickable links that allow readers to navigate to a website or another document. While hyperlinks can provide valuable supplemental information, sometimes they can also be distracting or unnecessarily annoying, so you may want to remove them. In this article, you will learn how to remove hyperlinks from a Word document in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Remove All the Hyperlinks in a Word Document

To delete all hyperlinks in a Word document at once, you'll need to find all the hyperlinks in the document and then create a custom method FlattenHyperlinks() to flatten them. The following are the detailed steps.

  • Create a Document object.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Find all the hyperlinks in the document using custom method FindAllHyperlinks().
  • Loop through the hyperlinks and flatten all of them using custom method FlattenHyperlinks().
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Collections.Generic;
using Spire.Doc.Fields;


namespace removeWordHyperlink
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();

            //Load a sample Word document
            doc.LoadFromFile("Hyperlink.docx");

            //Find all hyperlinks
            List<Field> hyperlinks = FindAllHyperlinks(doc);

            //Flatten all hyperlinks
            for (int i = hyperlinks.Count - 1; i >= 0; i--)
            {
                FlattenHyperlinks(hyperlinks[i]);
            }

            //Save the result document
            doc.SaveToFile("RemoveHyperlinks.docx", FileFormat.Docx);

        }

        //Create a method FindAllHyperlinks() to get all the hyperlinks from the sample document
        private static List<Field> FindAllHyperlinks(Document document)
        {
            List<Field> hyperlinks = new List<Field>();

            //Iterate through the items in the sections to find all hyperlinks
            foreach (Section section in document.Sections)
            {
                foreach (DocumentObject sec in section.Body.ChildObjects)
                {
                    if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
                    {
                        foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
                        {
                            if (para.DocumentObjectType == DocumentObjectType.Field)
                            {
                                Field field = para as Field;

                                if (field.Type == FieldType.FieldHyperlink)
                                {
                                    hyperlinks.Add(field);
                                }
                            }
                        }
                    }
                }
            }
            return hyperlinks;
        }

        //Create a method FlattenHyperlinks() to flatten the hyperlink field
        private static void FlattenHyperlinks(Field field)
        {
            int ownerParaIndex = field.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.OwnerParagraph);
            int fieldIndex = field.OwnerParagraph.ChildObjects.IndexOf(field);
            Paragraph sepOwnerPara = field.Separator.OwnerParagraph;
            int sepOwnerParaIndex = field.Separator.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.Separator.OwnerParagraph);
            int sepIndex = field.Separator.OwnerParagraph.ChildObjects.IndexOf(field.Separator);
            int endIndex = field.End.OwnerParagraph.ChildObjects.IndexOf(field.End);
            int endOwnerParaIndex = field.End.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.End.OwnerParagraph);
            FormatFieldResultText(field.Separator.OwnerParagraph.OwnerTextBody, sepOwnerParaIndex, endOwnerParaIndex, sepIndex, endIndex);

            field.End.OwnerParagraph.ChildObjects.RemoveAt(endIndex);

            for (int i = sepOwnerParaIndex; i >= ownerParaIndex; i--)
            {
                if (i == sepOwnerParaIndex && i == ownerParaIndex)
                {
                    for (int j = sepIndex; j >= fieldIndex; j--)
                    {
                        field.OwnerParagraph.ChildObjects.RemoveAt(j);

                    }
                }
                else if (i == ownerParaIndex)
                {
                    for (int j = field.OwnerParagraph.ChildObjects.Count - 1; j >= fieldIndex; j--)
                    {
                        field.OwnerParagraph.ChildObjects.RemoveAt(j);
                    }

                }
                else if (i == sepOwnerParaIndex)
                {
                    for (int j = sepIndex; j >= 0; j--)
                    {
                        sepOwnerPara.ChildObjects.RemoveAt(j);
                    }
                }
                else
                {
                    field.OwnerParagraph.OwnerTextBody.ChildObjects.RemoveAt(i);
                }
            }
        }

        //Create a method FormatFieldResultText() to remove the font color and underline format of the hyperlinks
        private static void FormatFieldResultText(Body ownerBody, int sepOwnerParaIndex, int endOwnerParaIndex, int sepIndex, int endIndex)
        {
            for (int i = sepOwnerParaIndex; i <= endOwnerParaIndex; i++)
            {
                Paragraph para = ownerBody.ChildObjects[i] as Paragraph;
                if (i == sepOwnerParaIndex && i == endOwnerParaIndex)
                {
                    for (int j = sepIndex + 1; j < endIndex; j++)
                    {
                        FormatText(para.ChildObjects[j] as TextRange);
                    }

                }
                else if (i == sepOwnerParaIndex)
                {
                    for (int j = sepIndex + 1; j < para.ChildObjects.Count; j++)
                    {
                        FormatText(para.ChildObjects[j] as TextRange);
                    }
                }
                else if (i == endOwnerParaIndex)
                {
                    for (int j = 0; j < endIndex; j++)
                    {
                        FormatText(para.ChildObjects[j] as TextRange);
                    }
                }
                else
                {
                    for (int j = 0; j < para.ChildObjects.Count; j++)
                    {
                        FormatText(para.ChildObjects[j] as TextRange);
                    }
                }
            }
        }

        //Create a method FormatText() to change the color of the text to black and remove the underline
        private static void FormatText(TextRange tr)
        {
            //Set the text color to black
            tr.CharacterFormat.TextColor = Color.Black;

            //Set the text underline style to none
            tr.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
        }
    }
}

C#/VB.NET: Remove Hyperlinks in Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

C#/VB.NET: Convert PDF to SVG

2022-07-14 03:39:00 Written by Administrator

SVG (Scalable Vector Graphics) is an image file format used for rendering two-dimensional images on the web. Comparing with other image file formats, SVG has many advantages such as supporting interactivity and animation, allowing users to search, index, script, and compress/enlarge images without losing quality. Occasionally, you may need to convert PDF files to SVG file format, and this article will demonstrate how to accomplish this task using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Convert a PDF File to SVG in C#/VB.NET

Spire.PDF for .NET offers the PdfDocument.SaveToFile(String, FileFormat) method to convert each page in a PDF file to a single SVG file. The detailed steps are as follows.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Convert the PDF file to SVG using PdfDocument.SaveToFile(String, FileFormat) method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace ConvertPDFtoSVG
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument document = new PdfDocument();

            //Load a sample PDF file
            document.LoadFromFile("input.pdf");

            //Convert PDF to SVG
            document.SaveToFile("PDFtoSVG.svg", FileFormat.SVG);
        }
    }
}

C#/VB.NET: Convert PDF to SVG

Convert Selected PDF Pages to SVG in C#/VB.NET

The PdfDocument.SaveToFile(String, Int32, Int32, FileFormat) method allows you to convert the specified pages in a PDF file to SVG files. The detailed steps are as follows.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Convert selected PDF pages to SVG using PdfDocument.SaveToFile(String, Int32, Int32, FileFormat) method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace PDFPagetoSVG
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a sample PDF file
            doc.LoadFromFile("input.pdf");

            //Convert selected PDF pages to SVG
            doc.SaveToFile("PDFPagetoSVG.svg", 1, 2, FileFormat.SVG);
        }
    }
}

C#/VB.NET: Convert PDF to SVG

Convert a PDF File to SVG with Custom Width and Height in C#/VB.NET

The PdfConvertOptions.SetPdfToSvgOptions() method offered by Spire.PDF for .NET allows you to specify the width and height of output SVG file. The detailed steps are as follows.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Set PDF convert options using PdfDocument.ConvertOptions property.
  • Specify the width and height of output SVG file using PdfConvertOptions.SetPdfToSvgOptions() method.
  • Convert the PDF file to SVG using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace PDFtoSVG
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument document = new PdfDocument();

            //Load a sample PDF file
            document.LoadFromFile("input.pdf");

            //Specify the width and height of output SVG file
            PdfToSvgConverter converter = new PdfToSvgConverter(inputFile);
            converter.SvgOptions.ScaleX = (float)0.5;
            converter.SvgOptions.ScaleY = (float)0.5;
            converter.Convert(outputFile);


            //Convert PDF to SVG
            document.SaveToFile("result.svg", FileFormat.SVG);
        }
    }
}

C#/VB.NET: Convert PDF to SVG

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

How to Copy a Chart in PowerPoint

2016-09-29 08:16:36 Written by Koohji

In this article, we will explain how to copy an existing chart within the same PowerPoint document or between PowerPoint documents by using Spire.Presentation. Below example called a main method public IChart CreateChart(IChart baseChart, RectangleF rectangle, int nIndex).

There are three Parameters passed in this method:

  • baseChart: The source chart.
  • rectangle: The area that the chart will be copied to.
  • nIndex: The index of the rectangle shape. For example, -1 means append it as the last shape of the slide, 0 means append as the first shape.

Now refer to the following steps:

Copy chart within the same PowerPoint document

Step 1: Instantiate a Presentation object and load the PowerPoint document.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx");

Step 2: Get the chart that is going to be copied.

IChart chart = ppt.Slides[0].Shapes[0] as IChart;

Step 3: Copy the chart from the first slide to the specified location of the second slide within the same document.

ppt.Slides[1].Shapes.CreateChart(chart, new RectangleF(100, 100, 500, 300), 0);

Step 4: Save the document.

ppt.SaveToFile("TestResult.pptx", FileFormat.Pptx2010);

Screenshot of copying chart within the same PowerPoint document:

Copy chart within the same PowerPoint document

Copy chart between PowerPoint documents

Step 1: Load the first PowerPoint document.

Presentation ppt1 = new Presentation();
ppt1.LoadFromFile("Sample.pptx");

Step 2: Get the chart that is going to be copied.

IChart chart = ppt1.Slides[0].Shapes[0] as IChart;

Step 3: Load the second PowerPoint document.

Presentation ppt2 = new Presentation();
ppt2.LoadFromFile("Dest.pptx");

Step 4: Copy chart from the first document to the specified location of the second document.

ppt2.Slides[0].Shapes.CreateChart(chart, new RectangleF(100, 100, 500, 300), -1);

Step 5: Save the second document.

ppt2.SaveToFile("TestResult2.pptx", FileFormat.Pptx2010);

Screenshot of copying chart between PowerPoint documents:

Copy chart between PowerPoint documents

Full code:

Copy chart within the same PowerPoint document

using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Charts;

namespace Copy_Chart
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("Sample.pptx");
            IChart chart = ppt.Slides[0].Shapes[0] as IChart;
            ppt.Slides[1].Shapes.CreateChart(chart, new RectangleF(100, 100, 500, 300), 0);
            ppt.SaveToFile("TestResult.pptx", FileFormat.Pptx2010);
        }
    }
}

Copy chart between PowerPoint documents

using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Charts;

namespace Copy_Chart
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation ppt1 = new Presentation();
            ppt1.LoadFromFile("Sample.pptx");
            IChart chart = ppt1.Slides[0].Shapes[0] as IChart;
            Presentation ppt2 = new Presentation();
            ppt2.LoadFromFile("Dest.pptx");
            ppt2.Slides[0].Shapes.CreateChart(chart, new RectangleF(100, 100, 500, 300), -1);
            ppt2.SaveToFile("TestResult2.pptx", FileFormat.Pptx2010);
        }
    }
}
page 212