We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Lock HYPERLINK Fields in Word DOT DOCX Files and then Convert to PDF | C# .NET

Hello everyone. I was converting .doc, .odt and .dot format files into .pdf using Aspose words for .NET.
My Aspose words version is 21.2.0.

I’m attaching the original files and generated files. You will see the generated files has “Name of Specialist (if required): -CUSTOM " where the original file has " Name of Specialist (if required): -”.
Below is the code that I used to convert the word file into PDF.

Aspose.Words.License license = new Aspose.Words.License();

//license.SetLicense("Aspose.Total.NET.lic");

Console.WriteLine("License set successfully.");

Aspose.Words.Document doc = new Aspose.Words.Document("C:\\Users\\abc\\Downloads\\Referral Elsa A (1).dot");

Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc);

// Create the headers.
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

builder.Write("Attachment: Referral Elsa A.odt \n");

doc.UpdateFields();

doc.UpdatePageLayout();

doc.Save("C:\\Users\\abc\\Downloads\\Referral Elsa A (1).dot.pdf");

Interestingly, when I remove those line which write extra text to header, this problem is solved.
Also, this problem is occurring with specific files and not with all the files.

Copy paste of the line causing issue from original document - " Name of Specialist (if required): -" don’t know why there are these ** symbols in the text where as it does not display in the word or PDF. This could be causing some issue?

Any help is greatly appreciated.

Note: you may find some patient data in the files. It is all fake data and use at free will. Template and logos may be copy righted.

Referral Elsa A (1).dot.zip (486.3 KB)

@sairamarao,

This seems to be an expected behavior because you are calling Document.UpdateFields method. For example, please open “Draft Referral Elsa A (1).doc” with MS Word, press Ctrl + A to select all content and then press F9 to update all fields in Word document. You will then notice that the CUSTOM text appears at a couple of places. And Aspose.Words mimics this behavior of MS Word. However, you can prevent Aspose.Words from showing that content in PDF by locking the HYPERLINK fields:

Document doc = new Document("C:\\Temp\\Draft Referral Elsa A (1).doc");
Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Attachment: Referral Elsa A.odt \n");

foreach (Field field in doc.Range.Fields)
    if (field.Type == FieldType.FieldHyperlink)
        field.IsLocked = true;

doc.UpdateFields();
doc.UpdatePageLayout();

doc.Save("C:\\temp\\output.pdf");

Thank you @awais.hafeez,

Seems like this fixed the issue. Do you have any idea why it added image.png (4.9 KB)
in another document? do you think the extra letters after “CUSTOM” were also because of the same issue?

@sairamarao,

Most likely, this also happens because of the same behavior of HYPERLINK fields. But, can you please also ZIP and upload your other Word document and Aspose.Words generated PDF file showing the undesired behavior here for testing. We will then investigate the particular issue on our end and provide you more information.

@awais.hafeez
Unfortunately I can not share that document but looks like the problem is fixed. I will get back to you if this problem shows up again.
Thank you so much for your help so far.

@sairamarao,

It is great that you were able to resolve the problem on your end. In case you have further inquiries or may need any help in future, please let us know by posting a new thread in Aspose.Words’ forum.