Spire.Doc for .NET (337)
Children categories
Replacing image with text or image in a Word document is possible in Spire.Doc. We've already illustrated how to replace image with text, in this tutorial, we will show you how to replace a specified image with another image using Spire.Doc and C#.
Below is the original Word document before replacing image:

Code Snippet:
Step 1: Load the Word document.
Document document = new Document("Input.docx");
Step 2: Replace the image which title is "Figure 1" with new image.
//Loop through the paragraphs of the section
foreach (Paragraph paragraph in document.Sections[0].Paragraphs)
{
//Loop through the child elements of paragraph
foreach (DocumentObject docObj in paragraph.ChildObjects)
{
if (docObj.DocumentObjectType == DocumentObjectType.Picture)
{
DocPicture picture = docObj as DocPicture;
if (picture.Title == "Figure 1")
{
//Replace the image
picture.LoadImage(Image.FromFile("PinkRoses.jpg"));
}
}
}
}
Step 3: Save and close the document.
document.SaveToFile("ReplaceImage.docx");
document.Close();
The resultant document looks as follows:

Full code:
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace Replace_Image
{
class Program
{
static void Main(string[] args)
{
//Load the Word document
Document document = new Document("Input.docx");
//Loop through the paragraphs of the section
foreach (Paragraph paragraph in document.Sections[0].Paragraphs)
{
//Loop through the child elements of paragraph
foreach (DocumentObject docObj in paragraph.ChildObjects)
{
if (docObj.DocumentObjectType == DocumentObjectType.Picture)
{
DocPicture picture = docObj as DocPicture;
if (picture.Title == "Figure 1")
{
//Replace the image
picture.LoadImage(Image.FromFile("PinkRoses.jpg"));
}
}
}
}
//Save and close the document
document.SaveToFile("ReplaceImage.docx");
document.Close();
}
}
}
With the help of Spire.Doc, we can easily add and remove header on the word documents in C#. This article we will demonstrate how to lock down the header information from editing. We will divide it into two parts for the demo. Once is for locking the header information on the existing word document with header and the other is on the new creating word document.
How to lock the header information on the existing word document.
//Load the sample document with header
Document doc = new Document();
doc.LoadFromFile("sample.docx");
//Get the first section from the word document
Section section = doc.Sections[0];
//Protect the document and set the ProtectionType as AllowOnlyFormFields
doc.Protect(ProtectionType.AllowOnlyFormFields, "123");
//Set the ProtectForm as false to unprotect the section
section.ProtectForm = false;
//Save the document to file
doc.SaveToFile("Result.docx", FileFormat.Docx2013);
Effective screenshot of the header has been locked and the other area can be edited:

How to lock the header information for the new word document.
//Create a new instance of word document
Document doc = new Document();
//Add a section to the word document
Section section = doc.AddSection();
//Add header information to the section
HeaderFooter header = section.HeadersFooters.Header;
Paragraph HParagraph = header.AddParagraph();
TextRange HText = HParagraph.AppendText("Protect header");
//Add a paragraph to the section
Paragraph Para = section.AddParagraph();
Para.AppendText("Demo of how to lock the header information by Spire.Doc ");
//Set the ProtectionType as AllowOnlyFormFields and then unprotect the section
doc.Protect(ProtectionType.AllowOnlyFormFields, "123");
section.ProtectForm = false;
//Save the document to file
doc.SaveToFile("Result.docx", FileFormat.Docx2013);

With the help of Spire.Doc, developers can encrypt word with password, and also convert the word document to PDF. This article will show you how to convert Word to PDF with encrypted password for the resulted PDF file.
Make sure Spire.Doc for .NET Version 5.8.92 (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".
Here comes to the code snippets of how to create password encrypted PDF directly from word to PDF conversion.
Step 1: Create a new word document and load the document from file.
Document document = new Document(false);
document.LoadFromFile("Sample.docx");
Step 2: Create an instance of ToPdfParameterList.
ToPdfParameterList toPdf = new ToPdfParameterList();
Step 3: Set open password, permission password and user's permission over the PDF document
toPdf.PdfSecurity.Encrypt("open password","permission password", PdfPermissionsFlags.None, PdfEncryptionKeySize.Key128Bit);
Step 4: Save the document to file.
document.SaveToFile("EncryptedPDF.pdf",toPdf);
Effective screenshot:

Full codes:
using Spire.Doc;
namespace EncryptPDF
{
class Program
{
static void Main(string[] args)
{
Document document = new Document(false);
document.LoadFromFile("Sample.docx");
ToPdfParameterList toPdf = new ToPdfParameterList();
toPdf.PdfSecurity.Encrypt("open password","permission password", PdfPermissionsFlags.None, PdfEncryptionKeySize.Key128Bit);
document.SaveToFile("EncryptedPDF.pdf", toPdf);
}
}
}