Knowledgebase (2328)
Children categories
Data bars in Excel are a built-in type of conditional formatting that inserts colored bars in cells to compare the values within them. The length of a bar depends on the value of a cell and the longest bar corresponds to the largest value in a selected data range, which allows you to spot it at a glance. In this article, you will learn how to add data bars in a cell range using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS 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.XLS
Add Data Bars in Excel in C# and VB.NET
Data bars are a great tool for visually comparing data within a selected range of cells. With Spire.XLS for .NET, you are allowed to add a data bar to a specified data range and also set its format. The following are the detailed steps.
- Create a Workbook instance.
- Load a sample Excel document using Workbook.LoadFromFile() method.
- Get a specified worksheet using Workbook.Worsheets[index] property.
- Add a conditional formatting to the worksheet using Worksheet.ConditionalFormats.Add() method and return an object of XlsConditionalFormats class.
- Set the cell range where the conditional formatting will be applied using XlsConditionalFormats.AddRange() method.
- Add a condition using XlsConditionalFormats.AddCondition() method, and then set its format type to DataBar using IConditionalFormat.FormatType property.
- Set the fill effect and color of the data bars using IConditionalFormat.DataBar.BarFillType and IConditionalFormat.DataBar.BarColor properties.
- Save the result document using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
using Spire.Xls.Core;
using Spire.Xls.Core.Spreadsheet.Collections;
using Spire.Xls.Core.Spreadsheet.ConditionalFormatting;
using System.Drawing;
namespace ApplyDataBar
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load a sample Excel docuemnt
workbook.LoadFromFile("sample.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Add a conditional format to the worksheet
XlsConditionalFormats xcfs = sheet.ConditionalFormats.Add();
//Set the range where the conditional format will be applied
xcfs.AddRange(sheet.Range["C2:C13"]);
//Add a condition and set its format type to DataBar
IConditionalFormat format = xcfs.AddCondition();
format.FormatType = ConditionalFormatType.DataBar;
//Set the fill effect and color of the data bars
format.DataBar.BarFillType = DataBarFillType.DataBarFillGradient;
format.DataBar.BarColor = Color.Red;
//Save the result document
workbook.SaveToFile("ApplyDataBarsToDataRange.xlsx", ExcelVersion.Version2013);
}
}
}

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.
As increasingly diverse types of documents are created in PDF, you may find yourself with a need to protect confidential information. Although many other PDF security options are available to keep confidential information secure, the most common approach is to add a custom watermark to the PDF document. In this article, you will learn how to add a single-line or multi-line text watermark to PDF in C++ using Spire.PDF for C++.
Install Spire.PDF for C++
There are two ways to integrate Spire.PDF 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.PDF for C++ in a C++ Application
Add a Single-Line Text Watermark to PDF in C++
Spire.PDF for C++ does not provide an interface or a class responsible for inserting watermarks into PDF files. You could, however, draw text like "confidential", "do not copy", or "draft" on each page to mimic the watermark effect. The following are the steps to add a single-line text watermark to a PDF document.
- Create a PdfDocument object.
- Load a sample PDF document using PdfDocument->LoadFromFile() method.
- Get a specific page from the document using PdfDocument->GetPages()->GetItem() method.
- Translate the coordinate system to the specified coordinate using PdfPageBase->GetCanvas()->TraslateTransform() method.
- Rotate the coordinate system 45 degrees counterclockwise using PdfPageBase->GetCanvas()->RotateTransform() method. This step and the step above make sure that the watermark will appear in the middle of the page with an angle of 45 degrees.
- Draw a text watermark on the page using PdfPageBase->GetCanvas()->DrawString() method.
- Save the document to a different PDF file using PdfDocument->SaveToFile() method.
- C++
"#include ""Spire.Pdf.o.h"";
using namespace std;
using namespace Spire::Pdf;
using namespace Spire::Common;
int main()
{
//Specify input file and output file paths
wstring inputFilePath = L""C:\\Users\\Administrator\\Desktop\\sample.pdf"";
wstring outputFilePath = L""Output\\SingleLineTextWatermark.pdf"";
//Create a PdfDocument object
PdfDocument* doc = new PdfDocument();
//Load a PDF file
doc->LoadFromFile(inputFilePath.c_str());
//Create a true type font
PdfTrueTypeFont* font = new PdfTrueTypeFont(L""Arial"", 50.0f, PdfFontStyle::Bold, true);
//Create a brush
PdfBrush* brush = PdfBrushes::GetDarkGray();
//Specify watermark text
wstring text = L""CONFIDENTIAL"";
//Measure the text size
SizeF textSize = font->MeasureString(text.c_str());
//Calculate two offsets, which are used to calculate the translation amount of coordinate system
float offset1 = (float)(textSize.GetWidth() * sqrt(2) / 4);
float offset2 = (float)(textSize.GetHeight() * sqrt(2) / 4);
//Traverse through the pages in the document
for (size_t i = 0; i < doc->GetPages()->GetCount(); i++)
{
//Get a specific page
PdfPageBase* page = doc->GetPages()->GetItem(i);
//Set the page transparency
page->GetCanvas()->SetTransparency(0.8);
//Translate the coordinate system to a specified coordinate
page->GetCanvas()->TranslateTransform(page->GetCanvas()->GetSize()->GetWidth() / 2 - offset1 - offset2, page->GetCanvas()->GetSize()->GetHeight() / 2 + offset1 - offset2);
//Rotate the coordinate system 45 degrees counterclockwise
page->GetCanvas()->RotateTransform(-45);
//Draw watermark text on the page
page->GetCanvas()->DrawString(text.c_str(), font, brush, 0, 0, new PdfStringFormat(PdfTextAlignment::Left));
}
//Save the document
doc->SaveToFile(outputFilePath.c_str());
doc->Close();
delete doc;
}"

Add a Tiled Text Watermark to PDF in C++
To achieve the tiled watermark effect, you can make use of the PdfTilingBrush class. The tiling brush produces a tiled pattern that is repeated to fill a graphics area. The following are the steps to add a tiled text watermark to a PDF document.
- Create a custom method InsertTiledTextWatermark(PdfPageBase* page, wstring watermarkText, PdfTrueTypeFont* font, int rowNum, int columnNum) to add a tiled watermark to a PDF page. The parameter rowNum and columnNum specify the row number and column number of the tiled watermark.
- Create a PdfDocument object.
- Load a sample PDF document using PdfDocument->LoadFromFile() method.
- Traverse through all pages in the document, and call the custom method InsertTiledTextWatermark() to apply watermark to each page.
- Save the document to another file using PdfDocument->SaveToFile() method.
- C++
#include "Spire.Pdf.o.h";
using namespace std;
using namespace Spire::Pdf;
static void InsertTiledTextWatermark(PdfPageBase* page, wstring watermarkText, PdfTrueTypeFont* font, int rowNum, int columnNum)
{
//Measure text size
SizeF textSize = font->MeasureString(watermarkText.c_str());
//Calculate two offsets, which are used to calculate the translation amount of coordinate system
float offset1 = (float)(textSize.GetWidth() * sqrt(2) / 4);
float offset2 = (float)(textSize.GetHeight() * sqrt(2) / 4);
//Get page height and width
float height = page->GetActualSize()->GetHeight();
float width = page->GetActualSize()->GetWidth();
//Create a tiling brush
PdfTilingBrush* brush = new PdfTilingBrush(new SizeF(width / columnNum, height / rowNum));
brush->GetGraphics()->SetTransparency(0.5f);
brush->GetGraphics()->TranslateTransform(brush->GetSize()->GetWidth() / 2 - offset1 - offset2, brush->GetSize()->GetHeight() / 2 + offset1 - offset2);
brush->GetGraphics()->RotateTransform(-45);
//Draw watermark text on the brush
brush->GetGraphics()->DrawString(watermarkText.c_str(), font, PdfBrushes::GetRed(), 0, 0, new PdfStringFormat(PdfTextAlignment::Left));
//Draw a rectangle (that covers the whole page) using the tiling brush
page->GetCanvas()->DrawRectangle(brush, new RectangleF(new PointF(0, 0), page->GetActualSize()));
}
int main()
{
//Specify input file and output file paths
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.pdf";
wstring outputFilePath = L"Output\\MultiLineTextWatermark.pdf";
//Create a PdfDocument object
PdfDocument* doc = new PdfDocument();
//Load a PDF file
doc->LoadFromFile(inputFilePath.c_str());
//Specify watermark text
wstring text = L"CONFIDENTIAL";
//Creat a true type font
PdfTrueTypeFont* font = new PdfTrueTypeFont(L"Arial", 20.0f, PdfFontStyle::Bold, true);
//Traverse through the pages in the document
for (size_t i = 0; i < doc->GetPages()->GetCount(); i++)
{
//Call the custom method to insert multi-line text watermark
boost::intrusive_ptr page = doc->GetPages()->GetItem(i);
InsertTiledTextWatermark(page.get(), text.c_str(), font, 3, 3);"
}
//Save the document
doc->SaveToFile(outputFilePath.c_str());
doc->Close();
delete doc;
}

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.
Adding documents as attachments to PDF brings you a lot of convenience. For example, you can transfer multiple documents as a single document; you can open another file inside a PDF document without needing to find the document from other places; you reduce the possibility of losing documents referenced in the PDF document.
Spire.PDF for C++ allows you to attach files in two ways:
- Document Level Attachment (or Regular Attachment): A document-level attachment refers to the attachment that’s added to the Attachment tab and cannot be found on a specific page.
- Annotation Attachment: An annotation attachment refers to the attachment that’s added to a specific position of a page. Annotation attachments are shown as a paper clip icon on the page; reviewers can double-click the icon to open the file.
This article will show you how to add or delete regular attachments and annotation attachments in a PDF document in C++ using Spire.PDF for C++.
- Add a Regular Attachment to PDF in C++
- Add an Annotation Attachment to PDF in C++
- Delete Regular Attachments in PDF in C++
- Delete Annotation Attachments in PDF in C++
Install Spire.PDF for C++
There are two ways to integrate Spire.PDF 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.PDF for C++ in a C++ Application
Add a Regular Attachment to PDF in C++
To add a regular attachment, use PdfDocument->GetAttachments()->Add() method. The following are the detailed steps.
- Create a PdfDocument object.
- Load a PDF document using PdfDocument->LoadFromFile() method.
- Create a PdfAttachment object based on an external file.
- Add the attachment to PDF using PdfDocument->GetAttachments()->Add() method.
- Save the document to another PDF file using PdfDocument.SaveToFile() method.
- C++
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main() {
//Specify input file path
wstring inputPdfPath = L"C:\\Users\\Administrator\\Desktop\\Sample.pdf";
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\Data.xlsx";
//Specify output file path
wstring outputFilePath = L"Output\\Attachment.pdf";
//Create a PdfDocument object
PdfDocument* doc = new PdfDocument();
//Load a sample PDF file
doc->LoadFromFile(inputPdfPath.c_str());
//Create a PdfAttachment object based on an external file
PdfAttachment* attachment = new PdfAttachment(inputFilePath.c_str());
//Add the attachment to PDF
doc->GetAttachments()->Add(attachment);
//Save to file
doc->SaveToFile(outputFilePath.c_str());
delete doc;
}

Add an Annotation Attachment to PDF in C++
An annotation attachment is represented by the PdfAttachmentAnnotation class. You need to create an instance of the class based on an external file, and then add it to a specific page using PdfPageBase->GetAnnotationsWidget()->Add() method. The following are the detailed steps.
- Create a PdfDocument object.
- Load a PDF document using PdfDocument->LoadFromFile() method.
- Get a specific page to add annotation using PdfDocument->GetPages()->GetItem() method.
- Create a PdfAttachmentAnnotation object based on an external file.
- Add the annotation attachment to the page using PdfPageBase->GetAnnotationsWidget->Add() method.
- Save the document using PdfDocument->SaveToFile() method.
- C++
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main() {
//Specify input file path
wstring inputPdfPath = L"C:\\Users\\Administrator\\Desktop\\Attachment.pdf";
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\Report.docx";
//Specify output file path
wstring outputFilePath = L"Output\\AnnotationAttachment.pdf";
//Create a PdfDocument object
PdfDocument* doc = new PdfDocument();
//Load a sample PDF file
doc->LoadFromFile(inputPdfPath.c_str());
//Get a specific page
boost::intrusive_ptr<PdfPageBase> page = doc->GetPages()->GetItem(0);
//Draw a label on PDF
wstring label = L"Here is the report:";
PdfTrueTypeFont* font = new PdfTrueTypeFont(L"Arial", 13.0f, PdfFontStyle::Bold, true);
float x = 35;
float y = doc->GetPages()->GetItem(0)->GetActualSize()->GetHeight() - 220;
page->GetCanvas()->DrawString(label.c_str(), font, PdfBrushes::GetRed(), x, y);
//Convert the file to be attached to stream
ifstream is1(inputFilePath.c_str(), ifstream::in | ios::binary);
is1.seekg(0, is1.end);
int length1 = is1.tellg();
is1.seekg(0, is1.beg);
char* buffer1 = new char[length1];
is1.read(buffer1, length1);
Stream* stream = new Spire::Pdf::Stream((unsigned char*)buffer1, length1);
boost::intrusive_ptr <SizeF> size = font->MeasureString(label.c_str());
RectangleF* bounds = new RectangleF((float)(x + size->GetWidth() + 5), (float)y, 10, 15);
//Create a PdfAttachmentAnnotation object based on the file
PdfAttachmentAnnotation* annotation = new PdfAttachmentAnnotation(bounds, L"Report.docx", stream);
annotation->SetColor(new PdfRGBColor(Spire::Pdf::Color::GetDarkOrange()));
annotation->SetFlags(PdfAnnotationFlags::ReadOnly);
annotation->SetIcon(PdfAttachmentIcon::Graph);
annotation->SetText(L"Click here to open the file");
//Add the attachment annotation to PDF
page->GetAnnotationsWidget()->Add(annotation);
//Save to file
doc->SaveToFile(outputFilePath.c_str());
delete doc;
}

Delete Regular Attachments in PDF in C++
The PdfDocument->GetAttachments() method returns a collection of regular attachments of a PDF document. A specific attachment or all attachments can be removed by using PdfAttachmentCollection->RemoveAt() method or PdfAttachmentCollection->Clear() method. The detailed steps are as follows.
- Create a PdfDocument object.
- Load a PDF document using PdfDocument->LoadFromFile() method.
- Get the attachment collection from the document using PdfDocument->GetAttachments() method.
- Remove a specific attachment using PdfAttachmentCollection->RemoveAt() method. To remove all attachments at once, use PdfAttachmentCollection->Clear() method.
- Save the document using PdfDocument->SaveToFile() method.
- C++
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main() {
//Specify input file path
wstring inputPdfPath = L"C:\\Users\\Administrator\\Desktop\\Sample.pdf";
//Specify output file path
wstring outputFilePath = L"Output\\DeleteAttachments.pdf";
//Create a PdfDocument object
PdfDocument* doc = new PdfDocument();
//Load a PDF file
doc->LoadFromFile(inputPdfPath.c_str());
//Get all attachments
boost::intrusive_ptr<PdfAttachmentCollection> attachments = doc->GetAttachments();
//Delete all attachments
attachments->Clear();
//Delete a specific attachment
//attachments->RemoveAt(0);
//Save the document
doc->SaveToFile(outputFilePath.c_str());
doc->Close();
delete doc;
}
Delete Annotation Attachments in PDF in C++
The annotation is a page-based element. You can get annotations from a specific page using PdfPageBase->GetAnnotationsWidget() method, and determine if a certain annotation is an annotation attachment. After that, remove the annotation attachment from the annotation collection using PdfAnnotationCollection->Remove() method. The following are the detailed steps.
- Create a PdfDocument object.
- Load a PDF document using PdfDocument->LoadFromFile() method.
- Get the annotation collection from a specific page using PdfPageBase->GetAnnotationsWidget() method.
- Determine if an annotation is an instance of PdfAttachmentAnnotationWidget. If yes, remove the annotation attachment using PdfAnnotationCollection->Remove() method.
- Save the document using PdfDocument->SaveToFile() method.
- C++
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main() {
//Specify input file path
wstring inputPdfPath = L"C:\\Users\\Administrator\\Desktop\\AnnotationAttachment.pdf";
//Specify output file path
wstring outputFilePath = L"Output\\DeleteAnnotationAttachments.pdf";
//Create a PdfDocument object
PdfDocument* doc = new PdfDocument();
//Load a PDF file
doc->LoadFromFile(inputPdfPath.c_str());
//Loop through the pages
for (int i = 0; i < doc->GetPages()->GetCount(); i++)
{
//Get the annotation collection
boost::intrusive_ptr <PdfAnnotationCollection> annotationCollection = doc->GetPages()->GetItem(i)->GetAnnotationsWidget();
//Loop through the annotations
for (int j = 0; j < annotationCollection->GetCount(); j++)
{
//Get a specific annotation
boost::intrusive_ptr <PdfAnnotation> annotation = annotationCollection->GetItem(j);
//Determine if the annotation is an instance of PdfAttachmentAnnotationWidget
wstring content;
wchar_t nm_w[100];
swprintf(nm_w, 100, L"%hs", typeid(PdfAttachmentAnnotationWidget).name());
LPCWSTR_S newName = nm_w;
if (wcscmp(newName, annotation->GetInstanceTypeName()) == 0) {
//Remove the annotation attachment
annotationCollection->RemoveAt(j);
}
}
}
//Save the document
doc->SaveToFile(outputFilePath.c_str());
doc->Close();
delete doc;
}
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.