Spire.PDF for .NET is a PDF library which enables users to handle the whole PDF document with wide range of tasks in C#, VB.NET. Spire.PDF supports to create PDF links, extract PDF links, update PDF links and remove PDF links from a PDF file. This article mainly shows how to extract and update link from a PDF file in C#.

Before the steps and codes, please check the original PDF file at first:

PDF link

Firstly we need to create a new Document object and load a PDF file which needs to extract and update the links. The links are represented as annotations in a PDF file. Before extract and update the link from a PDF file, we need to extract all the AnnotationsWidget objects.

The following code snippet shows you how to extract links from the PDF file.

//Load an existing PDF file
PdfDocument document = new PdfDocument();
document.LoadFromFile(@"..\..\output.pdf");

//Get the first page
PdfPageBase page = document.Pages[0];

//Get the annotation collection
PdfAnnotationCollection widgetCollection = page.Annotations;

//Verify whether widgetCollection is not null or not
if (widgetCollection.Count > 0)
            {
                foreach (var obj in widgetCollection)
                {

//Get the TextWebLink Annotation
if (obj is PdfTextWebLinkAnnotationWidget)
   {
     PdfTextWebLinkAnnotationWidget link = obj as PdfTextWebLinkAnnotationWidget;

The following code snippet shows you how to update the link in PDF file

//Change the url of TextWebLink Annotation
link.Url = "http://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html";

//Saves PDF document to file.
document.SaveToFile("sample.pdf");

This picture will show you the link has been updated in the PDF file.

Update link

PDF Bookmarks is widely used to help us locate and link to points that we want to go. Spire.PDF for .NET, a powerful PDF component, not only enables developers to add bookmarks, get bookmark information, but also helps us to remove the bookmarks that we want. This article will show you how to delete bookmark from the PDF document in C# easily.

First, check the PDF document with bookmarks.

Bookmark

Here comes to the steps of how to remove the bookmarks in C#.

  • Download Spire.PDF for .NET and install it correctly. The Spire.PDF installation is clean, professional and wrapped up in a MSI installer.
  • Add Spire.Pdf.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Pdf\Bin\NET4.0\ Spire.Pdf.dll".
  • Check the code snippet of how to delete the bookmarks. With Spire.PDF.NET, we can remove both the particular bookmark and remove all the bookmarks at one time.
using Spire.Pdf;

namespace DeletePDFBookmark
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new PDF document and load from the file.
            PdfDocument document = new PdfDocument();
            document.LoadFromFile(@"D:\Bookmark.pdf");

            //remove the first bookmark
            document.Bookmarks.RemoveAt(0);

            //remove all bookmarks
            document.Bookmarks.Clear();

            //save the document to file.
            document.SaveToFile("result.pdf");
        }
    }
}

Please check the effective screenshots:

Remove particular bookmark

Remove all Bookmarks

Section breaks allow you to divide a document into sections, each with its own formatting and layout settings, giving you greater control over the overall structure and appearance of your document. Whether you're compiling a complex report or a lengthy dissertation, mastering section breaks can greatly improve your document management skills. This article will demonstrate how to insert or remove section breaks in Word in C# 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

Insert Section Breaks in Word in C#

Spire.Doc for .NET provides the Paragraph.InsertSectionBreak(SectionBreakType breakType) method to insert a specified type of section break to a paragraph. The following table provides an overview of the supported section break types, along with their corresponding Enums and descriptions:

Section Break Enum Description
New page SectionBreakType.NewPage Start the new section on a new page.
Continuous SectionBreakType.NoBreak Start the new section on the same page, allowing for continuous content flow.
Odd page SectionBreakType.OddPage Start the new section on the next odd-numbered page.
Even page SectionBreakType.EvenPage Start the new section on the next even-numbered page.
New column SectionBreakType.NewColumn Start the new section in the next column if columns are enabled.

The following are the detailed steps to insert a continuous section break:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Get a specified paragraph of the section using Section.Paragraphs[] property.
  • Add a section break to the end of the paragraph using Paragraph.InsertSectionBreak() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;

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

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

            //Get the first section in the document
            Section sec = doc.Sections[0];

            //Get the second paragraph in the section 
            Paragraph para = sec.Paragraphs[1];

            //Insert a continuous section break
            para.InsertSectionBreak(SectionBreakType.NoBreak);

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

C#: Insert or Remove Section Breaks in Word

Remove Section Breaks in Word in C#

To delete all sections breaks in a Word document, we need to access the first section in the document, then copy the contents of the other sections to the first section and delete them. The following are the detailed steps:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the first section using Document.Sections[] property.
  • Iterate through other sections in the document.
  • Get the second section, and then iterate through to get its child objects.
  • Clone the child objects of the second section and add them to the first section using Section.Body.ChildObjects.Add() method.
  • Delete the second section using Document.Sections.Remove() method.
  • Repeat the process to copy and delete the remaining sections.
  • Save the result document using Document.SaveToFile() method.
  • C#
using Spire.Doc;

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

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

            //Get the first section in the document
            Section sec = doc.Sections[0];

            //Iterate through other sections in the document
            for (int i = 0; i < doc.Sections.Count - 1; i++)
            {
                //Get the second section in the document
                Section section = doc.Sections[1];

                //Iterate through all child objects of the second section
                for (int j = 0; j < section.Body.ChildObjects.Count; j++)
                {
                    //Get the child objects
                    DocumentObject obj = section.Body.ChildObjects[j];
                    //Clone the child objects to the first section
                    sec.Body.ChildObjects.Add(obj.Clone());
                }

                //Remove the second section
                doc.Sections.Remove(section);
            }

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

C#: Insert or Remove Section Breaks in Word

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.

Line spacing is the amount of white space between each line in a paragraph, while paragraph spacing is the amount of white space before and after each paragraph in a document. In MS Word, you can adjust the spacing manually if the default spacing does not meet your needs. In this article, you will learn how to programmatically set line spacing and paragraph spacing in Word 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

Set Line Spacing and Paragraph Spacing in Word

Loose line or paragraph spacing can make text more readable, while tight line or paragraph spacing can fit more text in a document. Below are the steps to adjust the line spacing and paragraph spacing in a Word document.

  • Create a Document instance.
  • Add a section to the document using Document.AddSection() method, and then add a paragraph to the section.
  • Set the before and after spacing for the paragraph using Paragraph.Format.BeforeSpacing and Paragraph.Format.AfterSpacing properties.
  • Add another paragraph and set line spacing in the paragraph using Paragraph.Format.LineSpacing property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

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

            //Add a section
            Section section = document.AddSection();

            //Add a paragraph
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendText("Spire.Doc for .NET is a professional Word .NET library specifically designed for developers to " +
                "create, read, write, convert, compare and print Word documents on any .NET platform " +
                "(Target .NET Framework, .NET Core, .NET Standard, .NET 5.0, .NET 6.0, Xamarin & Mono Android ) with fast and high quality performance.");

            //Set spacing before the paragraph
            paragraph.Format.BeforeSpacing = 30;

            //Set spacing after the paragraph
            paragraph.Format.AfterSpacing = 30;

            //Add another paragraph
            Paragraph paragraph2 = section.AddParagraph();
            paragraph2.AppendText("Spire.Doc for .NET is a reliable API which enables to perform many Word document processing tasks. " +
                "It supports C#, VB.NET, ASP.NET and ASP.NET MVC. Spire.Doc supports Word 97-2003 /2007/2010/2013/2016/2019 " +
                "and it has the ability to convert them to commonly used file formats like XML, RTF, TXT, XPS, EPUB, HTML and vice versa. ");

            //Set line spacing in the paragraph
            paragraph2.Format.LineSpacing = 25;

            //Save the result document
            document.SaveToFile("SetSpacing.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Set Line Spacing and Paragraph Spacing in Word

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.

PDF/A is widely used for long term archiving for PDF format. By using Spire.PDF, you can create PDF/A file directly. This article mainly shows how to set up PDF/A file; it will also demonstrate how to add image and insert hyperlink to image in C#.

Make sure Spire.PDF for .NET (version 2.9.43 or above) has been installed correctly and then add Spire.Pdf.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Pdf\Bin\NET4.0\ Spire.Pdf.dll".

Here comes to the steps:

Step 1: Create a PDF/A document.

// Create a PdfDocument instance
PdfDocument document = new PdfDocument();
PdfPageBase page = document.Pages.Add();
page.Canvas.DrawString("Hello World", new PdfFont(PdfFontFamily.Helvetica, 30f), new PdfSolidBrush(Color.Black), 10, 10);

Step 2: Load an image from file and insert to the PDF.

//insert an image
PdfImage image = PdfImage.FromFile(@"D:\PDF.png");

Step 3: Add a hyper link to the image.

//Add a link to image
PointF location = new PointF(100, 100);
RectangleF linkBounds = new RectangleF(location, new SizeF(image.Width, image.Height));
Spire.Pdf.Annotations.PdfUriAnnotation link = new Spire.Pdf.Annotations.PdfUriAnnotation(linkBounds, "http://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html");
link.Border = new PdfAnnotationBorder(0);
page.Canvas.DrawImage(image, linkBounds);
page.Annotations.Add(link);

Step 4: Save the PDF document.

//Save the document to file in PDF format
String output1 = @"..\..\..\..\..\..\Data\ToPDF.pdf";
document.SaveToFile(output1);

// Create an instance of the PdfStandardsConverter class, passing the input PDF file path as a parameter.
PdfStandardsConverter converter = new PdfStandardsConverter(output1);

// Specify the desired file name for the resulting PDFA-1b compliant PDF.
String output2 = @"..\..\..\..\..\..\Data\ToPDFA.pdf";

// Convert the input PDF file to PDFA-1b format and save it using the specified output file name.
converter.ToPdfA1B(output2);

Effective screenshot:

Create PDF/A and insert hyperlink to image

This article shows how to place a footnote in a paragraph using Spire.Doc for .NET, for example, after the word "Spire.Doc" in the following example:

Insert a footnote in a paragraph

Insert a footnote in a paragraph

Step 1: Load a word document, Sample.docx.

Document document1 = new Document();
document1.LoadFromFile("D:\\Sample.docx",FileFormat.Docx2010);

Step 2: Get the first paragraph of the first section of Sample.docx.

Paragraph paragraph1 = document1.Sections[0].Paragraphs[0];

Step 3: Add a footnote for paragraph1.

Footnote footnote1 = paragraph1.AppendFootnote(FootnoteType.Footnote);

Step 4: Find the word "Spire.Doc" and insert footnote1 after it.

DocumentObject obj=null;
for (int i = 0; i < paragraph1.ChildObjects.Count; i++)
{
   obj=paragraph1.ChildObjects[i];
   if (obj.DocumentObjectType == DocumentObjectType.TextRange)
   {
      TextRange textRange = obj as TextRange;
// Find the word "Spire.Doc" in paragraph1
if (textRange.Text == "Spire.Doc")
{
//Set bold format for the word "Spire.Doc"
textRange.CharacterFormat.Bold = true;
//Insert footnote1 after the word "Spire.Doc"
paragraph1.ChildObjects.Insert(i + 1, footnote1);
break;
}
     }
 }

Step 5: Type the footnote1 text and set the text's FontName, FontSize and Color.

TextRange text = footnote1.TextBody.AddParagraph().AppendText("Welcome to evaluate Spire.Doc");
text.CharacterFormat.FontName = "Arial Black";
text.CharacterFormat.FontSize = 10;
text.CharacterFormat.TextColor = Color.DarkGray;

Step 6: Set FontName, FontSize, Bold and Color for the footnote1 number.

footnote1.MarkerCharacterFormat.FontName = "Calibri";
footnote1.MarkerCharacterFormat.FontSize = 12;
footnote1.MarkerCharacterFormat.Bold = true;
footnote1.MarkerCharacterFormat.TextColor = Color.DarkGreen;

Step 7: Save Sample.docx to a new document, Footnote.docx.

document1.SaveToFile("D:\\ Footnote.docx"", FileFormat.Docx2010);

Full code:

Document document1 = new Document();
document1.LoadFromFile("D:\\Sample.docx" ,FileFormat.Docx2010);
Paragraph paragraph1= document1.Sections[0].Paragraphs[0];
Footnote footnote1 = paragraph1.AppendFootnote(FootnoteType.Footnote);
DocumentObject obj=null;
for (int i = 0; i < paragraph1.ChildObjects.Count; i++)
{
obj=paragraph1.ChildObjects[i];
if (obj.DocumentObjectType == DocumentObjectType.TextRange)
{
TextRange textRange = obj as TextRange;
// Find the word "Spire.Doc" in paragraph1
if (textRange.Text == "Spire.Doc")
{
//Set bold format for the word "Spire.Doc"
textRange.CharacterFormat.Bold = true;
//Insert footnote1 after the word "Spire.Doc"
paragraph1.ChildObjects.Insert(i + 1, footnote1);
break;
}
}
}
TextRange text = footnote1.TextBody.AddParagraph().AppendText("Welcome to evaluate Spire.Doc");
text.CharacterFormat.FontName = "Arial Black";
text.CharacterFormat.FontSize = 10;
text.CharacterFormat.TextColor = Color.DarkGray;

footnote1.MarkerCharacterFormat.FontName = "Calibri";
footnote1.MarkerCharacterFormat.FontSize = 12;
footnote1.MarkerCharacterFormat.Bold = true;
footnote1.MarkerCharacterFormat.TextColor = Color.DarkGreen;
document1.SaveToFile("Footnote.docx",FileFormat.Docx2010);

How to Convert Word to PDF/A in C# ?

2014-03-20 08:52:36 Written by Koohji

PDF/A is an ISO-standardized version of the Portable Document Format (PDF) specialized for the digital preservation of electronic documents. It is widely used for long term archiving for PDF format. This article mainly shows how to convert word document (doc and docx) to PDF/A in C# by using Spire.Doc.

Make sure Spire.Doc for .NET Version 5.0.26 (or above) has been installed correctly and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll".

First, check the original word document that will be converted to PDF/A.

Convert Word to PDF/A in C#

Here comes to the details of how developers convert word document to PDF/A directly by using Spire.Doc:

Step 1: Load a word document from the file.

Document document = new Document();
document.LoadFromFile(@"D:\test.docx",FileFormat.Docx);

Step 2: Sets the Pdf document's Conformance-level to PDF_A1B.

ToPdfParameterList toPdf = new ToPdfParameterList();
toPdf.PdfConformanceLevel = Spire.Pdf.PdfConformanceLevel.Pdf_A1B;

Step 3: Save word document to PDF

document.SaveToFile("result.Pdf",toPdf);

Please check the effective screenshot of the result PDF in PDF/A format.

Convert Word to PDF/A in C#

New Method to Merge Word Documents

2014-03-14 07:48:15 Written by Koohji

When dealing with Word documents, sometimes developers need to merge multiple files into a single file. Spire.Doc, especially designed for developers enables you to manipulate doc files easily and flexibly.

There is already a document introducing how to merge doc files. Check it here:

.NET Merge Word - Merge Multiple Word Documents into One in C# and VB.NET

Using the method above, you have to copy sections one by one. But the new method just concatenates them. It has improved and is very easy to use

Step 1: Load the original word file "A Good Man.docx".

document.LoadFromFile("A Good Man.docx", FileFormat.Docx);

Step 2: Merge another word file "Original Word.docx" to the original one.

document.InsertTextFromFile("Original Word.docx", FileFormat.Docx);

Step 3: Save the file.

document.SaveToFile("MergedFile.docx", FileFormat.Docx);

Full code and screenshot:

static void Main(string[] args)
{
    Document document = new Document();
    document.LoadFromFile("A Good Man.docx", FileFormat.Docx);

    document.InsertTextFromFile("Original Word.docx", FileFormat.Docx);

    document.SaveToFile("MergedFile.docx", FileFormat.Docx);
    System.Diagnostics.Process.Start("MergedFile.docx");
}

Full code and screenshot:

using Spire.Doc;
namespace MergeWord
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("A Good Man.docx", FileFormat.Docx);

            document.InsertTextFromFile("Original Word.docx", FileFormat.Docx);

            document.SaveToFile("MergedFile.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("MergedFile.docx");
        }
    }
}

New Method to Merge Word Documents

Word Marco is widely used to record the operation that needs to be done repeatedly and you can apply it with only a single click. It can save lots of time for you. It is not an easy work to load a word document with Macro in C# and sometimes, you need to remove the Marco in the word documents in C#. This article will focus on demonstrate how to load and save word document with Marco, and clear the Marco by using Spire.Doc in C# in simple lines of codes.

First, download Spire.Doc and install on your system. The Spire.Doc installation is clean, professional and wrapped up in a MSI installer.

Then adds Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll".

Here comes to the steps.

Step 1: Load and save the document with Marco. Spire.Doc for .NET supports .doc, .docx(Word 97-2003) document with macros and .docm(Word 2007 and Word 2010) document.

//Loading document with macros.
document.LoadFromFile(@"D:\Macros.docm", FileFormat.Docm);
//Save docm file.
document.SaveToFile("Sample.docm", FileFormat.Docm);

Load and save word with Macro

Step 2: Clear the Marco in word document. With Spire.Doc, you only need one line of code to remove all the Marcos at one time.

//Removes the macros from the document
document.ClearMacros();
//Save docm file.
document.SaveToFile("Sample.docm", FileFormat.Docm);

Here comes to the screenshot which has removed the Marco in word document.

Remove Macro

Image can add interest and take effect to your Word documents. Suppose you've written an article introducing a city about its beautiful scenery. Just using words describe, you cannot present perfect scenery to your readers, because that page of text looks indistinct and dull. You need image to embellish your scenery.

Spire.Doc for .NET, a professional .NET word component to fast generate, open, modify and save Word documents without using MS Office Automation, enables users to insert image into Word document and set its size according to page by using C#.NET. This guide introduces an easy method how to insert image via Spire.Doc for .NET.

At first, create new Word document and add section, paragraph for this document. Then, insert image in the new created paragraph. You can set this image's size, position, rotate it and choice the wrapping-style about the text. Download and Install Spire.Doc for .NET. Use the following code to insert image in Word by using C#.

The sample demo shows how to insert an image into a document.

Insert Image in Word Document

using System;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace Insert_image_to_Word_Document
{
    class Program
    {
        static void Main(string[] args)
        {
 //Open a blank word document as template
            Document document = new Document(@"Blank.doc");
            Section section = document.Sections[0];
            Paragraph paragraph
                = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
            paragraph.AppendText("The sample demonstrates how to insert an image into a document.");
            paragraph.ApplyStyle(BuiltinStyle.Heading2);
            paragraph = section.AddParagraph();
  
            //get original image 
            Bitmap p = new Bitmap(Image.FromFile(@"Word.jpg")); 
           
            //rotate image and insert image to word document
            p.RotateFlip(RotateFlipType.Rotate90FlipX);

            DocPicture picture = document.Sections[0].Paragraphs[0].AppendPicture(p);
   
            //set image's position
            picture.HorizontalPosition = 50.0F;
            picture.VerticalPosition = 60.0F;

            //set image's size
            picture.Width = 200;
            picture.Height = 200;

            //set textWrappingStyle with image;
            picture.TextWrappingStyle = TextWrappingStyle.Through;

            //Save doc file.
            document.SaveToFile("Sample.doc", FileFormat.Doc);

            //Launching the MS Word file.            System.Diagnostics.Process.Start("Sample.doc");
        }
       }
}

If you have finished the tutorial Spire.Doc Quick Start, the steps above will be simpler.

With Spire.Doc, you can insert an image into your Word documents and do more same thing in your ASP.NET, WPF and Silverlight applications without Word automation and any other third party add-ins.

page 64