Document security is particularly important when critical or private data is involved in a Word document. There are several security options in Word that you can use to protect your documents, including password protection, read-only mode, editing restrictions, and partial protection. On the contrary, when the protection is no longer required, you may need to unprotect a Word document to improve work efficiency.

In this article, you will learn how to protect or unprotect Word documents in C++ using Spire.Doc for C++.

Install Spire.Doc for C++

There are two ways to integrate Spire.Doc for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.Doc for C++ in a C++ Application

Password Protect a Word Document in C++

Encrypting a document with a password makes sure that only you and certain people can read or edit it. The following are the steps to password protect a Word document using Spire.Doc for C++.

  • Create a Document object.
  • Load a Word document using Document->LoadFromFile() method.
  • Encrypt the document with a password using Document->Eencrypt(LPCWSTR_S password) method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample.docx");

    //Encrypt the document with a password
    document->Encrypt(L"open-psd");

    //Save the document to another Word file
    document->SaveToFile(L"Output/Encryption.docx", FileFormat::Docx2013);
    document->Close();
}

C++: Protect or Unprotect Word Documents

Restrict Editing of a Word Document in C++

If you want to grant people permission to read your document but restrict the types of modifications that someone can make, you can protect your document with a specific protection type and a permission password.

Protection Type Description
AllowOnlyComments Modification of comments in the document is allowed.
AllowOnlyFormFields The user can only enter data in the form fields in the document.
AllowOnlyReading The document is read-only.
AllowOnlyRevisions The user can only add revision marks to the document.
NoProtection The document is not protected.

The following are the steps to restrict editing of a Word document using Spire.Doc for C++.

  • Create a Document object.
  • Load a Word document using Document->LoadFromFile() method.
  • Protect the document by specifying the protection type and the permission password using Document->Protect(Spire::Doc::ProtectionType type, LPCWSTR_S password) method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample.docx");

    //Protect the document by specifying the protection type and the password
    document->Protect(ProtectionType::AllowOnlyReading, L"permission-psd");

    //Save the document to another Word file
    document->SaveToFile(L"Output/RestrictEditing.docx", FileFormat::Docx2013);
    document->Close();
}

C++: Protect or Unprotect Word Documents

Protect Sections of a Word Document in C++

Word allows you to lock some sections of your Word document and leave the rest available for editing. The following are the steps to protect selected sections of a Word document using Spire.Doc for C++.

  • Create a Document object.
  • Load a Word document using Document->LoadFromFile() method.
  • Set the protection type to AllowOnlyFormFields using Document->Protect() method.
  • Unprotect a particular section by passing false as an argument to the Section->SetProtectForm() method. Other sections will continue to be protected.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample.docx");

    //Set the protection type as "AllowOnlyFormFields"
    document->Protect(ProtectionType::AllowOnlyFormFields, L"permission-psd");

    //Unprotect section 2
    document->GetSections()->GetItemInSectionCollection(1)->SetProtectForm(false);

    //Save the document to another Word file
    document->SaveToFile(L"Output/ProtectSections.docx", FileFormat::Docx2013);
    document->Close();
}

C++: Protect or Unprotect Word Documents

Create Editable Regions in a Word Document in C++

Aside from making certain sections editable, you can create editable regions based on text ranges in order to narrow down the scope of changes that users are able to make. The following are the steps to create editable regions in a read-only Word document using Spire.Doc for C++.

  • Create a Document object.
  • Load a Word file using Document->LoadFromFile() method.
  • Set the protection type to AllowOnlyReading using Document->Protect() method.
  • Create a PermissionStart object and a PermissionEnd object.
  • Insert the PermissionStart object at the beginning of a paragraph using DocumentObjectCollection->Insert(int index, Spire::Doc::lDocumentObject *entity) method, which indicates the start of an editable region.
  • Add the PermissionEnd object at the end of a paragraph using DocumentObjectCollection->Add(Spire::Doc::lDocumentObject *entity) method, which indicates the end of an editable region.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample2.docx");

    //Set the protect type to AllowOnlyReading
    document->Protect(ProtectionType::AllowOnlyReading, L"permission-psd");

    //Create tags for permission start and end
    intrusive_ptr<PermissionStart> start = new PermissionStart(document, L"regionOne");
    intrusive_ptr<PermissionEnd> end = new PermissionEnd(document, L"regionOne");

    //Add the start and end tags to allow the selected paragraphs to be editable
    document->GetSections()->GetItemInSectionCollection(0)->GetParagraphs()->GetItemInParagraphCollection(0)->GetChildObjects()->Insert(0, start);
    document->GetSections()->GetItemInSectionCollection(0)->GetParagraphs()->GetItemInParagraphCollection(1)->GetChildObjects()->Add(end);

    //Save the document to another Word file
    document->SaveToFile(L"Output/SetEditableRegions.docx", FileFormat::Docx2013);
    document->Close();
}

C++: Protect or Unprotect Word Documents

Remove Editable Regions from a Word Document in C++

In order to remove the editable regions, you need to find the "PermissionStart" and "PermissionEnd" tags in the document and remove them. The following are the detailed steps.

  • Create a Document object.
  • Load a Word document containing editable regions using Document->LoadFromFile() method.
  • Traverse through all the child objects in the document, and determine if a certain child object is an instance of PermissionStart class or PermissionEnd class. If yes, remove the child object from the paragraph using Paragraph->GetChildObjects()->Remove(Spire::Doc::IDocumentObject *entity) method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

 //Create a Document object
 intrusive_ptr<Document> document = new Document();

 //Load a Word file
 document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\SetEditableRegions.docx");

 //Find "PermissionStart" and "PermissionEnd" tags and remove them
 for (int i = 0; i < document->GetSections()->GetCount(); i++)
 {
  intrusive_ptr<Section> section = document->GetSections()->GetItemInSectionCollection(i);
  for (int j = 0; j < section->GetBody()->GetParagraphs()->GetCount(); j++)
  {
   intrusive_ptr<Paragraph> para = section->GetBody()->GetParagraphs()->GetItemInParagraphCollection(j);
   for (int k = 0; k < para->GetChildObjects()->GetCount(); k++)
   {
    intrusive_ptr<DocumentObject> obj = para->GetChildObjects()->GetItem(k);
    if (Object::Dynamic_cast<PermissionStart>(obj) != nullptr || Object::Dynamic_cast<PermissionEnd>(obj) != nullptr)
    {
     para->GetChildObjects()->Remove(obj);
    }
    else
    {
     k++;
    }
   }
  }
 }

 //Save the document to another Word file
 document->SaveToFile(L"Output/RemoveEditableRegions.docx", FileFormat::Docx2013);
 document->Close();
}

Remove Restrictions from a Word Document in C++

Spire.Doc for C++ allows you to remove editing restrictions without knowing the permission password. The following are the detailed steps.

  • Create a Document object.
  • Load a Word document containing editing restrictions using Document->LoadFromFile() method.
  • Set the protection type to NoProtection using Document->Protect() method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\RestrictEditing.docx");

    //Set the protect type to NoProtection
    document->Protect(ProtectionType::NoProtection);

    //Save the document to another Word file
    document->SaveToFile(L"Output/RemoveRestrictions.docx", FileFormat::Docx2013);
    document->Close();
}

Remove Password from a Password-Protected Word Document in C++

The password of an encrypted Word document can be removed if it is no longer needed. The following are the detailed steps.

  • Create a Document object.
  • Load a password-protected Word document using Document->LoadFromFile((LPCWSTR_S fileName, Spire::Doc::FileFormat fileFormat, LPCWSTR_S password) method.
  • Remove the password using Document->RemoveEncryption() method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Load an encrypted Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\Encryption.docx", FileFormat::Docx, L"open-psd");

    //Remove the open password
    document->RemoveEncryption();

    //Save the document to another Word file
    document->SaveToFile(L"Output/RemovePassword.docx", FileFormat::Docx2013);
    document->Close();
}

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.

Excel and CSV (Comma-Separated Values) are two commonly used file formats for managing and storing tabular data in various industries. Excel organizes data in rows and columns and provides users with a wide range of advanced features for data manipulation, analysis and visualization, whereas CSV stores data in a plain text format that is lightweight and highly compatible with various applications. Converting Excel files to CSV format can be very helpful when users need to exchange or import Excel data in different programs. Conversely, users who require more advanced data analysis features, such as creating charts or applying formulas, may find it beneficial to convert CSV files to Excel format. In this article, we will demonstrate how to convert Excel to CSV or CSV to Excel in C++ using Spire.XLS for C++.

Install Spire.XLS for C++

There are two ways to integrate Spire.XLS for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.XLS for C++ in a C++ Application

Convert Excel to CSV in C++

Spire.XLS for C++ offers the XlsWorksheet->SaveToFile (LPCWSTR_S fileName, LPCWSTR_S separator, Spire::Common::Encoding* encoding) method to convert a worksheet in an Excel file to CSV. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook->LoadFromFile() method.
  • Get a specific worksheet in the workbook by its index using Workbook->GetWorksheets()->Get(int index) method.
  • Save the worksheet to a CSV file using XlsWorksheet->SaveToFile (LPCWSTR_S fileName, LPCWSTR_S separator, Spire::Common::Encoding* encoding) method.
  • C++
#include "Spire.Xls.o.h"

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the Workbook class
	intrusive_ptr<Workbook> workbook = new Workbook();

	//Load an Excel file
	workbook->LoadFromFile(L"Input.xlsx");

	//Get the first worksheet
	intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));

	//Save the worksheet to a CSV file
	sheet->SaveToFile(L"ExcelToCsv.csv", L",", Encoding::GetUTF8());
	workbook->Dispose();
}

C++: Convert Excel to CSV or CSV to Excel

Convert Visible Data in Excel to CSV in C++

When converting an Excel worksheet to CSV using the above code, all data, including both visible and hidden data, will be saved to CSV. If you only want to save visible data in the worksheet to CSV, you can use the XlsWorksheet->SaveToFile (LPCWSTR_S fileName, LPCWSTR_S separator, bool retainHiddenData) method. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook->LoadFromFile() method.
  • Get a specific worksheet in the workbook by its index using Workbook->GetWorksheets()->Get(int index) method.
  • Save visible data in the worksheet to a CSV file using XlsWorksheet->SaveToFile (LPCWSTR_S fileName, LPCWSTR_S separator, bool retainHiddenData) method.
  • C++
#include "Spire.Xls.o.h"

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the Workbook class
	intrusive_ptr<Workbook> workbook = new Workbook();

	//Load an Excel file
	workbook->LoadFromFile(L"Input.xlsx");

	//Get the first worksheet
	intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));

	//Save visible data in the worksheet to a CSV file
	sheet->SaveToFile(L"ExcelToCsv1.csv", L",", false);
	workbook->Dispose();
}

C++: Convert Excel to CSV or CSV to Excel

Convert CSV to Excel in C++

To convert a CSV file to Excel, you need to load the CSV file using Workbook->LoadFromFile(LPCWSTR_S fileName, LPCWSTR_S separator) method, then use the Workbook->SaveToFile (LPCWSTR_S fileName, ExcelVersion version) method to save it to an Excel file. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load a CSV file with separator using Workbook->LoadFromFile(LPCWSTR_S fileName,LPCWSTR_S separator) method.
  • Get a specific worksheet in the file by its index using Workbook->GetWorksheets()->Get(int index) method.
  • Set ignore error option to ignore errors when saving numbers in a specific cell range as text using Worksheet->GetRange(LPCWSTR_S name)->SetIgnoreErrorOptions(IgnoreErrorType::NumberAsText) method.
  • Auto-fit column widths using Worksheet->GetAllocatedRange()->AutoFitColumns() method.
  • Save the CSV file to an Excel file using Workbook->SaveToFile (LPCWSTR_S fileName, ExcelVersion version) method.
  • C++
#include "Spire.Xls.o.h" 

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the Workbook class
	intrusive_ptr<Workbook> workbook = new Workbook();

	//Load a CSV file with separator
	workbook->LoadFromFile(L"ExcelToCSV.csv", L",");

	//Get the first worksheet
	intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));

	//Set ignore error option to ignore errors when saving numbers in a specific cell range as text
	sheet->GetRange(L"C2:C11")->SetIgnoreErrorOptions(IgnoreErrorType::NumberAsText);

	//Auto-fit column widths
	sheet->GetAllocatedRange()->AutoFitColumns();

	//Save the file to an Excel XLSX file
	workbook->SaveToFile(L"CsvToExcel.xlsx", ExcelVersion::Version2013);
	workbook->Dispose();
}

C++: Convert Excel to CSV or CSV to Excel

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.

In Word, a watermark is a semi-transparent text or image that you can place in the background. Typically, watermarks are used to emphasize something important about a document. For example, you can use it to remind the user that the content is confidential or a draft. Or other times, you may want to add a washout mark to include the company logo in the document. In this article, you will learn how to insert text or image watermarks to Word documents in C++ using Spire.Doc for C++.

Install Spire.Doc for C++

There are two ways to integrate Spire.Doc for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.Doc for C++ in a C++ Application

Insert a Text Watermark to Word in C++

Spire.Doc for C++ offers the TextWatermark class to represent a text watermark. The content and appearance of the watermark can be set using the methods under it. Once a text watermark is created, you can apply it to the whole document by using Document->SetWatermark() method. The following are the detailed steps.

  • Create a Document object.
  • Load a Word file using Document->LoadFromFile() method.
  • Create a TextWatermark object.
  • Se the content and appearance of the watermark using the methods under the TextWatermark object.
  • Apply the text watermark to the document using Document->SetWatermrak() method.
  • Save the document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample.docx");

    //Create a TextWatermark object
    intrusive_ptr<TextWatermark> txtWatermark = new TextWatermark();

    //Set the content and format of the text watermark
    txtWatermark->SetText(L"CONFIDENTIAL");
    txtWatermark->SetFontSize(40);
    txtWatermark->SetSemitransparent(90);
    txtWatermark->SetFontName(L"Arial Black");
    txtWatermark->SetColor(Color::GetBlue());
    txtWatermark->SetLayout(WatermarkLayout::Diagonal);

    //Apply the text watermark to the document
    document->SetWatermark(txtWatermark);

    //Save the document
    document->SaveToFile(L"Output/TextWatermark.docx", FileFormat::Docx2013);
    document->Close();
}

C++: Insert Text or Image Watermarks to Word

Insert an Image Watermark to Word in C++

Likewise, an image watermark can be created using PictureWatermark class. Once created, you can apply it to a Word document using Document->SetWatermark() method. Here are the detailed steps.

  • Create a Document object.
  • Load a Word file using Document->LoadFromFile() method.
  • Create a PictureWatermark object.
  • Set the image and appearance of the watermark using the methods under the PictureWatermark object.
  • Apply the image watermark to the document using Document->SetWatermrak() method.
  • Save the document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample.docx");

    //Create a PictureWatermark object
    intrusive_ptr<PictureWatermark> pictureWatermark = new PictureWatermark();

    //Specify image for the watermark
    pictureWatermark->SetPicture(L"C:\\Users\\Administrator\\Desktop\\company-png.png");
    pictureWatermark->SetScaling(100);
    pictureWatermark->SetIsWashout(false);

    //Apply the image watermark to the document
    document->SetWatermark(pictureWatermark);

    //Save the document 
    document->SaveToFile(L"Output/ImageWatermark.docx", FileFormat::Docx2013);
    document->Close();
}

C++: Insert Text or Image Watermarks to 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.

page 103