The sample demonstrates how to Design Excel Cell Borders in Silverlight via Spire.XLS.

PDF Header and Footer in Silverlight

2012-02-13 08:58:50 Written by Koohji

The sample demonstrates how to create PDF Header and Footer in Silverlight via Spire.PDF.

Paginate PDF File in C#/VB.NET

2012-02-13 08:03:27 Written by Koohji

PDF pagination not only enables users to add page numbers in PDF file but also can divide PDF document into sections so that some additional information such as PDF file cover, introduction or reference can be added. So PDF pagination provides great convenience for managing large files or organizing books. This section will show you a solution to paginate PDF pages via a .NET PDF component in C#, VB.NET.

Spire.PDF for .NET, a PDF component for managing PDF files, enables you to paginate PDF pages quickly with C#, VB.NET. Before expounding the main code, I want to show the screenshot of the output PDF file as below:

Set PDF Pagination

Here you can download Spire.PDF for .NET and install it on your system. After adding Spire.Pdf dll, let us start to paginate PDF file together.

In the whole solution, there are three main steps to paginate PDF file. One is to draw page number; another is to draw content; the last one is to draw PDF cover so that you can add additional information. Let us see them one by one.

Paginate PDF - Draw PDF Page Number

When drawing page number, page label is also set in below method. By calling this method: DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y, PdfStringFormat format). You can not only set page label font, color, position and format. Please see detail code below:

[C#]
            foreach (PdfPageBase page in section.Pages)
            {
                page.Canvas.SetTransparency(0.5f);
                PdfBrush brush = PdfBrushes.Black;
                PdfPen pen = new PdfPen(brush, 0.75f);
                PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic), true);
                PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
                format.MeasureTrailingSpaces = true;
                float space = font.Height * 0.75f;
                float x = margin.Left;
                float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;
                float y = page.Canvas.ClientSize.Height - margin.Bottom + space;
                page.Canvas.DrawLine(pen, x, y, x + width, y);
                y = y + 1;
                String numberLabel
                    = String.Format("{0} of {1}", startNumber++, pageCount);
                page.Canvas.DrawString(numberLabel, font, brush, x + width, y, format);
                page.Canvas.SetTransparency(1);
            }
[VB.NET]
         For Each page As PdfPageBase In section.Pages
            page.Canvas.SetTransparency(0.5!)
            Dim brush As PdfBrush = PdfBrushes.Black
            Dim pen As PdfPen = New PdfPen(brush, 0.75!)
            Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial", 9!, FontStyle.Italic), true)
            Dim format As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Right)
            format.MeasureTrailingSpaces = true
            Dim space As Single = (font.Height * 0.75!)
            Dim x As Single = margin.Left
            Dim width As Single = (page.Canvas.ClientSize.Width  _
                        - (margin.Left - margin.Right))
            Dim y As Single = ((page.Canvas.ClientSize.Height - margin.Bottom)  _
                        + space)
            page.Canvas.DrawLine(pen, x, y, (x + width), y)
            y = (y + 1)
            Dim numberLabel As String = String.Format("{0} of {1}", startNumber++, pageCount)
            page.Canvas.DrawString(numberLabel, font, brush, (x + width), y, format)
            page.Canvas.SetTransparency(1)
        Next

Paginate PDF - Draw PDF Content

PDF content depends on your own like, so here I avoid coding them in detail.

Paginate PDF - Draw PDF Cover

Cover page is always the best place for adding additional information about this PDF file. In below method, I ignore adding reference content and document title, you can add cover image a class PdfImage in the PDF cover page. Please see code below:

[C#]
            //cover
            PdfBrush brush3 = PdfBrushes.Black;
            PdfBrush brush4 = new PdfSolidBrush(new PdfRGBColor(0xf9, 0xf9, 0xf9));
            PdfImage image
                = PdfImage.FromFile(@"..\potala palace1.jpg");
            String text = paginate_PDF.Properties.Resources.ImageDescription;
            float r = image.PhysicalDimension.Height / image.Height;
            PdfPen pen = new PdfPen(brush1, r);
            SizeF size = font1.MeasureString(text, image.PhysicalDimension.Width - 2);
            PdfTemplate template
                = new PdfTemplate(image.PhysicalDimension.Width + 4 * r + 4,
                    image.PhysicalDimension.Height + 4 * r + 7 + size.Height);
            template.Graphics.DrawRectangle(pen, brush4, 0, 0, template.Width, template.Height);
[VB.NET]
        'cover
        Dim brush3 As PdfBrush = PdfBrushes.Black
        Dim brush4 As PdfBrush = New PdfSolidBrush(New PdfRGBColor(249, 249, 249))
        Dim image As PdfImage = PdfImage.FromFile("..\potala palace1.jpg")
        Dim text As String = paginate_PDF.Properties.Resources.ImageDescription
        Dim r As Single = (image.PhysicalDimension.Height / image.Height)
        Dim pen As PdfPen = New PdfPen(brush1, r)
        Dim size As SizeF = font1.MeasureString(text, (image.PhysicalDimension.Width - 2))
        Dim template As PdfTemplate = New PdfTemplate((image.PhysicalDimension.Width  _
                        + ((4 * r)  _
                        + 4)), (image.PhysicalDimension.Height  _
                        + ((4 * r) + (7 + size.Height))))
        template.Graphics.DrawRectangle(pen, brush4, 0, 0, template.Width, template.Height)

Spire.PDF for .NET is a professional PDF component to generate, edit, read, and manipulate PDF documents in your .NET applications with C#, VB.NET.

In a PDF document, the background refers to the overall visual appearance behind the content of the pages. The background can be a simple solid color or an image of your choice. Adding backgrounds to PDFs can help you add visual interest to your documents and also enhance readability. In this article, you will learn how to programmatically set the background color or image for PDF 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 DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF 

Add Background Color to PDF Documents in C# and VB.NET

The PdfPageBase.BackgroundColor property offered by Spire.PDF for .NET allows you to set a solid color as the PDF background. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Loop through all the PDF pages and add a background color to each page using PdfPageBase.BackgroundColor property.
  • Set the opacity of the background using PdfPageBase.BackgroudOpacity property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using System.Drawing;

namespace PDFBackgroundColor
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a sample PDF file from disk
            pdf.LoadFromFile("input.pdf");

            //Loop through the pages in the PDF file
            foreach (PdfPageBase page in pdf.Pages)
            {
                //Set the background color for each page
                page.BackgroundColor = Color.Yellow;

                //Set the opacity of the background
                page.BackgroudOpacity = 0.1f;
            }

            //Save the result PDF file
            pdf.SaveToFile("BackgroundColor.pdf");
            pdf.Close();

        }
    }
}

C#/VB.NET: Add Background Color or Background Image to PDF

Add Background Images to PDF Documents C# and VB.NET

If you want to add an image as a background to match the document theme, you can use the PdfPageBase.BackgroundImage property. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Loop through all the PDF pages and add a background picture to each page using PdfPageBase.BackgroundImage property.
  • Set the opacity of the background using PdfPageBase.BackgroudOpacity property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using System.Drawing;

namespace PDFBackgroundImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a sample PDF file from disk
            pdf.LoadFromFile("input.pdf");

            //Load an image
            Image background = Image.FromFile("background.png");

            //Loop through the pages in the PDF file
            foreach (PdfPageBase page in pdf.Pages)
            {
                //Set the loaded image as the background image for each page
                page.BackgroundImage = background;

                //Set the opacity of the background
                page.BackgroudOpacity = 0.2f;
            }

            //Save the result PDF file
            pdf.SaveToFile("BackgroundImage.pdf");
            pdf.Close();

        }
    }
}

C#/VB.NET: Add Background Color or Background Image to 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 Footer in Silverlight

2012-02-10 06:46:37 Written by Koohji

The sample demonstrates how to create Word Footer in Silverlight via Spire.Doc.

The sample demonstrates how to use Excel Filter function in Silverlight via Spire.XLS.

C#: Convert RTF to HTML, Image

2024-09-10 02:49:00 Written by Koohji

RTF, or Rich Text Format, is a file format designed for storing text and formatting information. While processing RTF files, sometimes you might need to convert them into a more web-friendly format such as HTML, or convert to images for better sharing and archiving purposes. In this article, you will learn how to convert RTF to HTML or images in C# 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

Convert RTF to HTML in C#

Converting RTF to HTML ensures that the document can be easily viewed and edited in any modern web browser without requiring any additional software.

With Spire.Doc for .NET, you can achieve RTF to HTML conversion through the Document.SaveToFile(string fileName, FileFormat.Html) method. The following are the detailed steps.

  • Create a Document instance.
  • Load an RTF document using Document.LoadFromFile() method.
  • Save the RTF document in HTML format using Document.SaveToFile(string fileName, FileFormat.Html) method.
  • C#
using Spire.Doc;

namespace ConvertRtfToHtml
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load an RTF document 
            document.LoadFromFile("input.rtf");

            //Save as HTML format
            document.SaveToFile("RtfToHtml.html", FileFormat.Html);
        }
    }
}

C#: Convert RTF to HTML, Image

Convert RTF to Image in C#

To convert RTF to images, we can use the Document.SaveToImages() method to convert an RTF file into individual Bitmap or Metafile images. Then, the Bitmap or Metafile images can be saved as a BMP, EMF, JPEG, PNG, GIF, or WMF format files. The following are the detailed steps.

  • Create a Document object.
  • Load an RTF document using Document.LoadFromFile() method.
  • Convert the document to images using Document.SaveToImages() method.
  • Iterate through the converted image, and then save each as a PNG file using Image[].Save(string fileName, ImageFormat format) method.
  • C#
using Spire.Doc;
using System.Drawing.Imaging;
using System.Drawing;
using Spire.Doc.Documents;

namespace ConvertRtfToImage

{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load an RTF document 
            document.LoadFromFile("input.rtf");

            //Convert the RTF document to images
            Image[] images = document.SaveToImages(ImageType.Bitmap);

            // Iterate through the image collection
            for (int i = 0; i < images.Length; i++)
            {
                //Save the image as png format
                string outputfile = string.Format("image-{0}.png", i);
                images[i].Save(outputfile, ImageFormat.Png);
            }
        }
    }
}

C#: Convert RTF to HTML, Image

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.

Excel spreadsheet is a widely used file format that enables users to organize, analyze, and present data in a tabular format. The ability to interact with Excel files programmatically is highly valuable, as it allows automation and integration of Excel functionality into software applications. This capability is particularly useful when working with large datasets, performing complex calculations, or when data needs to be dynamically generated or updated. In this article, you will learn how to create, read, or update Excel documents in C# and VB.NET using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Create an Excel File in C#, VB.NET

Spire.XLS for .NET offers a variety of classes and interfaces that you can use to create and edit Excel documents. Here is a list of important classes, properties and methods involved in this article.

Member Description
Workbook class Represents an Excel workbook model.
Workbook.Worksheets.Add() method Adds a worksheet to workbook.
Workbook.SaveToFile() method Saves the workbook to an Excel document.
Worksheet class Represents a worksheet in a workbook.
Worksheet.Range property Gets a specific cell or cell range from worksheet.
Worksheet.Range.Value property Gets or sets the value of a cell.
Worksheet.Rows property Gets a collection of rows in worksheet.
Worksheet.InsertDataTable() method Imports data from DataTable to worksheet.
CellRange class Represents a cell or cell range in worksheet.

The following are the steps to create an Excel document from scratch using Spire.XLS for .NET.

  • Create a Workbook object.
  • Add a worksheet using Workbook.Worksheets.Add() method.
  • Write data to a specific cell through Worksheet.Range.Value property.
  • Import data from a DataTable to the worksheet using Worksheet.InsertDataTable() method.
  • Save the workbook to an Excel document using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
using System.Data;

namespace CreateExcelSpreadsheet
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook wb = new Workbook();

            //Remove default worksheets
            wb.Worksheets.Clear();

            //Add a worksheet and name it "Employee"
            Worksheet sheet = wb.Worksheets.Add("Employee");

            //Merge the cells between A1 and G1
            sheet.Range["A1:G1"].Merge();

            //Write data to A1 and apply formatting to it
            sheet.Range["A1"].Value = "Basic Information of Employees of Huanyu Automobile Company";
            sheet.Range["A1"].HorizontalAlignment = HorizontalAlignType.Center;
            sheet.Range["A1"].VerticalAlignment = VerticalAlignType.Center;
            sheet.Range["A1"].Style.Font.IsBold = true;
            sheet.Range["A1"].Style.Font.Size = 13F;

            //Set row height of the first row
            sheet.Rows[0].RowHeight = 30F;

            //Create a DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Gender");
            dt.Columns.Add("Birth Date");
            dt.Columns.Add("Educational Background");
            dt.Columns.Add("Contact Number");
            dt.Columns.Add("Position");
            dt.Columns.Add("ID");
            dt.Rows.Add("Allen", "Male", "1990-02-10", "Bachelor", "24756854", "Mechanic", "0021");
            dt.Rows.Add("Patrick", "Male", "1985-06-08", "Master", "59863247", "Mechanic", "0022");
            dt.Rows.Add("Jenna", "Female", "1989-11-25", "Bachelor", "79540352", "Sales", "0023");
            dt.Rows.Add("Tommy", "Male", "1988-04-16", "Master", "52014060", "Mechanic", "0024");
            dt.Rows.Add("Christina", "Female", "1998-01-21", "Bachelor", "35401489", "HR", "0025");

            //Import data from DataTable to worksheet
            sheet.InsertDataTable(dt, true, 2, 1, true);

            //Set row height of a range
            sheet.Range["A2:G7"].RowHeight = 15F;

            //Set column width 
            sheet.Range["A2:G7"].Columns[2].ColumnWidth = 15F;
            sheet.Range["A2:G7"].Columns[3].ColumnWidth = 21F;
            sheet.Range["A2:G7"].Columns[4].ColumnWidth = 15F;

            //Set border style of a range
            sheet.Range["A2:G7"].BorderAround(LineStyleType.Medium);
            sheet.Range["A2:G7"].BorderInside(LineStyleType.Thin);
            sheet.Range["A2:G2"].BorderAround(LineStyleType.Medium);
            sheet.Range["A2:G7"].Borders.KnownColor = ExcelColors.Black;

            //Save to a .xlsx file
            wb.SaveToFile("NewSpreadsheet.xlsx", FileFormat.Version2016);
        }
    }
}

C#/VB.NET: Create, Read, or Update Excel Documents

Read Data of a Worksheet in C#, VB.NET

The Worksheet.Range.Value property returns number value or text value of a cell as a string. To get data of a whole worksheet or a cell range, loop through the cells within it. The following are the steps to get data of a worksheet using Spire.XLS for .NET.

  • Create a Workbook object.
  • Load an Excel document using Workbook.LoadFromFile() method.
  • Get a specific worksheet through Workbook.Worksheets[index] property.
  • Get the cell range containing data though Worksheet.AllocatedRange property.
  • Iterate through the rows and columns to get cells within the range, and return the value of each cell through CellRange.Value property.
  • C#
  • VB.NET
using Spire.Xls;

namespace ReadExcelData
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook wb = new Workbook();

            //Load an existing Excel file
            wb.LoadFromFile(@"C:\Users\Administrator\Desktop\NewSpreadsheet.xlsx");

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

            //Get the cell range containing data
            CellRange locatedRange = sheet.AllocatedRange;

            //Iterate through the rows
            for (int i = 0;i < locatedRange.Rows.Length;i++)
            {
                //Iterate through the columns
                for (int j = 0; j < locatedRange.Rows[i].ColumnCount; j++)
                {
                    //Get data of a specific cell
                    Console.Write(locatedRange[i + 1, j + 1].Value + "  ");

                }
                Console.WriteLine();            
            }
        }
    }
}

C#/VB.NET: Create, Read, or Update Excel Documents

Update an Excel Document in C#, VB.NET

To change the value of a certain cell, just re-assign a value to it through Worksheet.Range.Value property. The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document using Workbook.LoadFromFile() method.
  • Get a specific worksheet through Workbook.Worksheets[index] property.
  • Change the value of a particular cell though Worksheet.Range.Value property.
  • Save the workbook to an Excel file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace UpdateCellValue
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook wb = new Workbook();

            //Load an existing Excel file
            wb.LoadFromFile(@"C:\Users\Administrator\Desktop\NewSpreadsheet.xlsx");

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

            //Change the value of a specific cell
            sheet.Range["A1"].Value = "Updated Value";

            //Save to file
            wb.SaveToFile("Updated.xlsx", ExcelVersion.Version2016);
        }
    }
}

C#/VB.NET: Create, Read, or Update Excel Documents

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.

Setting a background in Excel can offer several benefits that enhance the usability and visual appeal of your workbook. For example, you can set background colors or images that align with company branding or specific themes, making your spreadsheets visually cohesive with other marketing or business materials. In this article, you will learn how set a background color or image for an Excel worksheet in C# using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Add a Background Color for an Excel Worksheet

Spire.XLS for .NET provides the CellRange.Style.Color property to set the background color for a specified cell range. The following are the detailed steps:

  • Create a Workbook object.
  • Load an Excel workbook using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Get the used range in the worksheet through Worksheet.AllocatedRange property.
  • Set a background color for the used range through CellRange.Style.Color property.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;
using System.Drawing;

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

            // Create a Workbook object
            Workbook workbook = new Workbook();

            // Load an Excel workbook 
            workbook.LoadFromFile("Budget.xlsx");

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

            // Get the used range of the worksheet
            CellRange usedRange = sheet.AllocatedRange;

            // Set the background color of the range
            usedRange.Style.Color = Color.FromArgb(200, 233, 255);

            // Save the workbook 
            workbook.SaveToFile("ExcelBackgroundColor.xlsx", ExcelVersion.Version2016);
        }
    }
}

Set a light blue background color for a specified range in the first worksheet

Add a Background Image for an Excel Worksheet

To insert a background image in Excel, you can load an image first and then set it as the worksheet background through the Worksheet.PageSetup.BackgroundImage property. The following are the detailed steps:

  • Create a Workbook object.
  • Load an Excel workbook using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Load an image using Image.FromFile() method.
  • Set the image as the background of the worksheet through Worksheet.PageSetup.BackgroundImage property.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;
using System.Drawing;

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

            // Create a Workbook object
            Workbook workbook = new Workbook();

            // Load an Excel workbook 
            workbook.LoadFromFile("Budget.xlsx");

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

            // Load an image
            Bitmap image = new Bitmap(Image.FromFile("C:\\Users\\Administrator\\Desktop\\bg.jpg"));

            // Set the background of the worksheet
            sheet.PageSetup.BackgoundImage = image;
             
            // Save the workbook 
            workbook.SaveToFile("ExcelBackgroundImage.xlsx", ExcelVersion.Version2016);
        }
    }
}

Insert a picture as the background of the first worksheet in Excel

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.

Merge PDF Documents in Silverlight

2012-01-13 03:11:49 Written by Koohji

The sample demonstrates how to Merge PDF documents in Silverlight via Spire.XLS.

(No Screenshots)

page 77