Spire.Doc

Bookmarks give convenience when users want go to specified location and it is clearly to know the contents brief information. Spire.Doc for .NET has a powerful function of operating the word elements of bookmarks. Developers can add bookmarks, Edit/replace bookmarks and remove bookmarks in word documents. Now Spire.Doc starts to support preserve bookmarks in DOCX to PDF conversion. This article will show you how to preserve bookmarks in C# when converting word document into PDF file format.

Download and install Spire.Doc for .NET (Version 5.2.20 or above) 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". Here comes to the details of how to finding hyperlinks in C#.

Step 1: Load a word documents with bookmarks.

Document doc = new Document();
doc.LoadFromFile("test.docx", FileFormat.Docx);

Step 2: Create an instance of ToPdfParameterList

ToPdfParameterList toPdf = new ToPdfParameterList();

Step 3: Set CreateWordBookmarks to true to use word bookmarks when create the bookmarks.

toPdf.CreateWordBookmarks = true;

Step 4: Save the PDF file.

doc.SaveToFile("test.Pdf",toPdf);

Effective screenshot of preserve the bookmarks in result PDF page:

Preserve bookmarks in PDF from word

Full codes:

[C#]
Document doc = new Document();
doc.LoadFromFile("test.docx", FileFormat.Docx);
ToPdfParameterList toPdf = new ToPdfParameterList();
toPdf.CreateWordBookmarks = true;
doc.SaveToFile("test.Pdf", toPdf);
System.Diagnostics.Process.Start("test.Pdf");

Sections are widely used by developers to set different formatting or layout options to each different section, such as use the different header and footer information for different sections and reset page number for each section dynamically. With the help of Spire.Doc for .NET, we can easily insert word section and remove word section in C# and VB.NET. We will show you how to reset page numbering that starts at 1 for each section easily by using Spire.Doc.

Firstly make sure Spire.Doc for .NET 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". Here comes to the codes of how to reset page numbering for each section.

Step 1: Load three different word documents

Document document1 = new Document();
document1.LoadFromFile("..\\..\\1.docx");
         
Document document2 = new Document();
document2.LoadFromFile("..\\..\\2.docx");

Document document3 = new Document();
document3.LoadFromFile("..\\..\\3.docx");

Step 2: Use section method to combine all documents into one word document

foreach (Section sec in document2.Sections)
{
document1.Sections.Add(sec.Clone());
}
foreach (Section sec in document3.Sections)
{
document1.Sections.Add(sec.Clone());     
}

Step 3: Traverse the document

//Traverse every section of document1 
foreach (Section sec in document1.Sections)
{
//Traverse every object of the footer
foreach (DocumentObject obj in sec.HeadersFooters.Footer.ChildObjects)
{
if (obj.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
{
DocumentObject para = obj.ChildObjects[0];
foreach (DocumentObject item in para.ChildObjects)
                        {
if (item.DocumentObjectType == DocumentObjectType.Field)

Step 4: Find the field type FieldNumPages and change it to FieldSectionPages

//Find the item and its field type is FieldNumPages
if ((item as Field).Type == FieldType.FieldNumPages)
{
//Change field type to FieldSectionPages
(item as Field).Type = FieldType.FieldSectionPages;

Step 5: Restart page number of section and set the starting page number to 1

document1.Sections[1].PageSetup.RestartPageNumbering = true;
document1.Sections[1].PageSetup.PageStartingNumber = 1;
       
document1.Sections[2].PageSetup.RestartPageNumbering = true;
document1.Sections[2].PageSetup.PageStartingNumber = 1;

Step 6: Save the document to file and launch it.

document1.SaveToFile("sample.docx",FileFormat.Docx);  
System.Diagnostics.Process.Start("sample.docx");

Full codes:

namespace ResetPageNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document1 = new Document();
            document1.LoadFromFile("..\\..\\1.docx");
         
            Document document2 = new Document();
            document2.LoadFromFile("..\\..\\2.docx");

            Document document3 = new Document();
            document3.LoadFromFile("..\\..\\3.docx");

            foreach (Section sec in document2.Sections)
            {
                document1.Sections.Add(sec.Clone());
            }

            foreach (Section sec in document3.Sections)
            {
                document1.Sections.Add(sec.Clone());     
               
            }
            foreach (Section sec in document1.Sections)
            {
                foreach (DocumentObject obj in sec.HeadersFooters.Footer.ChildObjects)
                {
                    if (obj.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
                    {
                        DocumentObject para = obj.ChildObjects[0];
                        foreach (DocumentObject item in para.ChildObjects)
                        {
                            if (item.DocumentObjectType == DocumentObjectType.Field)
                            {
                                if ((item as Field).Type == FieldType.FieldNumPages)
                                {
                                    (item as Field).Type = FieldType.FieldSectionPages;
                                }
                            }
                        }
                    }       
                }
            }

            document1.Sections[1].PageSetup.RestartPageNumbering = true;
            document1.Sections[1].PageSetup.PageStartingNumber = 1;
       
            document1.Sections[2].PageSetup.RestartPageNumbering = true;
            document1.Sections[2].PageSetup.PageStartingNumber = 1;

            document1.SaveToFile("sample.docx",FileFormat.Docx);
            System.Diagnostics.Process.Start("sample.docx");
        }
    }
}
Friday, 27 June 2014 08:50

How to Modify Hyperlinks in Word Document

Written by support iceblue

Hyperlinks have two basic parts: the address (URL) of a certain object and the display text. As an essential element in electronic document, hyperlinks are frequently used in Word to redirect to large amount of information without occupying much space. However, Word doesn't always get the hyperlink correct. In this article, I'll introduce you how to change the displayed text of a hyperlink in C# via Spire.Doc for .NET.

Here is a Word document that makes a brief introduction about Spire.Doc, but as you can see from the picture below, the first hyperlink has been embedded with a wrong text description.

Modify Hyperlinks in Word Document

Now, we are able to change the hypertext as "Spire.Doc component" by using the following method.

Step 1: Create a new instance of Word document and load the sample file.

Document document = new Document();
document.LoadFromFile("..\\..\\Spire.Doc.docx");

Step 2: Call FindAllHyperlinks() method to find all hyperlinks in the Word document.

static List FindAllHyperlinks(Document document)
        {
            List hyperlinks = new List();
            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);
                                }
                            }
                        }
                    }
                }
            }

Step 3: Reset the property of hyperlinks[0].FieldText by using the index of the hyperlink.

hyperlinks[0].FieldText = "Spire.Doc component";

string outfile="result.doc";
document.SaveToFile(outfile,FileFormat.Doc);
System.Diagnostics.Process.Start(outfile);

Result:

Modify Hyperlinks in Word Document

Page 1 of 24