We have already demonstrated how to insert footnote to the word document with the help of Spire.Doc. This article we will show you how to set the position and number format for the footnote. The footnote position can be at the bottom of each page or below text. The default number format for the footnote is “1, 2, 3”. The following example shows how to set the position, number format, and the restart rule of footnote by calling the property of Section.FootnoteOptions.

Firstly, view the Footnote options under Microsoft Word and the original sample document file:

How to set the position and number format for word footnote in C#

How to set the position and number format for word footnote in C#

How to set the position and number format for word footnote in C#

Step 1: Create a new instance of Document and load the document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx",FileFormat.Docx2013);

Step 2: Get the first section from the document.

Section sec = doc.Sections[0];

Step 3: Set the number format, restart rule and position for the footnote.

sec.FootnoteOptions.NumberFormat = FootnoteNumberFormat.UpperCaseLetter;
sec.FootnoteOptions.RestartRule = FootnoteRestartRule.RestartPage;
sec.FootnoteOptions.Position = FootnotePosition.PrintAtBottomOfPage;

Step 4: Save the document to file.

doc.SaveToFile("Footnoteoptions.docx", FileFormat.Docx2013);

Effective screenshot after setting the formatting for the footnote.

How to set the position and number format for word footnote in C#

How to set the position and number format for word footnote in C#

Full codes of how to set the footnote options:

Document doc = new Document();
doc.LoadFromFile("Sample.docx", FileFormat.Docx2013);

Section sec = doc.Sections[0];

sec.FootnoteOptions.NumberFormat = FootnoteNumberFormat.UpperCaseLetter;
sec.FootnoteOptions.RestartRule = FootnoteRestartRule.RestartPage;
sec.FootnoteOptions.Position = FootnotePosition.PrintAtBottomOfPage;

////Clear all the formatting for the footnote and back to the default opitions
//sec.FootnoteOptions.ClearFormatting();

doc.SaveToFile("Footnoteoptions.docx", FileFormat.Docx2013);
Wednesday, 29 November 2017 00:38

Hide Formulas When Protecting a Worksheet in C#

If your worksheet has some important formulas that you don’t want others to view, you may want to hide these formulas. This article demonstrates how to hide formulas when protecting a worksheet using Spire.XLS and C#.

The XlsRange.IsFormulaHidden property is used to determine if the formula will be hidden when the worksheet is protected. You can hide the formulas in a specific cell range by setting the XlsRange.IsFormulaHidden property to true, but note that the formulas can be hidden only if the worksheet is protected, they won’t be hidden if the workbook is protected while the worksheet is not.

//Initialize an object of Workbook class 
Workbook workbook = new Workbook();
//Load the Excel file
workbook.LoadFromFile("Input.xlsx");

//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];

//Hide the formulas in the used range
sheet.AllocatedRange.IsFormulaHidden = true;

//Protect the worksheet with password
sheet.Protect("123");

//Save the file
workbook.SaveToFile("HideFormulas.xlsx", ExcelVersion.Version2013);

Screenshot:

Hide Formulas When Protecting a Worksheet in C#

Monday, 27 November 2017 08:42

C# Encrypt and Decrypt PDF file

Spire.PDF has a powerful function to encrypt and decrypt the PDF document with password. We have done some adjustments by the code to protect the PDF files. If you are using the Spire.PDF Version before 3.9.421, please refer to tutorial here for decrypt PDF, encrypt PDF in C#. From Spire.PDF v 3.9.421, you should use the new method offers in this article. This example shows how to protect and unprotect the PDF file with password using C# via the following two parts:

  • Encrypt PDF
  • Decrypt PDF

Encrypt PDF

There are two kinds of password for encrypt PDF, open password and modify password. The open password only sets to open the password. The modify password not only sets to open the PDF file, but also to set the restrictions of printing, contents copying, commenting and so on.

Spire.PDF offers pdf.Security.Encrypt(string openPassword, string permissionPassword, PdfPermissionsFlags permissions, PdfEncryptionKeySize keySize) methods for us to encrypt the PDF file from opening, access permissions and set the PDF key size.

//load the document from file
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");

//encrypt the PDF file
pdf.Security.Encrypt("open", "permission", PdfPermissionsFlags.Print|PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit);

//save the document to file
pdf.SaveToFile("Encrypted.pdf", FileFormat.PDF);

C# Encrypt and Decrypt PDF file

Decrypt PDF

We need to load the encrypted PDF file with password (either open password or modify password is OK) by calling the method PdfDocument.LoadFromFile (string filename, string password). And then use the method of pdf.Security.Encrypt(string openPassword, string permissionPassword, PdfPermissionsFlags permissions, PdfEncryptionKeySize keySize, string originalPermissionPassword) to reset the password or set the password as empty to decrypt the PDF file.

//use the open password to load the encrypted PDF file
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Encrypted.pdf", "open");

//set the password as empty to decrypt PDF 
pdf.Security.Encrypt(string.Empty, string.Empty, PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key128Bit, "permission");

//save the document to file
pdf.SaveToFile("Decrypt.pdf", FileFormat.PDF);

C# Encrypt and Decrypt PDF file

Monday, 27 November 2017 01:56

How to Create EAN-13 Barcode in C#

EAN-13, based upon the UPC-A standard, is used world-wide for marking retail goods. The 13-digit EAN-13 number consists of four components:

  • Country code - 2 or 3 digits
  • Manufacturer Code - 5 to 7 digits
  • Product Code - 3 to 5 digits
  • Check digit - last digit

The following code snippets demonstrate how to create EAN-13 barcode image using Spire.Barcode in C#.

Step 1: Create a BarcodeSettings instance.

BarcodeSettings settings = new BarcodeSettings();

Step 2: Set the barcode type as EAN13.

settings.Type = BarCodeType.EAN13;

Step 3: Set the data to encode.

settings.Data = "123456789012";

Step 4: Calculate checksum and add the check digit to barcode.

settings.UseChecksum = CheckSumMode.ForceEnable;

Step 5: Display barcode's text on bottom and centrally align the text.

settings.ShowTextOnBottom = true;
settings.TextAlignment = StringAlignment.Center;

Step 6: Generate barcode image based on the settings and save it in .png format.

BarCodeGenerator generator = new BarCodeGenerator(settings);
Image image = generator.GenerateImage();
image.Save("EAN-13.png", System.Drawing.Imaging.ImageFormat.Png);

Output:

How to Create EAN-13 Barcode in C#

Full Code:

BarcodeSettings settings = new BarcodeSettings();
settings.Type = BarCodeType.EAN13;
settings.Data = "123456789012";         
settings.UseChecksum = CheckSumMode.ForceEnable;
settings.ShowTextOnBottom = true;
settings.TextAlignment = StringAlignment.Center;
BarCodeGenerator generator = new BarCodeGenerator(settings);
Image image = generator.GenerateImage();
image.Save("EAN-13.png", System.Drawing.Imaging.ImageFormat.Png);

When we work with the pie chart on the presentation slide, we may need to separate each part of pie chart to make them stand out. This article is going to introduce the method of how to set the pie explosion for the pie chart on the presentation slides in C# by using Spire.Presentation.

Spire.Presentation offers a property of chart.Series[].Distance to enable developers to pull the whole pie apart by exploding the pie chart.

On Microsoft PowerPoint, We can adjust the percentage of "Pie Explosion" on the Series Options at the "Format Data Series" area to control the distance between each section in the chart.

How to explode a pie chart on a presentation slide in C#

Step 1: Create a presentation document and load the file from disk.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx");

Step 2: Get the chart that needs to set the point explosion.

IChart chart = ppt.Slides[0].Shapes[0] as IChart;

Step 3: Explode the pie chart.

chart.Series[0].Distance = 15;

Step 4: Save the document to file.

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

Effective screenshots after exploding the pie chart on presentation slide:

How to explode a pie chart on a presentation slide in C#

Full codes:

Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx");

IChart chart = ppt.Slides[0].Shapes[0] as IChart;
 
chart.Series[0].Distance = 15;
 
ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);
Tuesday, 21 November 2017 08:26

Show Print Preview of PDF file in C#

At some point, we may want to display a PDF file as it will appear when printed. This article demonstrates how to show print preview of a PDF file in Windows Forms application using Spire.PDF and c#.

Before using the following code, we need to create a windows forms application, add a PrintPreviewControl control to the form and reference Spire.Pdf.dll into the application.

using System;
using System.Windows.Forms;
using Spire.Pdf;

namespace PreviewPDF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
               
        private void printPreviewControl1_Click(object sender, EventArgs e)
        {
            //Load PDF file
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("New Zealand.pdf");

            //Set the PrintPreviewControl.Rows and PrintPreviewControl.Columns properties to show multiple pages
            this.printPreviewControl1.Rows = 2;
            this.printPreviewControl1.Columns = 2;

            //Preview the pdf file
            pdf.Preview(this.printPreviewControl1);  
        }             
    }
}

Screenshot:

Show Print Preview of PDF file in C#

Friday, 17 November 2017 07:15

How to Create DataMatrix Barcode in C#

A DataMatrix code is a two-dimensional barcode consisting of black and white "cells" or modules arranged in either a square or rectangular pattern. The information to be encoded can be text or numeric data.

Following code snippets show how to create a DataMatrix barcode image using Spire.Barcode.

Step 1: Create a BarcodeSettings instance.

BarcodeSettings settings = new BarcodeSettings();

Step 2: Set the width of barcode module

settings.X = 2;

Step 3: Set the barcode type as DataMatrix.

settings.Type = BarCodeType.DataMatrix;

Step 4: Set the data and display text

settings.Data = "ABC 123456789ABC 123456789ABC 123456789";
settings.Data2D = "ABC 123456789ABC 123456789ABC 123456789";

Step 5: Set the DataMatrix symbol shape to square.

settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Square;

Step 6: Generate barcode image based on the settings and save it in .png format.

BarCodeGenerator generator = new BarCodeGenerator(settings);
Image image = generator.GenerateImage();
image.Save("DataMatrix.png", System.Drawing.Imaging.ImageFormat.Png);

How to Create DataMatrix Barcode in C#

To create a rectangular DataMatrix barcode, set the DataMatrixSymbolShape property to Rectangle.

settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Rectangle;

How to Create DataMatrix Barcode in C#

Full Code:

BarcodeSettings settings = new BarcodeSettings();
settings.Type = BarCodeType.DataMatrix;
settings.X = 1.5f;
settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Square;
//rectangular DataMatrix barcode
//settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Rectangle;  
settings.Data = "ABC 123456789ABC 123456789ABC 123456789";
settings.Data2D = "ABC 123456789ABC 123456789ABC 123456789";
BarCodeGenerator generator = new BarCodeGenerator(settings);
Image image = generator.GenerateImage();
image.Save("DataMatrix.png", System.Drawing.Imaging.ImageFormat.Png);
Thursday, 16 November 2017 06:34

Scan Barcode in Java

Spire.Barcode for Java supports to generate various types of Barcodes such as Code 128, Pdf 417, QR code etc. At the same time, it also supports to read data from the generated barcode images. This article demonstrates how to scan a barcode image using Spire.Barcode for Java.

Prerequisite:

Import Spire.Barcode.jar file

Download Spire.Barcode for Java and import Spire.Barcode.jar file into the project. The detail steps of how to import Spire.Barcode.jar can be found in this article: How to Create Barcode Using Spire.Barcode for Java.

Code:

To scan a barcode image, we can use the scan method in BarcodeScanner class.

import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeScanner;

public class SpireBarcode_test {

	public static void main(String[] args) throws Exception {
        
        String[] datas = BarcodeScanner.scan("C:\\code128.jpg", BarCodeType.Code_128);       
        System.out.print(datas[0]);
	}
}

Code 128 image:

Scan Barcode in Java

Output:

Scan Barcode in Java

We have already demonstrated how to add a brand new TOC when we create the word documents. This article will show you how to insert a TOC to the existing word documents with styles and remove the TOC from the word document.

Firstly, view the sample document with Title, Heading1 and Heading 2 styles:

C# insert and remove TOC from the word document

The below code snippet shows how to insert a Table of contents (TOC) into a document.

//Create a new instance of Document and load the document from file.
Document doc = new Document();
doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);

//Add the TOC to the document
TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\h \\z \\u}");
Paragraph p = doc.LastSection.Paragraphs[0];
p.Items.Add(toc);
p.AppendFieldMark(FieldMarkType.FieldSeparator);
p.AppendText("TOC");
p.AppendFieldMark(FieldMarkType.FieldEnd);
doc.TOC = toc;

//Update the table of contents
doc.UpdateTableOfContents();

//Save the document to file
doc.SaveToFile("Result.docx", FileFormat.Docx);

C# insert and remove TOC from the word document

Removing a Table of Contents from the Document

//load the document from file with TOC 
Document doc = new Document();
doc.LoadFromFile("Result.docx",FileFormat.Docx2010);

//get the first body from the first section
Body body = doc.Sections[0].Body;

//remove TOC from first body
Regex regex = new Regex("TOC\\w+");
for (int i = 0; i < body.Paragraphs.Count; i++)
 {
     if (regex.IsMatch(body.Paragraphs[i].StyleName))
     {
         body.Paragraphs.RemoveAt(i);
         i--;
     }
}
//save the document to file
doc.SaveToFile("RemoveTOC.docx", FileFormat.Docx2010);

C# insert and remove TOC from the word document

Friday, 10 November 2017 06:46

How to Compress PDF Images in C#, VB.NET

Compressing images is an effective way for reducing the size of a scanned PDF or a normal PDF that contains multiple high quality images inside. Spire.PDF provides a straightforward method TryCompressImage() to compress the high resolution images in PDF.

Step 1: Create a PdfDocument instance and load a sample PDF file.

PdfDocument doc = new PdfDocument("Input.pdf");

Step 2: Disable increment update when updating the document. The TryCompressImage() method will make changes on the existing bytes in the file and overwrite the original.

doc.FileInfo.IncrementalUpdate = false;

Step 3: Traverse the Pages collection to get all images in the document. Invoke TryCompressImage() method to try to compress each of them.

foreach (PdfPageBase page in doc.Pages)
{
    if (page != null)
    {
        if (page.ImagesInfo != null)
        {
            foreach (PdfImageInfo info in page.ImagesInfo)
            {
                page.TryCompressImage(info.Index);
            }
        }
    }                              
}

Step 4: Save the file.

doc.SaveToFile("Output.pdf");

Result:

How to Compress PDF Images in C#, VB.NET

Full Code:

[C#]
PdfDocument doc = new PdfDocument("Input.pdf");
doc.FileInfo.IncrementalUpdate = false;
foreach (PdfPageBase page in doc.Pages)
{
    if (page != null)
    {
        if (page.ImagesInfo != null)
        {
            foreach (PdfImageInfo info in page.ImagesInfo)
            {
                page.TryCompressImage(info.Index);
            }
        }
    }                              
}                                
doc.SaveToFile("Output.pdf");
[VB.NET]
Dim doc As New PdfDocument("Input.pdf")
doc.FileInfo.IncrementalUpdate = False
For Each page As PdfPageBase In doc.Pages

    If page IsNot Nothing Then
        If page.ImagesInfo IsNot Nothing Then

            For Each info As PdfImageInfo In page.ImagesInfo
                page.TryCompressImage(info.Index)

            Next
        End If
    End If
Next
doc.SaveToFile("Output.pdf")