.NET (1322)
Children categories
PDF bookmarks are navigational aids that allow users to quickly locate and jump to specific sections or pages in a PDF document. Through a simple click, users can arrive at the target location, which eliminates the need to manually scroll or search for specific content in a lengthy document. In this article, you will learn how to programmatically add, modify and remove bookmarks in PDF using Spire.PDF for .NET.
- Add Bookmarks to a PDF Document
- Edit Bookmarks in a PDF Document
- Delete Bookmarks from 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
Add Bookmarks to a PDF Document in C# and VB.NET
Spire.PDF for .NET provides the PdfDocument.Bookmarks.Add() method to add bookmarks to a PDF document. After a bookmark is created, you can also use the PdfBookmark.Add() method to add sub-bookmarks for it. The following are the detailed steps.
- Create a PdfDocument object.
- Load a sample PDF file using PdfDocument.LoadFromFile() method.
- Loop through all pages in the PDF file to add bookmarks and set their styles.
- Add a parent bookmark to the document using PdfDocument.Bookmarks.Add() method.
- Create a PdfDestination object and set the destination of the parent bookmark using PdfBookmark.Action property.
- Set the text color and style of the parent bookmark.
- Add a sub-bookmark to the parent bookmark using PdfBookmark.Add() method.
- Set the destination, text color, and text style of the sub-bookmark.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Actions;
using Spire.Pdf.Bookmarks;
using Spire.Pdf.General;
namespace AddBookmark
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a sample PDF file
pdf.LoadFromFile("template.pdf");
//Loop through the pages in the PDF file
for (int i = 0; i < pdf.Pages.Count; i++)
{
PdfPageBase page = pdf.Pages[i];
//Add a bookmark
PdfBookmark bookmark = pdf.Bookmarks.Add(string.Format("Bookmark-{0}", i + 1));
//Set the destination page and location of the bookmark
PdfDestination destination = new PdfDestination(page, new PointF(0, 0));
bookmark.Action = new PdfGoToAction(destination);
//Set the text color and style of the bookmark
bookmark.Color = new PdfRGBColor(Color.Black);
bookmark.DisplayStyle = PdfTextStyle.Bold;
//Add a child bookmark
PdfBookmark childBookmark = bookmark.Add(string.Format("Sub-Bookmark-{0}", i + 1));
//Set the destination page and location of the child bookmark
PdfDestination childDestination = new PdfDestination(page, new PointF(0, 100));
childBookmark.Action = new PdfGoToAction(childDestination);
//Set the text color and style of the child bookmark
childBookmark.Color = new PdfRGBColor(Color.Brown);
childBookmark.DisplayStyle = PdfTextStyle.Italic;
}
// Save the result file
pdf.SaveToFile("AddBookmarks.pdf");
}
}
}

Edit Bookmarks in a PDF Document in C# and VB.NET
If you need to update the existing bookmarks, you can use the methods of PdfBookmark class to rename the bookmarks and change their text color, text style. The following are the detailed steps.
- Create a PdfDocument object.
- Load a sample PDF file using PdfDocument.LoadFromFile() method.
- Get a specified bookmark using PdfDocument.Bookmarks[] property.
- Change the title of the bookmark using PdfBookmark.Title property.
- Change the font color of the bookmark using PdfBookmark.Color property.
- Change the text style of the bookmark using PdfBookmark.DisplayStyle property.
- Change the text color and style of the sub-bookmark using the above methods.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.Bookmarks;
using System.Drawing;
namespace ModifyBookmarks
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a sample PDF file
pdf.LoadFromFile("AddBookmarks.Pdf");
//Get the first bookmark
PdfBookmark bookmark = pdf.Bookmarks[0];
//Change the title of the bookmark
bookmark.Title = "Modified BookMark";
//Change text color and style of the bookmark
bookmark.Color = Color.Red;
bookmark.DisplayStyle = PdfTextStyle.Italic;
//Edit sub-bookmarks of the first bookmark
foreach (PdfBookmark childBookmark in bookmark)
{
childBookmark.Color = Color.Blue;
childBookmark.DisplayStyle = PdfTextStyle.Bold;
}
//Save the result file
pdf.SaveToFile("EditBookmarks.Pdf");
}
}
}

Delete Bookmarks from a PDF Document in C# and VB.NET
Spire.PDF for .NET allows to remove a specified bookmark as well as all bookmarks in a PDF file. Furthermore, removing only a specified sub-bookmark can also be achieved. The following are the detailed steps.
- Create a PdfDocument object.
- Load a sample PDF file using PdfDocument.LoadFromFile() method.
- Get the first bookmark using PdfDocument.Bookmarks[] property.
- Remove a specified sub-bookmark of the first bookmark using PdfBookmark.RemoveAt() method.
- Remove a specified bookmark including its sub-bookmarks using PdfDocument.Bookmarks.RemoveAt() method. Or you can remove all bookmarks in the PDF file using PdfDocument.Bookmarks.Clear() method.
- Save the document using PdfDocument.saveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.Bookmarks;
namespace DeleteBookmarks
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a sample PDF file
pdf.LoadFromFile("AddBookmarks.Pdf");
//Get the first bookmark
PdfBookmark bookmark = pdf.Bookmarks[0];
//Delete the first sub-bookmark of the first bookmark
bookmark.RemoveAt(0);
//Delete the second bookmark including its sub-bookmarks
pdf.Bookmarks.RemoveAt(1);
//Delete all bookmarks
//pdf.Bookmarks.Clear();
//Save the result file
pdf.SaveToFile("DeleteBookmarks.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.
In this section, I will introduce a solution to export Excel to datatable through a datagridview via this WPF Excel component Spire.XLS for WPF. Using it, we can quickly export any Excel sheet to datatable. First let us see below picture:

When we export Excel to datatable, first we need to initialize a new instance of the class Spire.Xls.Workbook, here it is named “workbook”, then, after loading our Excel file from system by this method: Spire.Xls.Workbook.LoadFromFile(string fileName, ExcelVersion version); we need to decide which sheet will be exported to datatable. When using Spire.XLS for WPF, we do not need to install MS Excel, so suppose we do not know how many sheets in our Excel file, we can use this class System.Random provided by Microsoft to get the page count and export any sheet to datatable by calling the method: Spire.Xls.Worksheet.ExportDataTable(). After export data to datatable, with the help of the two properties of the class System.Data.Dataview:System.Data.Dataview.AutoGenerateColumns and System.Data.Dataview.ItemsSource. The datatable columns will be automatically created and all the data in datatable will be shown in datagrid.
Here we can download Spire.XLS for WPF and install it on system. After adding Spire.Xls dll on system, we can start our task of excel to datatable by below code:
using System.Data;
using Spire.Xls;
namespace wpfexceltodatatable
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//load an excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"..\excel.xls", ExcelVersion.Version97to2003);
//get the count of excel worksheet
int sheetCount = workbook.Worksheets.Count;
Random r = new Random();
int index = r.Next(0, sheetCount);
//show datatable in datagrid
Worksheet sheet = workbook.Worksheets[index];
DataTable dataTable = sheet.ExportDataTable();
DataView view = new DataView(dataTable);
this.dataGrid1.ItemsSource = view;
this.dataGrid1.AutoGenerateColumns = true;
}
}
}
Imports System.Data
Imports Spire.Xls
Namespace wpfexceltodatatable
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
'load an excel file
Dim workbook As New Workbook()
workbook.LoadFromFile("..\excel.xls", ExcelVersion.Version97to2003)
'get the count of excel worksheet
Dim sheetCount As Integer = workbook.Worksheets.Count
Dim r As New Random()
Dim index As Integer = r.[Next](0, sheetCount)
'show datatable in datagrid
Dim sheet As Worksheet = workbook.Worksheets(index)
Dim dataTable As DataTable = sheet.ExportDataTable()
Dim view As New DataView(dataTable)
Me.dataGrid1.ItemsSource = view
Me.dataGrid1.AutoGenerateColumns = True
End Sub
End Class
End Namespace
A text box is a movable, resizable container for storing text or images. In Word documents, you can insert text boxes as sidebars or to bring focus to some important text, such as headings or quotes. Occasionally, you may also need to delete some mistakenly added text boxes. In this article, you will learn how to programmatically insert or remove a text box in a Word document using Spire.Doc for .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 DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Insert a Text Box in a Word Document
Spire.Doc for .NET provides the Paragraph.AppendTextBox(float width, float height) method to insert a text box in a specified paragraph. After the text box is inserted, Spire.Doc for .NET also provides the TextBox class for users to format the text box by setting its properties, such as Format, Body etc. The detailed steps are as follows.
- Create a Document instance, and then load a sample Word document using Document.LoadFromFile() method.
- Get the first section using Document.Sections[] property, and then add a paragraph to the section using Section.AddParagraph() method.
- Add a text box to the paragraph using Paragraph.AppendTextBox(float width, float height) method.
- Get the format of the text box using TextBox.Format property, and then set the text box's wrapping type, position, border color and fill color using the properties of TextBoxFormat Class.
- Add a paragraph to the text box using TextBox.Body.AddParagraph() method, and then set its alignment.
- Insert an image to the paragraph using Paragraph.AppendPicture() method, and then set the size of the inserted image.
- Insert text to the text box using Paragraph.AppendText() method, and then set the text font.
- Save the document to another file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertTextbox
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a sample Word document
document.LoadFromFile("Ralph.docx");
//Insert a textbox and set its wrapping style
TextBox TB = document.Sections[0].AddParagraph().AppendTextBox(130, 320);
TB.Format.TextWrappingStyle = TextWrappingStyle.Square;
//Set the position of the textbox
TB.Format.HorizontalOrigin = HorizontalOrigin.RightMarginArea;
TB.Format.HorizontalPosition = -100;
TB.Format.VerticalOrigin = VerticalOrigin.Page;
TB.Format.VerticalPosition = 130f;
//Set the border style and fill color of the textbox
TB.Format.LineColor = Color.DarkBlue;
TB.Format.FillColor = Color.LightCyan;
//Insert an image to textbox as a paragraph
Paragraph para = TB.Body.AddParagraph();
DocPicture picture = para.AppendPicture(@"C:\Users\Administrator\Desktop\Ralph.jpg");
//Set alignment for the paragraph
para.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Set the size of the inserted image
picture.Height = 90;
picture.Width = 90;
//Insert text to textbox as the second paragraph
TextRange TR = para.AppendText("Emerson is truly the center of the American transcendental movement, "
+ "setting out most of its ideas and values in a little book, Nature, published in 1836, "
+ "that represented at least ten years of intense study in philosophy, religion, and literature.");
//Set alignment for the paragraph
para.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Set the font of the text
TR.CharacterFormat.FontName = "Times New Roman";
TR.CharacterFormat.FontSize = 12;
TR.CharacterFormat.Italic = true;
//Save the result file
document.SaveToFile("AddTextBox.docx", FileFormat.Docx);
}
}
}

Remove a Text Box from a Word Document
Spire.Doc for .NET provides the Document.TextBoxes.RemoveAt(int index) method to delete a specified text box. If you want to delete all text boxes from a Word document, you can use the Document.TextBoxes.Clear() method. The below example shows how to remove the first text box from a Word document.
- Create a Document instance.
- Load a sample Word document using Document.LoadFromFile() method.
- Remove the first text box using Document.TextBoxes.RemoveAt(int index) method.
- Save the document to another file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
namespace Removetextbox
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document Doc = new Document();
//Load a sample Word document
Doc.LoadFromFile("TextBox.docx");
//Remove the first text box
Doc.TextBoxes.RemoveAt(0);
//Remove all text boxes
//Doc.TextBoxes.Clear();
//Save the result document
Doc.SaveToFile("RemoveTextbox.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.
A PDF document can contain multiple pages with different text content, images, or other objects. Occasionally, users may need to delete certain pages with incorrectly drawn objects or pages that are irrelevant to the topic of the document. This article will demonstrate how to programmatically delete/remove pages from an existing PDF document using Spire.PDF for .NET.
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 DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Delete/Remove Pages from PDF
- Create a PdfDocument object.
- Load a sample PDF document using PdfDocument.LoadFromFile() method.
- Get the pages in the PDF document using PdfDocument.Pages property.
- Delete a specified page by index using PdfPageCollection.RemoveAt(int index)method.
- Save the document to another file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
namespace RemovePage
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument document = new PdfDocument();
//Load a sample PDF document
document.LoadFromFile(@"E:\Files\input.pdf");
//Remove the second page
document.Pages.RemoveAt(1);
//Save the result document
document.SaveToFile("RemovePDFPage.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.
PDF booklet is pretty helpful when people print a huge PDF document. It enjoys special popularity among book, newspaper and magazine editors. This section will introduce a very simple way to create PDF booklet via a .NET PDF component in C#, VB.NET.
Spire.PDF for .NET is a .NET PDF library which can manipulate PDF documents without Adobe Acrobat or any third party library. Using this PDF component, you can quickly create PDF booklet in your .NET applications. After setting the PDF page width and height by a class Spire.Pdf.PdfPageSize, you can create your PDF booklet through implementing PdfDocument.CreateBooklet(string fileName, float width, float height, bool doubleSide) directly. Below picture shows the effect of this task:

Here you can quickly download Spire.PDF for .NET and install it on your system. After adding Spire.Pdf reference, please see the detail code of PDF booklet below.
Draw PDF Barcode in C#, VB.NET
using System.Drawing;
using Spire.Pdf;
namespace PDF_Booklet
{
class Program
{
static void Main(string[] args)
{
//Load a PDF file
PdfDocument doc = new PdfDocument();
String srcPdf = @"..\read PDF.pdf";
//Create PDF booklet
float width = PdfPageSize.A4.Width * 2;
float height = PdfPageSize.A4.Height;
doc.CreateBooklet(srcPdf, width, height, true);
//Save pdf file.
doc.SaveToFile("Booklet.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("Booklet.pdf");
}
}
}
Imports System.Drawing
Imports Spire.Pdf
Namespace PDF_Booklet
Class Program
Private Shared Sub Main(args As String())
'Load a PDF file
Dim doc As New PdfDocument()
Dim srcPdf As [String] = "..\read PDF.pdf"
'Create PDF booklet
Dim width As Single = PdfPageSize.A4.Width * 2
Dim height As Single = PdfPageSize.A4.Height
doc.CreateBooklet(srcPdf, width, height, True)
'Save pdf file.
doc.SaveToFile("Booklet.pdf")
doc.Close()
'Launching the Pdf file.
System.Diagnostics.Process.Start("Booklet.pdf")
End Sub
End Class
End Namespace
Spire.PDF for .NET is a PDF component that enables you to create, read, edit and manipulate PDF files in C#, VB.NET
Table Layout decides how the table displays in PDF page. People always set PDF table layout in order to let the table perfectly fit PDF page according to their own like. In this section, I will introduce a solution to set table layout in PDF via this .NET PDF component Spire.PDF for .NET with C#, VB.NET. First let us view the target PDF file as below picture:

In Spire.PDF for .NET, there is a class called: Spire.Pdf.Tables.PdfTableLayoutFormat. By using this class, we can set table layout type and break type. Here Spire.PDF provided two layout types: Paginate and OnePage and two break type: FitElement and Fit Page. When you set the break type to be FitElement, PDF table will display according to the table length, while for FitPage choice, the table will automatically fit the PDF to every page ignoring table length.
Here let us see this method: PdfLayoutResult.Draw(PdfPageBase page, PointF location, PdfTextLayout format).There are three parameters passed. The first parameter determines the page size and margin. By setting the second parameter, we can set the horizontal and vertical distance between the table and PDF margin. While the last parameter is the table layout we just set. Obviously, we can set table layout by calling this method directly.
Now, you can download Spire.PDF for .NET and start table layout task by below key code:
table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout);
PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
tableLayout.Break = PdfLayoutBreakType.FitElement;
tableLayout.Layout = PdfLayoutType.Paginate;
PdfLayoutResult result = table.Draw(page, new PointF(0, y), tableLayout);
y = result.Bounds.Bottom + 5;
table.BeginRowLayout += New BeginRowLayoutEventHandler(table_BeginRowLayout) Dim tableLayout As New PdfTableLayoutFormat() tableLayout.Break = PdfLayoutBreakType.FitElement tableLayout.Layout = PdfLayoutType.Paginate Dim result As PdfLayoutResult = table.Draw(page, New PointF(0, y), tableLayout) y = result.Bounds.Bottom + 5
Spire.PDF for .NET is a PDF api that enables users to create, edit, read and handle PDF files in .NET applications.
A simple PDF List is always one level list which displays briefly the content items of a PDF file. When the PDF is very huge and complex, people need more details, creating multiple level list in PDF becomes pretty necessary. Here a solution will be introduced to you about how to create multiple level list in PDF via a .NET PDF component Spire.PDF for .NET in C#, VB.NET. First let us see the effect of PDF multiple level list as below picture:

Here we can download Spire.PDF for .NET. After adding Spire.Pdf dll in project, please view below key code to start PDF list task.
In this solution, apart from creating PDF list, we also can set list format, position and layout. This class Graphics.PdfTrueTypeFont which is provided by Spire.PDF enables to set list font and size. From above picture, we can view that the first level list is red. It can be set in this class Spire.Pdf.Graphics.PdfLinearGradientBrush. In order to let users themselves to set PDF list style according to their needs, Spire.Pdf.Lists.PdfOrderedMarker can help us to set label style by this enum Spire.Pdf.PdfNumberStyle. Here there are six styles available: LowerLatin, LowerRoman, None, Numeric, UpperLatin and UpperRoman. In below code, I only show a part of code:
//Set PDF list label style
PdfOrderedMarker marker1
= new PdfOrderedMarker(PdfNumberStyle.LowerRoman, new PdfFont(PdfFontFamily.Helvetica, 12f));
PdfOrderedMarker marker2
= new PdfOrderedMarker(PdfNumberStyle.Numeric, new PdfFont(PdfFontFamily.Helvetica, 10f));
'Set PDF list label style Dim marker1 As New PdfOrderedMarker(PdfNumberStyle.LowerRoman, New PdfFont(PdfFontFamily.Helvetica, 12F)) Dim marker2 As New PdfOrderedMarker(PdfNumberStyle.Numeric, New PdfFont(PdfFontFamily.Helvetica, 10F))
After setting the PDF list format, we can search data in database by System.Data.Oledb.OleDbCommand and then, create multi-level list by two classes in Spire.Pdf: Spire.Pdf.Lists. PdfListItem and Spire.Pdf.Lists. PdfSortedList. The former class represents list item while the later represents the ordered list.
//Create multi-level list in PDF
PdfSortedList vendorList = new PdfSortedList(font2);
vendorList.Indent = 0;
vendorList.TextIndent = 5;
vendorList.Brush = brush2;
vendorList.Marker = marker1;
using (OleDbConnection conn = new OleDbConnection())
{
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=demo.mdb";
OleDbCommand command = new OleDbCommand();
command.CommandText
= " select VendorNo, VendorName from vendors ";
command.Connection = conn;
OleDbCommand command2 = new OleDbCommand();
command2.CommandText
= " select Description from parts where VendorNo = @VendorNo";
command2.Connection = conn;
OleDbParameter param = new OleDbParameter("@VendorNo", OleDbType.Double);
command2.Parameters.Add(param);
conn.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
double id = reader.GetDouble(0);
PdfListItem item = vendorList.Items.Add(reader.GetString(1));
PdfSortedList subList = new PdfSortedList(font3);
subList.Marker = marker2;
subList.Brush = brush3;
item.SubList = subList;
subList.TextIndent = 20;
command2.Parameters[0].Value = id;
using (OleDbDataReader reader2 = command2.ExecuteReader())
{
while (reader2.Read())
{
subList.Items.Add(reader2.GetString(0));
}
}
String maxNumberLabel = Convert.ToString(subList.Items.Count);
subList.Indent = 30 - font3.MeasureString(maxNumberLabel).Width;
}
}
'Create multi-level list in PDF
Dim vendorList As New PdfSortedList(font2)
vendorList.Indent = 0
vendorList.TextIndent = 5
vendorList.Brush = brush2
vendorList.Marker = marker1
Using conn As New OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=demo.mdb"
Dim command As New OleDbCommand()
command.CommandText = " select VendorNo, VendorName from vendors "
command.Connection = conn
Dim command2 As New OleDbCommand()
command2.CommandText = " select Description from parts where VendorNo = @VendorNo"
command2.Connection = conn
Dim param As New OleDbParameter("@VendorNo", OleDbType.[Double])
command2.Parameters.Add(param)
conn.Open()
Dim reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
Dim id As Double = reader.GetDouble(0)
Dim item As PdfListItem = vendorList.Items.Add(reader.GetString(1))
Dim subList As New PdfSortedList(font3)
subList.Marker = marker2
subList.Brush = brush3
item.SubList = subList
subList.TextIndent = 20
command2.Parameters(0).Value = id
Using reader2 As OleDbDataReader = command2.ExecuteReader()
While reader2.Read()
subList.Items.Add(reader2.GetString(0))
End While
End Using
Dim maxNumberLabel As [String] = Convert.ToString(subList.Items.Count)
subList.Indent = 30 - font3.MeasureString(maxNumberLabel).Width
End While
End Using
Finally, if we want to make sure that PDF multi-level list fits the page perfectly, we need to set the PDF text layout. When doing this you can call this method: Spire.Pdf.Graphics.PdfLayoutResult.Draw(PdfPageBase page, PointF location, PdfTextLayout format); there are three parameters passed, one is to draw text in PDF page, another is to set text location, and the last one is to set text layout.
//Set PDF Layout and position
PdfTextLayout textLayout = new PdfTextLayout();
textLayout.Break = PdfLayoutBreakType.FitPage;
textLayout.Layout = PdfLayoutType.Paginate;
vendorList.Draw(page, new PointF(0, y), textLayout);
'Set PDF Layout and position Dim textLayout As New PdfTextLayout() textLayout.Break = PdfLayoutBreakType.FitPage textLayout.Layout = PdfLayoutType.Paginate vendorList.Draw(page, New PointF(0, y), textLayout)
Spire.PDF for .NET is a .NET PDF component that enables you to create, read, edit and handle PDF files in C#, VB.NET.
Users can change Word view mode according to own reading habit. This guide introduces a solution to set Word view modes in C# and VB.NET.
There are several Word View Modes provided with customers, including Print Layout, Web Layout, Full Screen, Draft, Outline and Zoom in/out with specified percentage. The view mode can be selected when opening to make the document to be presented to match readers’ reading habit. This guide focuses on demonstrating how to set Word view mode in C# and VB.NET via Spire.Doc for .NET. The screenshot presents the result after setting Word view mode.

Spire.Doc for .NET, specializing in operating Word in .NET, offers a ViewSetup class to enable users to set Word view modes through assigning specified values for its properties. In this example, the Word view modes will be set as Web Layout with zoom out 150 percent. Therefore, the set properties of ViewSetup class include DocumentViewType, ZoomPercent and ZoomType.
DocumentViewTyp: There are five types provided by Spire.Doc for .NET: None, NormalLayout, OutlineLayout, PrintLayout and WebLayout.
ZoomPercent: The default ZoomPercent is 100. User can set other percent according to requirements. In this example, ZoomPercent is set as 150.
ZoomType: There are four zoom types provided by Spire.Doc for .NET: Full Page to automatically recalculate zoom percentage to fit one full page; None to use the explicit zoom percentage; PageWidth to automatically recalculate zoom percentage to fit page width; TextFit to automatically recalculate zoom percentage to fit text. Because the zoom percentage is set as 150, so the ZoomType is set as None in this example.
Download and install Spire.Doc for .NET and follow the code:
using Spire.Doc;
namespace WordViewMode
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile(@"E:\Work\Documents\WordDocuments\.NET Framework.docx");
doc.ViewSetup.DocumentViewType = DocumentViewType.WebLayout;
doc.ViewSetup.ZoomPercent = 150;
doc.ViewSetup.ZoomType = ZoomType.None;
doc.SaveToFile("WordViewMode.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("WordViewMode.docx");
}
}
}
Imports Spire.Doc
Namespace WordViewMode
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim doc As New Document()
doc.LoadFromFile("E:\Work\Documents\WordDocuments\.NET Framework.docx")
doc.ViewSetup.DocumentViewType = DocumentViewType.WebLayout
doc.ViewSetup.ZoomPercent = 150
doc.ViewSetup.ZoomType = ZoomType.None
doc.SaveToFile("WordViewMode.docx", FileFormat.Docx2010)
System.Diagnostics.Process.Start("WordViewMode.docx")
End Sub
End Class
End Namespace
Spire.Doc, professional Word component, is specially designed for developers to fast generate, write, modify and save Word documents in .NET, Silverlight and WPF with C# and VB.NET. Also, it supports conversion between Word and other popular formats, such as PDF, HTML, Image, Text and so on, in .NET and WPF platform.
Page margins are the distance from the page edges to the content area. They are helpful in making a document look neat and professional. When generating a document in Microsoft Word, you may need to set page margins to make the document look better. In this article, we will demonstrate how to set page margins for Word documents in C# and VB.NET using Spire.Doc for .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
Set Page Margins in Word in C# and VB.NET
The following are the steps to set page margins in Word:
- Initialize an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get the desired section through Document.Sections[sectionIndex] property.
- Set the top, bottom, left and right margins for the pages in the section through Section.PageSetup.Margins.Top, Section.PageSetup.Margins.Bottom, Section.PageSetup.Margins.Left, Section.PageSetup.Margins.Right properties.
- Save the result document using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
namespace PageMargins
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a Word document
document.LoadFromFile(@"Sample.docx");
//Get the first section
Section section = document.Sections[0];
//Set top, bottom, left and right page margins for the section
section.PageSetup.Margins.Top = 17.9f;
section.PageSetup.Margins.Bottom = 17.9f;
section.PageSetup.Margins.Left = 17.9f;
section.PageSetup.Margins.Right = 17.9f;
//Save the result document
document.SaveToFile("SetMargins.docx", FileFormat.Docx2013);
}
}
}

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.
Modifying passwords of PDF file is really a rational choice especially when the passwords are known by someone and your PDF file is no longer safe. Spire.PDF for .NET enables you to modify passwords of your encrypted PDF file in C#, VB.NET. You can modify your owner password as well as user password and set user restrictions when access the PDF file. Now please see the process of modifying encrypted PDF passwords as below picture:

From above picture, you can easily find that the first step is to decrypt PDF file by owner password. So the original owner password is necessary. You can decrypt it by this method: Spire.Pdf.PdfDocument(string filename, string password)
Then, modify passwords by resetting owner password and user password. PDFSecurity class which is in the namespace Spire.PDFDocument.Security can help you not only to set owner password and user password, but also can set user permissions to restrict user access.
Below shows the whole code of modifying passwords of encrypted PDF file, please download Spire.PDF for .NET and install it on system before following the code:
using Spire.Pdf;
using Spire.Pdf.Security;
namespace modify_PDF_passwords
{
class Program
{
static void Main(string[] args)
{
//load a encrypted file and decrypt it
String encryptedPdf = @""..\Encrypt.pdf"";
PdfDocument doc = new PdfDocument(encryptedPdf, ""e-iceblue"");
// Define user and owner passwords
string userPassword = ""user"";
string ownerPassword = ""owner"";
// Create a security policy with the specified passwords
PdfSecurityPolicy securityPolicy = new PdfPasswordSecurityPolicy(userPassword, ownerPassword);
// Set the encryption algorithm to AES 128-bit
securityPolicy.EncryptionAlgorithm = PdfEncryptionAlgorithm.AES_128;
// Allow printing of the document
securityPolicy.DocumentPrivilege.AllowPrint = true;
// Allow filling form fields in the document
securityPolicy.DocumentPrivilege.AllowFillFormFields = true;
// Allow copying content from the document
securityPolicy.DocumentPrivilege.AllowContentCopying = true;
// Encrypt the PDF document using the specified security policy
doc.Encrypt(securityPolicy);
// Save the encrypted PDF document to a file named ""SecurityPermission.pdf""
doc.SaveToFile(""Encryption-result.pdf"");
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Security
Namespace modify_PDF_passwords
Class Program
Private Shared Sub Main(args As String())
load a encrypted file and decrypt it
Dim encryptedPdf As String = ""..\Encrypt.pdf""
Dim doc As New PdfDocument(encryptedPdf, ""e-iceblue"")
' Define user and owner passwords
Dim userPassword As String = ""user""
Dim ownerPassword As String = ""owner""
' Create a security policy with the specified passwords
Dim securityPolicy As PdfSecurityPolicy = New PdfPasswordSecurityPolicy(userPassword, ownerPassword)
' Set the encryption algorithm to AES 128-bit
securityPolicy.EncryptionAlgorithm = PdfEncryptionAlgorithm.AES_128
' Allow printing of the document
securityPolicy.DocumentPrivilege.AllowPrint = True
' Allow filling form fields in the document
securityPolicy.DocumentPrivilege.AllowFillFormFields = True
' Allow copying content from the document
securityPolicy.DocumentPrivilege.AllowContentCopying = True
' Encrypt the PDF document using the specified security policy
doc.Encrypt(securityPolicy)
' Save the encrypted PDF document to a file named ""SecurityPermission.pdf""
doc.SaveToFile(""Encryption-result.pdf"")
End Sub
End Class
End Namespace
Spire.PDF for .NET is a .NET PDF component that enables you to generate, read, edit and manipulate PDF files in C#, VB.NET.