.NET (1273)
Children categories
The sample demonstrates how to set number style in an excel workbook.
Barcodes provide a quick and efficient way to identify and track items, making them indispensable in a variety of industries. By adding barcodes to PDFs, companies can enhance their document management processes, allowing for a more streamlined way to handle and track PDF files. Additionally, this operation also enables the creation of dynamic, interactive PDF documents that not only contain traditional text and images, but also integrate the functionality of barcodes. In this article, you will learn how to add barcodes to PDF in C# using Spire.PDF for .NET and Spire.Barcode for .NET.
Install Spire.PDF for .NET
To begin with, you need to download Spire.PDF for.NET and Spire.Barcode for .NET libraries, then add the DLL files included in both product packages as references in your .NET project. Or you can install them via NuGet.
PM> Install-Package Spire.PDF PM> Install-Package Spire.Barcode
Add Barcodes to PDF in C#
Spire.PDF for .NET support several 1D barcode types represented by different classes, such as PdfCodabarBarcode, PdfCode128ABarcode, PdfCode32Barcode, PdfCode39Barcode, PdfCode93Barcode.
Each class provides corresponding properties for setting the barcode text, size, color, etc. The following are the steps to draw the common Codabar, Code128, Code39 and Code93 barcodes at the specified locations on a PDF page.
- Create a PdfDocument object.
- Add a PDF page using PdfDocument.Pages.Add() method.
- Create a PdfTextWidget object and draw text on the page using PdfTextWidget.Draw( PdfPageBase page, float x, float y) method.
- Create PdfCodabarBarcode, PdfCode128ABarcode, PdfCode39Barcode, PdfCode93Barcode objects.
- Set the gap between the barcode and the displayed text through the BarcodeToTextGapHeight property of the corresponding classes.
- Sets the barcode text display location through the TextDisplayLocation property of the corresponding classes.
- Set the barcode text color through the TextColor property of the corresponding classes.
- Draw the barcodes at specified locations on the PDF page using the Draw(PdfPageBase page, PointF location) method of the corresponding classes.
- Save the result PDF file using PdfDocument.SaveToFile() method.
- C#
using Spire.Pdf; using Spire.Pdf.Barcode; using Spire.Pdf.Graphics; using System.Drawing; namespace PDFBarcode { class Program { static void Main(string[] args) { //Create a PDF document PdfDocument pdf = new PdfDocument(); //Add a page PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4); //Initialize y-coordinate float y = 20; //Create a true type font PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold), true); //Draw text on the page PdfTextWidget text = new PdfTextWidget(); text.Font = font; text.Text = "Codabar:"; PdfLayoutResult result = text.Draw(page, 0, y); page = result.Page; y = result.Bounds.Bottom + 2; //Draw Codabar barcode on the page PdfCodabarBarcode Codabar = new PdfCodabarBarcode("00:12-3456/7890"); Codabar.BarcodeToTextGapHeight = 1f; Codabar.TextDisplayLocation = TextLocation.Bottom; Codabar.TextColor = Color.Blue; Codabar.Draw(page, new PointF(0, y)); //Draw text on the page text.Text = "Code128-A:"; result = text.Draw(page, 240, 20); page = result.Page; y = result.Bounds.Bottom + 2; //Draw Code128-A barcode on the page PdfCode128ABarcode Code128 = new PdfCode128ABarcode("HELLO 00-123"); Code128.BarcodeToTextGapHeight = 1f; Code128.TextDisplayLocation = TextLocation.Bottom; Code128.TextColor = Color.Blue; Code128.Draw(page, new PointF(240, y)); //Draw text on the page text.Text = "Code39:"; result = text.Draw(page, 0, Codabar.Bounds.Bottom + 8); page = result.Page; y = result.Bounds.Bottom + 2; //Draw Code39 barcode on the page PdfCode39Barcode Code39 = new PdfCode39Barcode("16-273849"); Code39.BarcodeToTextGapHeight = 1f; Code39.TextDisplayLocation = TextLocation.Bottom; Code39.TextColor = Color.Blue; Code39.Draw(page, new PointF(0, y)); //Draw text on the page text.Text = "Code93:"; result = text.Draw(page, 240, Code128.Bounds.Bottom + 8); page = result.Page; y = result.Bounds.Bottom + 2; //Draw Code93 barcode on the page PdfCode93Barcode Code93 = new PdfCode93Barcode("16-273849"); Code93.BarcodeToTextGapHeight = 1f; Code93.TextDisplayLocation = TextLocation.Bottom; Code93.TextColor = Color.Blue; Code93.QuietZone.Bottom = 5; Code93.Draw(page, new PointF(240, y)); //Save the document pdf.SaveToFile("AddBarcodes.pdf"); pdf.Close(); } } }
Add QR Codes to PDF in C#
To add 2D barcodes to a PDF file, the Spire.Barcode for .NET library is required to generate QR code first, and then you can add the QR code image to the PDF file with the Spire.PDF for .NET library. The following are the detailed steps.
- Create a PdfDocument object.
- Add a PDF page using PdfDocument.Pages.Add() method.
- Create a BarcodeSettings object.
- Call the corresponding properties of the BarcodeSettings class to set the barcode type, data, error correction level and width, etc.
- Create a BarCodeGenerator object based on the settings.
- Generate QR code image using BarCodeGenerator.GenerateImage() method.
- Draw the QR code image at a specified location on the PDF page using PdfPageBase.Canvas.DrawImage(PdfImage image, float x, float y) method.
- Save the result PDF file using PdfDocument.SaveToFile() method.
- C#
using System.Drawing; using Spire.Barcode; using Spire.Pdf; using Spire.Pdf.Graphics; namespace PDFQRcode { class Program { static void Main(string[] args) { //Create a PDF document PdfDocument pdf = new PdfDocument(); //Add a page PdfPageBase page = pdf.Pages.Add(); //Create a BarcodeSettings object BarcodeSettings settings = new BarcodeSettings(); //Set the barcode type to QR Code settings.Type = BarCodeType.QRCode; //Set the data of the QR code settings.Data = "E-iceblue"; settings.Data2D = "E-iceblue"; //Set the width of the QR code settings.X = 2.5f; //Set the error correction level of the QR code settings.QRCodeECL = QRCodeECL.Q; //Set to show QR code text at the bottom settings.ShowTextOnBottom = true; //Generate QR code image based on the settings BarCodeGenerator generator = new BarCodeGenerator(settings); Image QRimage = generator.GenerateImage(); //Initialize y-coordinate float y = 20; //Create a true type font PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold), true); //Draw text on the PDF page PdfTextWidget text = new PdfTextWidget(); text.Font = font; text.Text = "QRCode:"; PdfLayoutResult result = text.Draw(page, 0, y); y = result.Bounds.Bottom + 2; //Draw QR code image on the PDF page PdfImage pdfImage = PdfImage.FromImage(QRimage); page.Canvas.DrawImage(pdfImage, 0, y); //Save the document pdf.SaveToFile("PdfQRCode.pdf"); } } }
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.
Spire.PDF can help us draw different shapes in PDF document. We can use Spire.PDF to draw rectangles in PDF, draw circles in PDF, draw arcs in PDF and draw ellipses in PDF. Besides these shapes, we can also use Spire.PDF to draw some other special shapes such as Spiral and five-pointed stars.
Draw Five-Pointed Star in PDF
By using Spire.PDF, we can draw different stars in PDF document. The sample below is showing how to draw 6 types of different five-pointed star.
using Spire.Pdf; using Spire.Pdf.Graphics; using System; using System.Drawing; namespace FivePointedStar { class Program { private static void DrawStar(PdfPageBase page) { PointF[] points = new PointF[5]; for (int i = 0; i < points.Length; i++) { float x = (float)Math.Cos(i * 2 * Math.PI / 5); float y = (float)Math.Sin(i * 2 * Math.PI / 5); points[i] = new PointF(x, y); } PdfPath path = new PdfPath(); path.AddLine(points[2], points[0]); path.AddLine(points[0], points[3]); path.AddLine(points[3], points[1]); path.AddLine(points[1], points[4]); path.AddLine(points[4], points[2]); //save graphics state PdfGraphicsState state = page.Canvas.Save(); PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f); PdfBrush brush1 = new PdfSolidBrush(Color.CadetBlue); page.Canvas.ScaleTransform(50f, 50f); page.Canvas.TranslateTransform(5f, 1.2f); page.Canvas.DrawPath(pen, path); page.Canvas.TranslateTransform(2f, 0f); path.FillMode = PdfFillMode.Alternate; page.Canvas.DrawPath(pen, brush1, path); page.Canvas.TranslateTransform(2f, 0f); path.FillMode = PdfFillMode.Winding; page.Canvas.DrawPath(pen, brush1, path); PdfLinearGradientBrush brush2 = new PdfLinearGradientBrush(new PointF(-2, 0), new PointF(2, 0), Color.Red, Color.Blue); page.Canvas.TranslateTransform(-4f, 2f); path.FillMode = PdfFillMode.Alternate; page.Canvas.DrawPath(pen, brush2, path); PdfRadialGradientBrush brush3 = new PdfRadialGradientBrush(new PointF(0f, 0f), 0f, new PointF(0f, 0f), 1f, Color.Red, Color.Blue); page.Canvas.TranslateTransform(2f, 0f); path.FillMode = PdfFillMode.Winding; page.Canvas.DrawPath(pen, brush3, path); PdfTilingBrush brush4 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f)); brush4.Graphics.DrawRectangle(brush2, 0, 0, 4f, 4f); page.Canvas.TranslateTransform(2f, 0f); path.FillMode = PdfFillMode.Winding; page.Canvas.DrawPath(pen, brush4, path); //restor graphics page.Canvas.Restore(state); } } }
Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports System.Drawing Namespace FivePointedStar Class Program Private Shared Sub DrawStar(page As PdfPageBase) Dim points As PointF() = New PointF(4) {} For i As Integer = 0 To points.Length - 1 Dim x As Single = CSng(Math.Cos(i * 2 * Math.PI / 5)) Dim y As Single = CSng(Math.Sin(i * 2 * Math.PI / 5)) points(i) = New PointF(x, y) Next Dim path As New PdfPath() path.AddLine(points(2), points(0)) path.AddLine(points(0), points(3)) path.AddLine(points(3), points(1)) path.AddLine(points(1), points(4)) path.AddLine(points(4), points(2)) 'save graphics state Dim state As PdfGraphicsState = page.Canvas.Save() Dim pen As New PdfPen(Color.DeepSkyBlue, 0.02F) Dim brush1 As PdfBrush = New PdfSolidBrush(Color.CadetBlue) page.Canvas.ScaleTransform(50F, 50F) page.Canvas.TranslateTransform(5F, 1.2F) page.Canvas.DrawPath(pen, path) page.Canvas.TranslateTransform(2F, 0F) path.FillMode = PdfFillMode.Alternate page.Canvas.DrawPath(pen, brush1, path) page.Canvas.TranslateTransform(2F, 0F) path.FillMode = PdfFillMode.Winding page.Canvas.DrawPath(pen, brush1, path) Dim brush2 As New PdfLinearGradientBrush(New PointF(-2, 0), New PointF(2, 0), Color.Red, Color.Blue) page.Canvas.TranslateTransform(-4F, 2F) path.FillMode = PdfFillMode.Alternate page.Canvas.DrawPath(pen, brush2, path) Dim brush3 As New PdfRadialGradientBrush(New PointF(0F, 0F), 0F, New PointF(0F, 0F), 1F, Color.Red, Color.Blue) page.Canvas.TranslateTransform(2F, 0F) path.FillMode = PdfFillMode.Winding page.Canvas.DrawPath(pen, brush3, path) Dim brush4 As New PdfTilingBrush(New RectangleF(0, 0, 4F, 4F)) brush4.Graphics.DrawRectangle(brush2, 0, 0, 4F, 4F) page.Canvas.TranslateTransform(2F, 0F) path.FillMode = PdfFillMode.Winding page.Canvas.DrawPath(pen, brush4, path) 'restor graphics page.Canvas.Restore(state) End Sub End Class End Namespace
Effective Screenshot:
Draw Spiral in PDF
A spiral is a curve which emanates from a central point, getting progressively farther away as it revolves around the point. Spire.PDF can also help us easily draw Spiral in PDF and we can design at will.
using Spire.Pdf; using Spire.Pdf.Graphics; using System; namespace Spiral { class Program { private static void DrawSpiral(PdfPageBase page) { //save graphics state PdfGraphicsState state = page.Canvas.Save(); //Draw shap - spiro PdfPen pen = PdfPens.DeepSkyBlue; int nPoints = 1000; double r1 = 30; double r2 = 25; double p = 35; double x1 = r1 + r2 - p; double y1 = 0; double x2 = 0; double y2 = 0; page.Canvas.TranslateTransform(100, 100); for (int i = 0; i < nPoints; i++) { double t = i * Math.PI / 90; x2 = (r1 + r2) * Math.Cos(t) - p * Math.Cos((r1 + r2) * t / r2); y2 = (r1 + r2) * Math.Sin(t) - p * Math.Sin((r1 + r2) * t / r2); page.Canvas.DrawLine(pen, (float)x1, (float)y1, (float)x2, (float)y2); x1 = x2; y1 = y2; } //restor graphics page.Canvas.Restore(state); } } }
Imports Spire.Pdf Imports Spire.Pdf.Graphics Namespace Spiral Class Program Private Shared Sub DrawSpiral(page As PdfPageBase) 'save graphics state Dim state As PdfGraphicsState = page.Canvas.Save() 'Draw shap - spiro Dim pen As PdfPen = PdfPens.DeepSkyBlue Dim nPoints As Integer = 1000 Dim r1 As Double = 30 Dim r2 As Double = 25 Dim p As Double = 35 Dim x1 As Double = r1 + r2 - p Dim y1 As Double = 0 Dim x2 As Double = 0 Dim y2 As Double = 0 page.Canvas.TranslateTransform(100, 100) For i As Integer = 0 To nPoints - 1 Dim t As Double = i * Math.PI / 90 x2 = (r1 + r2) * Math.Cos(t) - p * Math.Cos((r1 + r2) * t / r2) y2 = (r1 + r2) * Math.Sin(t) - p * Math.Sin((r1 + r2) * t / r2) page.Canvas.DrawLine(pen, CSng(x1), CSng(y1), CSng(x2), CSng(y2)) x1 = x2 y1 = y2 Next 'restor graphics page.Canvas.Restore(state) End Sub End Class End Namespace
Effective Screenshot:
Spire.PDF owns the ability of drawing text in PDF document. It supports text formatting, multilingual and text extraction. Here we will introduce how to draw text in PDF with different styles. Spire.PDF enables users to draw rotate text in PDF, draw transform text in PDF, draw alignment text in PDF, draw alignment text in rectangle and other style settings such as font, size, color, pen, etc.
Download Spire.PDF (Spire.Office) and install on system. Create project in visual studio, add Spire.PDF dll and system.drawing as reference. The following code can help us create a blank PDF document, PDF page and save the result PDF document. We can also find the solutions here.
static void Main(string[] args) { //Create a pdf document. PdfDocument doc = new PdfDocument(); // Create one page PdfPageBase page = doc.Pages.Add(); RotateText(page); //Save doc file. doc.SaveToFile("DrawText.pdf"); doc.Close(); //Launching the Pdf file. System.Diagnostics.Process.Start("DrawText.pdf"); }
Draw Transform Text in PDF
To draw transform text in PDF, we can set scale transform and skew transform. Furthermore, we can also design font and color. Sample Code:
private static void TransformText(PdfPageBase page) { //save graphics state PdfGraphicsState state = page.Canvas.Save(); //Draw the text - transform PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 18f); PdfSolidBrush brush1 = new PdfSolidBrush(Color.DeepSkyBlue); PdfSolidBrush brush2 = new PdfSolidBrush(Color.CadetBlue); page.Canvas.TranslateTransform(20, 200); page.Canvas.ScaleTransform(1f, 0.6f); page.Canvas.SkewTransform(-10, 0); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush1, 0, 0); page.Canvas.SkewTransform(10, 0); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, 0); page.Canvas.ScaleTransform(1f, -1f); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, -2 * 18); //restor graphics page.Canvas.Restore(state); }
Effective Screenshot:
Draw Alignment Text in PDF
Usually we have 3 types of alignment on PDF text, left alignment, right alignment and center alignment. Sample code:
private static void AlignText(PdfPageBase page) { //Draw the text - alignment PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 20f); PdfSolidBrush brush = new PdfSolidBrush(Color.Blue); PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); page.Canvas.DrawString("Left!", font, brush, 0, 20, leftAlignment); page.Canvas.DrawString("Left!", font, brush, 0, 50, leftAlignment); PdfStringFormat rightAlignment = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); page.Canvas.DrawString("Right!", font, brush, page.Canvas.ClientSize.Width, 30, rightAlignment); page.Canvas.DrawString("Right!", font, brush, page.Canvas.ClientSize.Width, 60, rightAlignment); PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, page.Canvas.ClientSize.Width / 2, 40, centerAlignment); }
Effective Screenshot:
Draw alignment Text in a Rectangle
Draw alignment text in a rectangle means there has rectangles in PDF document and we will draw text in the rectangles and set alignment style. Sample Code:
private static void AlignTextInRectangle(PdfPageBase page) { //Draw the text - align in rectangle PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f); PdfSolidBrush brush = new PdfSolidBrush(Color.Blue); RectangleF rctg1 = new RectangleF(0, 70, page.Canvas.ClientSize.Width / 2, 100); RectangleF rctg2 = new RectangleF(page.Canvas.ClientSize.Width / 2, 70, page.Canvas.ClientSize.Width / 2, 100); page.Canvas.DrawRectangle(new PdfSolidBrush(Color.LightBlue), rctg1); page.Canvas.DrawRectangle(new PdfSolidBrush(Color.LightSkyBlue), rctg2); PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top); page.Canvas.DrawString("Left! Left!", font, brush, rctg1, leftAlignment); page.Canvas.DrawString("Left! Left!", font, brush, rctg2, leftAlignment); PdfStringFormat rightAlignment = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); page.Canvas.DrawString("Right! Right!", font, brush, rctg1, rightAlignment); page.Canvas.DrawString("Right! Right!", font, brush, rctg2, rightAlignment); PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Bottom); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, rctg1, centerAlignment); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, rctg2, centerAlignment); }
Effective Screenshot:
Draw Rotate Text in PDF
To draw rotate text in PDF, we should set the text as center alignment first. In addition, we can also set transform text here. Sample Code:
private static void RotateText(PdfPageBase page) { //save graphics state PdfGraphicsState state = page.Canvas.Save(); //Draw the text - transform PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f); PdfSolidBrush brush = new PdfSolidBrush(Color.Blue); PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); float x = page.Canvas.ClientSize.Width / 2; float y = 380; page.Canvas.TranslateTransform(x, y); for (int i = 0; i < 12; i++) { page.Canvas.RotateTransform(30); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, 20, 0, centerAlignment); } //restor graphics page.Canvas.Restore(state); }
Effective Screenshot:
Compared with text-only documents, documents containing images are undoubtedly more vivid and engaging to readers. When generating or editing a PDF document, you may sometimes need to insert images to improve its appearance and make it more appealing. In this article, you will learn how to insert, replace or delete images in PDF documents in C# and VB.NET using Spire.PDF for .NET.
- Insert an Image into a PDF Document
- Replace an Image with Another Image in a PDF Document
- Delete a Specific Image in a PDF Document
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF 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.PDF
Insert an Image into a PDF Document in C# and VB.NET
The following steps demonstrate how to insert an image into an existing PDF document:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the desired page in the PDF document through PdfDocument.Pages[pageIndex] property.
- Load an image using PdfImage.FromFile() method.
- Specify the width and height of the image area on the page.
- Specify the X and Y coordinates to start drawing the image.
- Draw the image on the page using PdfPageBase.Canvas.DrawImage() method.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; using Spire.Pdf.Graphics; namespace InsertImage { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Input.pdf"); //Get the first page in the PDF document PdfPageBase page = pdf.Pages[0]; //Load an image PdfImage image = PdfImage.FromFile("image.jpg"); //Specify the width and height of the image area on the page float width = image.Width * 0.50f; float height = image.Height * 0.50f; //Specify the X and Y coordinates to start drawing the image float x = 180f; float y = 70f; //Draw the image at a specified location on the page page.Canvas.DrawImage(image, x, y, width, height); //Save the result document pdf.SaveToFile("AddImage.pdf", FileFormat.PDF); } } }
Replace an Image with Another Image in a PDF Document in C# and VB.NET
The following steps demonstrate how to replace an image with another image in a PDF document:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the desired page in the PDF document through PdfDocument.Pages[pageIndex] property.
- Load an image using PdfImage.FromFile() method.
- Initialize an instance of the PdfImageHelper class.
- Get the image information from the page using PdfImageHelper.GetImagesInfo() method.
- Replace a specific image on the page with the loaded image using PdfImageHelper.ReplaceImage() method.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Utilities; namespace ReplaceImage { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument doc = new PdfDocument(); //Load a PDF document doc.LoadFromFile("AddImage.pdf"); //Get the first page PdfPageBase page = doc.Pages[0]; //Load an image PdfImage image = PdfImage.FromFile("image1.jpg"); //Create a PdfImageHelper instance PdfImageHelper imageHelper = new PdfImageHelper(); //Get the image information from the page PdfImageInfo[] imageInfo = imageHelper.GetImagesInfo(page); //Replace the first image on the page with the loaded image imageHelper.ReplaceImage(imageInfo[0], image); //Save the result document doc.SaveToFile("ReplaceImage.pdf", FileFormat.PDF); } } }
Delete a Specific Image in a PDF Document in C# and VB.NET
The following steps demonstrate how to delete an image from a PDF document:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the desired page in the PDF document through PdfDocument.Pages[pageIndex] property.
- Delete a specific image on the page using PdfPageBase.DeleteImage() method.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; namespace DeleteImage { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a PDF document pdf.LoadFromFile("AddImage.pdf"); //Get the first page PdfPageBase page = pdf.Pages[0]; //Delete the first image on the page page.DeleteImage(0); //Save the result document pdf.SaveToFile("DeleteImage.pdf", FileFormat.PDF); } } }
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.
Word documents can be protected in a variety of ways, depending on the security requirements. To prevent unauthorized people from opening a document, you can encrypt it with a password. To allow users to open the document, but not edit or modify its content, you can make the document read-only or mark it as final. To allow users to modify parts of the document, you can lock the entire document but let specified sections available for editing. This article focuses on how to protect or unprotect a Word document in C# and VB.NET using Spire.Doc for .NET.
- Password Protect a Word Document in C#, VB.NET
- Change Permission of a Word Document in C#, VB.NET
- Lock Specified Sections of a Word Document in C#, VB.NET
- Mark a Word Document as Final in C#, VB.NET
- Remove Password from a Word Document in C#, VB.NET
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc 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.Doc
Password Protect a Word Document in C#, VB.NET
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 protect a Word document with a password using Spire.Doc for .NET.
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Encrypt the document with a password using Document.Encrypt() method.
- Save the document to another Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; namespace PasswordProtectWordDocument { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Load a Word file document.LoadFromFile(@"C:\Users\Administrator\Desktop\test.docx"); //Encrypt the document with a password document.Encrypt("open-psd"); //Save the document to another Word file document.SaveToFile("Encryption.docx", FileFormat.Docx); } } }
Change Permission of a Word Document in C#, VB.NET
Documents encrypted with an open password cannot be opened by those who do not know the password. If you’d like to grant people permission to read your document but restrict the types of modifications that someone can make, you can set the document permission. The following are the steps to change permission of a Word document using Spire.Doc for .NET.
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Set the document permission and set the permission password using Document.Protect() method.
- Save the document to another Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; namespace ChangeDocumentPermission { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Load a Word document document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx"); //Set the document permission and set the permission password document.Protect(ProtectionType.AllowOnlyFormFields, "permission-psd"); //Save the document to another Word file document.SaveToFile("Permission.docx"); } } }
Lock Specified Sections of a Word Document in C#, VB.NET
When you protect your document, you can lock parts of it so that they cannot be changed and leave the unlocked parts available for editing. The following are the steps to protect specified sections of a Word document using Spire.Doc for .NET.
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Set the editing restriction as AllowOnlyFormFields.
- Unprotect a specific section by setting Document.Sections[index].ProtectForm to false. The rest sections will continue to be protected.
- Save the document to another Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; namespace ProtectSpecificSection { class Program { static void Main(string[] args) { //Create a Document object Document doc = new Document(); //Load a Word document doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx"); //Set editing restriction as "AllowOnlyFormFields" doc.Protect(ProtectionType.AllowOnlyFormFields, "permissionPsd"); //Unprotect section 2 doc.Sections[1].ProtectForm = false; //Save the document to another Word file doc.SaveToFile("ProtectSection.docx"); } } }
Mark a Word Document as Final in C#, VB.NET
By marking a document as Final, you disable typing, editing, and format changes capabilities and a message will appear to any reader that the document has been finalized. The following are the steps to mark a Word document as final using Spire.Doc for .NET.
- Create a Document object.
- Load a Word file using Document.LoadFromFile() method.
- Get the CustomDocumentProperties object from the document.
- Add a custom property "_MarkAsFinal" to the document.
- Save the document to another Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; namespace MarkAsFinal { class Program { static void Main(string[] args) { //Create a Document object Document doc = new Document(); //Load a Word document doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx"); //Get custom document properties CustomDocumentProperties customProperties = doc.CustomDocumentProperties; //Add "_MarkAsFinal" property to the document customProperties.Add("_MarkAsFinal", true); //Save the document to another Word file doc.SaveToFile("MarkAsFinal.docx"); } } }
Remove Password from a Word Document in C#, VB.NET
You can remove the password from an encrypted document if the encryption is no longer needed. The following are the detailed steps.
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Remove the password using Document.RemoveEncryption() method.
- Save the document to another Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; namespace RemovePassword { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Load an encrypted Word document document.LoadFromFile(@"C:\Users\Administrator\Desktop\Encryption.docx", FileFormat.Docx, "open-psd"); //Remove encryption document.RemoveEncryption(); //Save the document to another Word file document.SaveToFile("Decryption.docx", FileFormat.Docx); } } }
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.
The sample demonstrates how to export doc document to Epub file.
The sample demonstrates how to provide a dropdownlist for valid data
The sample demonstrates how to read data with fomulas
The sample demonstrates how to calculate formulas