When we want to manage the pagination for the paragraphs, we can insert a page break directly. But later we may find that it is hard to add or remove text above the page break and then we have to remove the whole page break. With Microsoft word, we can also use the Paragraph dialog to manage the pagination flexible for the word paragraph as below:

How to manage pagination on word document in C#

We have already shown you how to insert page break to the word document, this article we will show you how to manage the pagination by using the Paragraph.Format offered by Spire.Doc. Here comes to the steps of how to manage the pagination for the word document paragraph in C#:

Step 1: Create a new word document and load the document from file.

Document doc = new Document();
doc.LoadFromFile("sample.docx");

Step 2: Get the first section and the paragraph we want to manage the pagination

Section sec = doc.Sections[0];
Paragraph para = sec.Paragraphs[4];

Step 3: Set the pagination format as Format.PageBreakBefore for the checked paragraph.

para.Format.PageBreakBefore = true; 

Step 4: Save the document to file.

doc.SaveToFile("result.docx",FileFormat.Docx2010);

Effective screenshot after managing the pagination for the word document:

How to manage pagination on word document in C#

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
namespace ManagePage
{
 class Program
    {

     static void Main(string[] args)
     {
         Document doc = new Document();
         doc.LoadFromFile("sample.docx");

         Section sec = doc.Sections[0];
         Paragraph para = sec.Paragraphs[4];
         para.Format.PageBreakBefore = true;

         doc.SaveToFile("result.docx", FileFormat.Docx2010);

     }

    }
}

A table in Microsoft Word can contain a variety of elements, including text, images, and many more. Sometimes, you may want to insert images into a table to present some information or extract images from a table for use in other documents. This article will teach you how to insert or extract images from tables in 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

Insert Images into a Table in a Word Document in C# and VB.NET

Spire.Doc provides the TableCell.Paragraphs[int].AppendPicture() method which enables you to add an image to a specific table cell. The detailed steps are as follows:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section in the document by its index through Document.Sections[int] property.
  • Get a specific table in the section by its index through Section.Tables[int] property.
  • Access a specific cell to which you want to add an image through Table.Row[int].Cells[int] property.
  • Add an image to a specific paragraph of the cell using TableCell.Paragraphs[int].AppendPicture() method.
  • Set image width and height through DocPicture.Width and DocPicture.Height properties.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertImagesIntoTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document doc = new Document();
            //Load a Word document
            doc.LoadFromFile("Table.docx");

            //Get the first section of the document
            Section section = doc.Sections[0];

            //Get the first table from the first section
            Table table = (Table)section.Tables[0];

            //Add an image to the 3rd cell of the second row in the table
            TableCell cell = table.Rows[1].Cells[2];
            DocPicture picture = cell.Paragraphs[0].AppendPicture(Image.FromFile("doc.png"));
            //Set image width and height
            picture.Width = 100;
            picture.Height = 100;
            //Add an image to the 3rd cell of the 3rd row in the table
            cell = table.Rows[2].Cells[2];
            picture = cell.Paragraphs[0].AppendPicture(Image.FromFile("xls.png"));
            //Set image width and height
            picture.Width = 100;
            picture.Height = 100;

            //Save the result document
            doc.SaveToFile("AddImageToTable.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Insert or Extract Images from Tables in Word

Extract Images from a Table in a Word Document in C# and VB.NET

To extract images from a table, you need to iterate through all rows in the tale, all cells in each row, all paragraphs in each cell and all child objects in each paragraph, then find the objects that are of DocPicture type and call DocPicture.Image.Save() method to save them to a specified file path. The detailed steps are as follows:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section in the document by its index through Document.Sections[int] property.
  • Get a specific table in the section by its index through Section.Tables[int] property.
  • Iterate through all rows in the table.
  • Iterate through all cells in each row.
  • Iterate through all paragraphs in each cell.
  • Iterate through all child objects in each paragraph.
  • Check if the current child object is of DocPicture type.
  • If the result is true, typecast the object as DocPicture and call DocPicture.Image.Save() method to save the image to a specified file path.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Drawing.Imaging;

namespace ExtractImagesFromTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document doc = new Document();
            //Load a Word document
            doc.LoadFromFile("AddImageToTable.docx");

            //Get the first section of the document
            Section section = doc.Sections[0];

            //Get the first table from the first section
            Table table = (Table)section.Tables[0];

            int index = 0;
            string imageName = null;

            //Iterate through all rows in the table
            for (int i = 0; i < table.Rows.Count; i++)
            {
                //Iterate through all cells in each row
                for (int j = 0; j < table.Rows[i].Cells.Count; j++)
                {
                    //Iterate through all paragraphs in each cell
                    foreach (Paragraph para in table[i, j].Paragraphs)
                    {
                        //Iterate through all child objects in each paragraph
                        foreach (DocumentObject obj in para.ChildObjects)
                        {
                            //Check if the current child object is of DocPicture type
                            if (obj is DocPicture)
                            {
                                //Save the images to a specified file path
                                imageName = string.Format(@"images\TableImage-{0}.png", index);
                                (obj as DocPicture).Image.Save(imageName, ImageFormat.Png);
                                index++;
                            }
                        }
                    }
                }
            }
        }
    }
}

C#/VB.NET: Insert or Extract Images from Tables in 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 transitions give visual sparkle by creating a "PowerPoint-like" effect when switching between pages. Page transitions are useful when you create a slideshow in PDF format. This article will introduce how to add page transition effects to an existing PDF document using Spire.PDF.

Code Snippet:

Step 1: Initialize an object of PdfDocument class and load the sample PDF which you want to add transition effects to.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

Step 2: Create a new PDF document, add sections to it based on the page count of source file and transfer every individual page to a section. Spire.PDF provides PageSettings property in PdfSection class for setting transition effects, that's reason why we should add pages to sections within a new PDF document.

PdfDocument newDoc = new PdfDocument();
for (int i = 0; i < doc.Pages.Count; i++)
{
    PdfSection secetion = newDoc.Sections.Add();
    PdfPageBase page = doc.Pages[i];
    PdfTemplate template = page.CreateTemplate();
    PdfNewPage newpage = secetion.Pages.Add();
    newpage.Canvas.DrawTemplate(template, new PointF(0 - doc.PageSettings.Margins.Left, 0- doc.PageSettings.Margins.Top));
}

Step 3: As demonstrated above, we know that each section in the new document contains a page. Then we could set transition effect for each page through PageSettings.Transition.

PdfSection secetion1 = newDoc.Sections[0];
secetion1.PageSettings.Transition.Style = PdfTransitionStyle.Cover;
secetion1.PageSettings.Transition.Duration = 3;
secetion1.PageSettings.Transition.PageDuration = 2;

PdfSection secetion2 = newDoc.Sections[1];
secetion2.PageSettings.Transition.Style = PdfTransitionStyle.Glitter;
secetion2.PageSettings.Transition.Duration = 3;
secetion2.PageSettings.Transition.PageDuration = 2;

PdfSection secetion3 = newDoc.Sections[2];
secetion3.PageSettings.Transition.Style = PdfTransitionStyle.Fade;
secetion3.PageSettings.Transition.Duration = 3;
secetion3.PageSettings.Transition.PageDuration = 2;

Step 4: Save and launch the file.

newDoc.SaveToFile("Transition.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("Transition.pdf");

Output:

How to set page transitions for existing PDF document in C#, VB.NET

[C#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace SetPageTransitions
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            PdfDocument newDoc = new PdfDocument();
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                PdfSection secetion = newDoc.Sections.Add();
                PdfPageBase page = doc.Pages[i];
                PdfTemplate template = page.CreateTemplate();
                PdfNewPage newpage = secetion.Pages.Add();
                newpage.Canvas.DrawTemplate(template, new PointF(0 - doc.PageSettings.Margins.Left, 0 - doc.PageSettings.Margins.Top));
            }

            PdfSection secetion1 = newDoc.Sections[0];
            secetion1.PageSettings.Transition.Style = PdfTransitionStyle.Cover;
            secetion1.PageSettings.Transition.Duration = 3;
            secetion1.PageSettings.Transition.PageDuration = 2;

            PdfSection secetion2 = newDoc.Sections[1];
            secetion2.PageSettings.Transition.Style = PdfTransitionStyle.Glitter;
            secetion2.PageSettings.Transition.Duration = 3;
            secetion2.PageSettings.Transition.PageDuration = 2;

            PdfSection secetion3 = newDoc.Sections[2];
            secetion3.PageSettings.Transition.Style = PdfTransitionStyle.Fade;
            secetion3.PageSettings.Transition.Duration = 3;
            secetion3.PageSettings.Transition.PageDuration = 2;

            newDoc.SaveToFile("Transition.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("Transition.pdf");
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing


Namespace SetPageTransitions
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument()
			doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")

			Dim newDoc As New PdfDocument()
			For i As Integer = 0 To doc.Pages.Count - 1
				Dim secetion As PdfSection = newDoc.Sections.Add()
				Dim page As PdfPageBase = doc.Pages(i)
				Dim template As PdfTemplate = page.CreateTemplate()
				Dim newpage As PdfNewPage = secetion.Pages.Add()
				newpage.Canvas.DrawTemplate(template, New PointF(0 - doc.PageSettings.Margins.Left, 0 - doc.PageSettings.Margins.Top))
			Next

			Dim secetion1 As PdfSection = newDoc.Sections(0)
			secetion1.PageSettings.Transition.Style = PdfTransitionStyle.Cover
			secetion1.PageSettings.Transition.Duration = 3
			secetion1.PageSettings.Transition.PageDuration = 2

			Dim secetion2 As PdfSection = newDoc.Sections(1)
			secetion2.PageSettings.Transition.Style = PdfTransitionStyle.Glitter
			secetion2.PageSettings.Transition.Duration = 3
			secetion2.PageSettings.Transition.PageDuration = 2

			Dim secetion3 As PdfSection = newDoc.Sections(2)
			secetion3.PageSettings.Transition.Style = PdfTransitionStyle.Fade
			secetion3.PageSettings.Transition.Duration = 3
			secetion3.PageSettings.Transition.PageDuration = 2

			newDoc.SaveToFile("Transition.pdf", FileFormat.PDF)
			System.Diagnostics.Process.Start("Transition.pdf")
		End Sub
	End Class
End Namespace

How to hide word paragraph in C#

2016-06-08 05:46:24 Written by Koohji

When we operate the word document, we can hide some texts or the whole paragraph on the Microsoft Word after click the font box to hide the selected texts. Please check the screenshot of how Microsoft hide the text as below:

How to hide word paragraph in C#

Spire.Doc offers a CharacterFormat.Hidden property to enable developers to hide the specified text or the whole paragraph. This article will demonstrate how to hide the whole paragraph on the word document in C#.

Here comes to the code snippets:

Step 1: Create a new word document and load from file.

Document doc = new Document();
doc.LoadFromFile("sample.docx");

Step 2: Get the first section and the first paragraph from the word document.

Section sec = doc.Sections[0];
Paragraph para = sec.Paragraphs[0];

Step 3: Get the first TextRange and set CharacterFormat.Hidden property as true to hide the texts.

(para.ChildObjects[0] as TextRange).CharacterFormat.Hidden = true;

Step 4: Save the document to file.

doc.SaveToFile("result.docx", FileFormat.Docx);

Effective screenshot after the paragraph is hidden:

How to hide word paragraph in C#

After we set the options to display the hidden text, the hidden text will show like this:

How to hide word paragraph in C#

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace HideParagh
{
 class Program
    {
     
      static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("sample.docx");
            Section sec = doc.Sections[0];
            Paragraph para = sec.Paragraphs[0];

            (para.ChildObjects[0] as TextRange).CharacterFormat.Hidden = true;

            doc.SaveToFile("result.docx", FileFormat.Docx);
        }
    }
}

When demonstrating an excel report, you may also want to share information from other external files or websites. In Excel, we can add both URL hyperlinks and external files by right-clicking on cells, selecting hyperlink and then adding URL address or choosing files from disk. This article is aimed to explain how to add hyperlinks to external files in excel programmatically using Spire.XLS for WPF. To add URL hyperlinks, please refer to this article: How to Insert Hyperlink in Excel for WPF Applications.

Please see the effective screenshot below after adding hyperlinks to external files:

How to add hyperlinks to external files in Excel for WPF Applications

Code Snippets:

Use the following namespace:

using System.Windows;
using Spire.Xls;

Step 1: Initialize a new Workbook object, load the sample excel file and get its first worksheet.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet sheet = workbook.Worksheets[0];

Step 2: Get the cell/cell range that you want to add hyperlink to, then call the sheet.HyperLinks.Add(CellRange range) method to add the hyperlink to the cell/cell range.

CellRange range1 = sheet.Range["D18"];
HyperLink hyperlink1 = sheet.HyperLinks.Add(range1); 

Step 3: Specify the hyperlink style and the hyperlink target, here we set its style to file and target to an external excel file.

hyperlink1.Type = HyperLinkType.File;
hyperlink1.Address = "SalesInfo.xlsx";

Step 4: Repeat step 2 and step 3 to add a hyperlink to another specific cell, set the hyperlink style to file and set its target to a word file.

CellRange range2 = sheet.Range["E18"];
HyperLink hyperlink2 = sheet.HyperLinks.Add(range2);
hyperlink2.Type = HyperLinkType.File;
hyperlink2.Address = "Report.doc";

Step 5: Save and launch the file.

workbook.SaveToFile("LinktoFile.xlsx", FileFormat.Version2010);
System.Diagnostics.Process.Start("LinktoFile.xlsx");

Full Codes:

C#
using Spire.Xls;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Vendors Information.xlsx");
            Worksheet sheet = workbook.Worksheets[0];

            HyperLink Link = sheet.HyperLinks.Add(sheet.Range["A5"]);
            Link.TextToDisplay = sheet.Range["A5"].Text;
            Link.Type = HyperLinkType.Url;
            Link.Address = "https://en.wikipedia.org/wiki/Canada";

            HyperLink NewLink = sheet.HyperLinks.Add(sheet.Range["D13"]);
            NewLink.TextToDisplay = "https://www.google.com";
            NewLink.Type = HyperLinkType.Url;
            NewLink.Address = "https://www.google.com";

            workbook.SaveToFile("Hyperlink.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("Hyperlink.xlsx");
        }
    }
}
VB.NET
Imports Spire.Xls
Imports System.Windows

Namespace WpfApplication1
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub
		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			Dim workbook As New Workbook()
			workbook.LoadFromFile("Vendors Information.xlsx")
			Dim sheet As Worksheet = workbook.Worksheets(0)

			Dim Link As HyperLink = sheet.HyperLinks.Add(sheet.Range("A5"))
			Link.TextToDisplay = sheet.Range("A5").Text
			Link.Type = HyperLinkType.Url
			Link.Address = "https://en.wikipedia.org/wiki/Canada"

			Dim NewLink As HyperLink = sheet.HyperLinks.Add(sheet.Range("D13"))
			NewLink.TextToDisplay = "https://www.google.com"
			NewLink.Type = HyperLinkType.Url
			NewLink.Address = "https://www.google.com"

			workbook.SaveToFile("Hyperlink.xlsx", ExcelVersion.Version2010)
			System.Diagnostics.Process.Start("Hyperlink.xlsx")
		End Sub
	End Class
End Namespace

In order to change the look of a shape, users can add a solid, gradient, pattern or picture fill to it. Spire.Presentation supports all fill types listed. This article will introduce how to add a picture fill to a shape in C# and VB.NET.

Code Snippets:

Step 1: Initialize an instance of Presentation class.

Presentation ppt = new Presentation();

Step 2: Add a shape to the first slide.

IAutoShape shape = (IAutoShape)ppt.Slides[0].Shapes.AppendShape(ShapeType.DoubleWave, new RectangleF(100, 100, 400, 200));

Step 3: Set the FillType as picture and fill the shape with an image.

string picUrl = @"C:\Users\Administrator\Desktop\image.jpg";
shape.Fill.FillType = FillFormatType.Picture;               
shape.Fill.PictureFill.Picture.Url = picUrl;

Step 4: Set the picture fill mode to stretch.

shape.Fill.PictureFill.FillType = PictureFillType.Stretch;

Step 5: Save to file.

ppt.SaveToFile("shape.pptx", FileFormat.Pptx2010);

Output:

How to fill shape with picture in PowerPoint in C#

Full Code:

[C#]
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace FillShape
{

    class Program
    {

        static void Main(string[] args)
        {
            Presentation ppt = new Presentation();

            IAutoShape shape = (IAutoShape)ppt.Slides[0].Shapes.AppendShape(ShapeType.DoubleWave, new RectangleF(100, 100, 400, 200));
            string picUrl = @"C:\Users\Administrator\Desktop\image.jpg";
            shape.Fill.FillType = FillFormatType.Picture;
            shape.Fill.PictureFill.Picture.Url = picUrl;
            shape.Fill.PictureFill.FillType = PictureFillType.Stretch;
            shape.ShapeStyle.LineColor.Color = Color.Transparent;

            ppt.SaveToFile("shape.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("shape.pptx");
        }
    }
}
[VB.NET]
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System.Drawing

Namespace FillShape

	Class Program

		Private Shared Sub Main(args As String())
			Dim ppt As New Presentation()

			Dim shape As IAutoShape = DirectCast(ppt.Slides(0).Shapes.AppendShape(ShapeType.DoubleWave, New RectangleF(100, 100, 400, 200)), IAutoShape)
			Dim picUrl As String = "C:\Users\Administrator\Desktop\image.jpg"
			shape.Fill.FillType = FillFormatType.Picture
			shape.Fill.PictureFill.Picture.Url = picUrl
			shape.Fill.PictureFill.FillType = PictureFillType.Stretch
			shape.ShapeStyle.LineColor.Color = Color.Transparent

			ppt.SaveToFile("shape.pptx", FileFormat.Pptx2010)
			System.Diagnostics.Process.Start("shape.pptx")
		End Sub
	End Class
End Namespace

Capitals are quite suitable for stressing texts in Word. A paragraph written with capitalized letters is easy to notice, and capitalized letters imply the importance of the paragraph. This article teaches you the method of changing the case of existing text to all capitals with Spire.Doc for .NET by programming.

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

Change the Case of a Specified Paragraph to All Capitals

The detailed steps are as follows:

  • Create an object of Document and load a sample Word document from file using Document.LoadFromFile() method.
  • Get the second paragraph using Document.Sections[].Paragraph[] property and set its characters to AllCaps through TextRange.CharacterFormat.AllCaps property.
  • Get the third paragraph using Document.Sections[].Paragraph[] property and set its characters to IsSmallCaps by TextRange.CharacterFormat.IsSmallCaps property.
  • Save the document to a new Word file using Document.SaveToFile() method.

Note: AllCaps means to capitalize all the letters and set them to the same size, and IsSmallCaps means to capitalize all the letters but set the original majuscules bigger than the minuscules.

  • C#
  • VB.NET
using System;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace changecase
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new document and load from file
            string input = @"D:\testp\test.docx"; ;
            Document doc = new Document();
            doc.LoadFromFile(input);
            TextRange textRange;
            //Get the second paragraph and set its characters to AllCaps
            Paragraph para1 = doc.Sections[0].Paragraphs[2];

            foreach (DocumentObject obj in para1.ChildObjects)
            {
                if (obj is TextRange)
                {
                    textRange = obj as TextRange;
                    textRange.CharacterFormat.AllCaps = true;
                }
            }

            //Get the third paragraph and set its characters to IsSmallCaps
            Paragraph para2 = doc.Sections[0].Paragraphs[3];
            foreach (DocumentObject obj in para2.ChildObjects)
            {
                if (obj is TextRange)
                {
                    textRange = obj as TextRange;
                    textRange.CharacterFormat.IsSmallCaps = true;
                }
            }

            //Save the document to a new Word file
            string output = "ChangeCase.docx";
            doc.SaveToFile(output, FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Change Text Case to All Capitals in 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.

How to Lock or Unlock Excel Cells in WPF

2016-05-27 09:21:41 Written by Koohji

When sharing an excel worksheet, we may want to protect some valuable or important data from being changed by others. With Spire.XLS, we can easily achieve this by locking excel cells. Once locked, we will not be able to modify data from the protected cells until we unlock them. This article will explain how to lock or unlock excel cells in WPF using Spire.XLS for WPF.

Generally, there are two main steps when locking cells in an excel worksheet:

  • Locking specific cells.
  • Applying sheet protection – locking cells has no effect until we protect the worksheet.

Detail steps and code snippets are as following:

Step 1: Initialize a new instance of Workbook class, load the sample excel file and get its first worksheet.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet sheet = workbook.Worksheets[0];

Step 2: Lock or unlock specified cells.

By default, all cells in an Excel worksheet are locked, so we need to unlock them first, next lock the specified cells and protect the worksheet with password.

sheet.Range.Style.Locked = false;
sheet.Range["A1:B3"].Style.Locked = true;
sheet.Protect("123", SheetProtectionType.All);

If you want to unlock excel cells, please use the following line of code:

sheet.Range["A1:B3"].Style.Locked = false;

Step 3: Save the changes and launch the file.

workbook.SaveToFile("locked.xlsx");
System.Diagnostics.Process.Start("locked.xlsx");

Effective screenshot after locking excel cells:

How to Lock or Unlock Excel Cells in WPF

Full codes:

using Spire.Xls;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");
            Worksheet sheet = workbook.Worksheets[0];

            //Lock excel cells
            sheet.Range.Style.Locked = false;
            sheet.Range["A1:B3"].Style.Locked = true;
            sheet.Protect("123", SheetProtectionType.All);

            //Unlock excel cells
            // sheet.Range["A1:B3"].Style.Locked = false;

            workbook.SaveToFile("locked.xlsx");
            System.Diagnostics.Process.Start("locked.xlsx");
        }

    }
}

In order to format a table in PDF, Spire.PDF provides a PdfTableStyle class that represents parameters of PDF light table and a PdfCellStyle class that represents information about cell style. This article will introduce how to set the border color of a table including table border and cell border by using the two classes mentioned above.

Code Snippets:

Step 1: Define a multidimensional array of string.

string[][] dataSource =new string[5][] {new string[] {"Name", "Capital", "Continent", "Area", "Population"},
  new string[] {"Argentina","Buenos Aires", "South American", "2777815", "3230003"},
  new string[] {"Bolivia","La Paz","South America","1098575","7300000"},
  new string[] {"Brazil","Brasilia","South America","8511196","150400000"},
  new string[] {"Canada","Ottawa","North America","9976147","26500000"}
};

Step 2: Initialize a new instance of PdfDocument class and add a page to it.

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();

Step 3: Initialize a new instance of PdfTbale class, filling the table with the data predefined.

PdfTable table = new PdfTable();
table.DataSource = dataSource;

Step 4: Initialize an instance of PdfTableStyle class, and set the color of table border as Blue, then apply the style to PdfTable.Style.

PdfTableStyle style = new PdfTableStyle();
style.BorderPen = new PdfPen(Color.Blue, 1f);
table.Style = style;

Step 5: The syntax to set style of cell border is much different from setting table border style, since PdfTable class doesn't contain a property of CellStyle. Instead, CellStyle is a property included in BeginRowLayoutEventArgs class, which is an argument of StratRowLayout event. Therefore we customize a method as below to set the color of cell border.

public static void table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
{
    PdfCellStyle cellStyle = new PdfCellStyle();
    cellStyle.BorderPen = new PdfPen(Color.Red, 0.5f);
    args.CellStyle = cellStyle;
}

In the Main method, "table_BeginRowLayout" must be added to BeginRowLayout event to ensure the custom method will be invoked when the event occurs.

table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout);

Step 6: Draw the table on PDF page and save to file.

table.Draw(page, new PointF(0,40));
pdf.SaveToFile(@"SetBorderColor.pdf"); 

Output:

 

How to Set the Border Color of Table in PDF in C#

 

Full Code:

using Spire.Pdf;
using Spire.Pdf.Tables;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace SetBorderColorOfTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //input data
            string[][] dataSource =new string[5][] {new string[] {"Name", "Capital", "Continent", "Area", "Population"},
              new string[] {"Argentina","Buenos Aires", "South American", "2777815", "3230003"},
              new string[] {"Bolivia","La Paz","South America","1098575","7300000"},
              new string[] {"Brazil","Brasilia","South America","8511196","150400000"},
              new string[] {"Canada","Ottawa","North America","9976147","26500000"}
            };

            //initialize an instance of PdfDocument
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

            //initialize an instance of PdfTable
            PdfTable table = new PdfTable();            
            table.DataSource = dataSource;   
                     
            //set the color of table border
            PdfTableStyle style = new PdfTableStyle();
            style.BorderPen = new PdfPen(Color.Blue, 1f);
            table.Style = style;

            //add custom method to BeginRowLayout event
            table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout);

            //draw table on PDF and save file
            table.Draw(page, new PointF(0,40));
            pdf.SaveToFile(@"SetBorderColor.pdf");
            System.Diagnostics.Process.Start(@"SetBorderColor.pdf");
        }
        //customize a method to set color of cell border
        public static void table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
        {
            PdfCellStyle cellStyle = new PdfCellStyle();
            cellStyle.BorderPen = new PdfPen(Color.Red, 0.5f);
            args.CellStyle = cellStyle;
        }
    }
}

By using Acrobat, we can edit the PDF bookmark actions and set the zoom level to "Inherit Zoom". Then no matter which bookmark you click, the PDF page will stay the same size as the previous page you are viewing and it won't be changed. Spire.PDF also enables developers to set the Bookmark actions to inherit zoom by setting PdfDestination.Zoom as 0. This article will show you how to update bookmarks in a PDF document in C#.

Firstly, view the original screenshot of the PDF bookmark property:

How to set inherit zoom property for PDF bookmarks

Here comes to the step of how to use Spire.PDF to set the PDF bookmark actions.

Step 1: Create a new PDF document and load the document from file.

PdfDocument pdfdoc = new PdfDocument();
pdfdoc.LoadFromFile("TheGreatGatsby.pdf");

Step 2: Get bookmarks collections of the PDF file.

PdfBookmarkCollection bookmarks = pdfdoc.Bookmarks;

Step 3: Set Zoom level as 0, which the value is inherit zoom.

foreach (PdfBookmark bookMark in bookmarks)
 {
   //value 1 is the actual size, other value is the customized size.
   bookMark.Destination.Zoom =0;                              
 }

Step 4: Save the document to file.

pdfdoc.SaveToFile("result.pdf");

Effective screenshot after setting the zoom level to Inherit zoom.

How to set inherit zoom property for PDF bookmarks

Full codes:

using Spire.Pdf;
using Spire.Pdf.Bookmarks;


namespace SetInheritZoomProperty
{
    class Program
    {
        static void Main(string[] args)
        {

            PdfDocument pdfdoc = new PdfDocument();
            pdfdoc.LoadFromFile("TheGreatGatsby.pdf");

            PdfBookmarkCollection bookmarks = pdfdoc.Bookmarks;

            foreach (PdfBookmark bookMark in bookmarks)
            {
                bookMark.Destination.Zoom = 0;

            }

            pdfdoc.SaveToFile("result.pdf");
        }
    }
}
page 37