PDF layer is a feature that arranges the content of a PDF file in layers, which allows users to selectively set some content to be visible and others to be invisible in the same PDF file. PDF layers are a common element used in layered artwork, maps and CAD drawings. This article will demonstrate how to programmatically add, hide or delete layers in a PDF file 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

Add Layers to a PDF Document in C# and VB.NET

Spire.PDF for .NET provides PdfDocument.Layers.AddLayer() method to add a layer in a PDF document, and you can then draw text, lines, images or shapes on the PDF layer. The detailed steps are as follows.

  • Create a PdfDocument instance.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Add a layer with specified name in the PDF using PdfDocument.Layers.AddLayer(String) method. Or you can also set the visibility of the layer while adding it using PdfDocument.Layers.AddLayer(String, PdfVisibility) method.
  • Create a canvas for the layer using PdfLayer.CreateGraphics() method.
  • Draw text, image or other elements on the canvas.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Graphics.Layer;
using System.Drawing;

namespace AddLayersToPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance and load a sample PDF file
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pdf");

            //Invoke AddLayerWatermark method to add a watermark layer
            AddLayerWatermark(pdf);

            //Invoke AddLayerHeader method to add a header layer
            AddLayerHeader(pdf);

            //Save to file
            pdf.SaveToFile("AddLayers.pdf");
            pdf.Close();
        }

        private static void AddLayerWatermark(PdfDocument doc)
        {
            //Create a layer named "Watermark"
            PdfLayer layer = doc.Layers.AddLayer("Watermark");

            //Create a font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 48), true);

            //Specify the watermark text
            string watermarkText = "CONFIDENTIAL";

            //Get text size
            SizeF fontSize = font.MeasureString(watermarkText);

            //Calculate two offsets
            float offset1 = (float)(fontSize.Width * System.Math.Sqrt(2) / 4);
            float offset2 = (float)(fontSize.Height * System.Math.Sqrt(2) / 4);

            //Get page count
            int pageCount = doc.Pages.Count;

            //Declare two variables
            PdfPageBase page;
            PdfCanvas canvas;

            //Loop through the pages
            for (int i = 0; (i < pageCount); i++)
            {
                page = doc.Pages[i];

                //Create a canvas from layer
                canvas = layer.CreateGraphics(page.Canvas);
                canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2);
                canvas.SetTransparency(0.4f);
                canvas.RotateTransform(-45);

                //Draw sting on the canvas of layer
                canvas.DrawString(watermarkText, font, PdfBrushes.DarkBlue, 0, 0);
            }
        }
        private static void AddLayerHeader(PdfDocument doc)
        {
            // Create a layer named "Header"
            PdfLayer layer = doc.Layers.AddLayer("Header");

            //Get page size
            SizeF size = doc.Pages[0].Size;

            //Specify the initial values of X and y
            float x = 90;
            float y = 40;

            //Get page count
            int pageCount = doc.Pages.Count;

            //Declare two variables
            PdfPageBase page;
            PdfCanvas canvas;

            //Loop through the pages
            for (int i = 0; (i < pageCount); i++)
            {
                //Draw an image on the layer
                PdfImage pdfImage = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\img.jpg");
                float width = pdfImage.Width;
                float height = pdfImage.Height;
                page = doc.Pages[i];
                canvas = layer.CreateGraphics(page.Canvas);
                canvas.DrawImage(pdfImage, x, y, width, height);

                //Draw a line on the layer
                PdfPen pen = new PdfPen(PdfBrushes.DarkGray, 2);
                canvas.DrawLine(pen, x, (y + (height + 5)), (size.Width - x), (y + (height + 2)));
            }
        }
    }
} 

C#/VB.NET: Add, Hide or Delete Layers in PDF

Set Visibility of Layers in a PDF Document in C# and VB.NET

To set the visibility of an existing layer, you'll need to get a specified layer by its index or name using PdfDocument.Layers property, and then show or hide the layer using PdfLayer.Visibility property. The detailed steps are as follows.

  • Create a PdfDocument instance.
  • Load a sample PDF document using PdfDocument.LoadFromFile() method.
  • Set the visibility of a specified layer using PdfDocument.Layers.Visibility property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics.Layer;

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

            //Load a sample PDF document
            pdf.LoadFromFile("AddLayers.pdf");

            //Hide a specified layer by index
            pdf.Layers[0].Visibility = PdfVisibility.Off;

            //Hide a specified layer by name
            //pdf.Layers["Watermark"].Visibility = PdfVisibility.Off;

            //Save the result document
            pdf.SaveToFile("HideLayer.pdf");
        }
    }
}

C#/VB.NET: Add, Hide or Delete Layers in PDF

Delete Layers in a PDF Document in C# and VB.NET

Spire.PDF for .NET also allows you to remove an existing layer by its name using PdfDocument.Layers.RemoveLayer(String) method. But kindly note that the names of PDF layers may not be unique and this method will remove all PDF layers with the same name. The detailed steps are as follows.

  • Create a PdfDocument instance.
  • Load a sample PDF document using PdfDocument.LoadFromFile() method.
  • Delete a specified layer by its name using PdfDocument.Layers.RemoveLayer(String) method.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;

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

            //Load a sample PDF document
            pdf.LoadFromFile("AddLayers.pdf");

            //Remove a layer by name
            pdf.Layers.RemoveLayer(("Watermark"));

            //Save the result document
            pdf.SaveToFile("DeleteLayer.pdf", FileFormat.PDF);
        }
    }
}

C#/VB.NET: Add, Hide or Delete Layers in 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.

C#/VB.NET: Convert PowerPoint to HTML

2022-12-13 09:27:00 Written by Koohji

When you share a PowerPoint presentation online, people have to download it to their computers before they can view it. Sometimes the downloading process can be quite annoying and time-consuming, especially when the file is very large. A good solution to this problem is to convert your presentation to HTML so that people can view it directly online. In this article, we will demonstrate how to programmatically convert PowerPoint presentations to HTML format in C# and VB.NET using Spire.Presentation for .NET.

Install Spire.Presentation for .NET

To begin with, you need to add the DLL files included in the Spire.Presentation 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.Presentation

Convert a PowerPoint Presentation to HTML in C# and VB.NET

In Spire.Presentation for .NET, the Presentation.SaveToFile(String, FileFormat) method is used to convert a PowerPoint presentation to other file formats such as PDF, XPS, and HTML. In the following steps, we will show you how to convert a PowerPoint presentation to HTML using Spire.Presentation for .NET:

  • Initialize an instance of the Presentation class.
  • Load a PowerPoint presentation using Presentation.LoadFromFile(String) method.
  • Save the PowerPoint presentation to HTML format using Presentation.SaveToFile(String, FileFormat) method.
  • C#
  • VB.NET
using Spire.Presentation;

namespace ConvertPowerPointToHtml
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Presentation class
            Presentation ppt = new Presentation();
            //Load a PowerPoint presentation
            ppt.LoadFromFile(@"E:\Program Files\Sample.pptx");

            //Specify the file path of the output HTML file 
            String result = @"E:\Program Files\PowerPointToHtml.html";

            //Save the PowerPoint presentation to HTML format
            ppt.SaveToFile(result, FileFormat.Html);
        }
    }
}

C#/VB.NET: Convert PowerPoint to HTML

Convert a Specific PowerPoint Slide to HTML in C# and VB.NET

In some cases, you may need to convert a specific slide instead of the whole presentation to HTML. Spire.Presentation offers the ISlide.SaveToFile(String, FileFormat) method to convert a PowerPoint slide to HTML. The following are the detailed steps:

  • Initialize an instance of the Presentation class.
  • Load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Get a specific slide in the PowerPoint presentation by its index through Presentation.Slides[int] property.
  • Save the PowerPoint slide to HTML format using ISlide.SaveToFile(String, FileFormat) method.
  • C#
  • VB.NET
using Spire.Presentation;
using System;

namespace ConvertPowerPointSlideToHtml
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Presentation class
            Presentation presentation = new Presentation();
            //Load the PowerPoint presentation
            presentation.LoadFromFile(@"E:\Program Files\Sample.pptx");

            //Get the first slide
            ISlide slide = presentation.Slides[0];

            //Specify the file path of the output HTML file 
            String result = @"E:\Program Files\SlideToHtml.html";

            //Save the first slide to HTML format
            slide.SaveToFile(result, FileFormat.Html);
        }
    }
}

C#/VB.NET: Convert PowerPoint to HTML

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.

C#/VB.NET: Convert CSV to PDF

2022-07-07 08:46:00 Written by Koohji

CSV is one of the commonly used file formats for exchanging tabular data between applications. However, in some circumstances, CSV may not be the most appropriate file format. For instance, if you have a CSV report that needs to be sent to an important customer, the best way to ensure the file appears as-is on the customer's device is to convert it to PDF. This article will demonstrate how to convert CSV to PDF 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

Convert CSV to PDF in C# and VB.NET

The following are the steps to convert a CSV file to PDF:

  • Create an instance of Workbook class.
  • Load the CSV file using Workbook.LoadFromFile(filePath, separator) method.
  • Set the Workbook.ConverterSetting.SheetFitToPage property as true to ensure the worksheet is rendered to one PDF page.
  • Get the first worksheet in the Workbook using Workbook.Worksheets[0] property.
  • Loop through the columns in the worksheet and auto-fit the width of each column using Worksheet.AutoFitColumn() method.
  • Save the worksheet to PDF using Worksheet.SaveToPdf() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace ConvertCsvToPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook wb = new Workbook();
            //Load a CSV file
            wb.LoadFromFile("Sample.csv", ",");

            //Set SheetFitToPage property as true to ensure the worksheet is converted to 1 PDF page
            wb.ConverterSetting.SheetFitToPage = true;

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

            //Loop through the columns in the worksheet
            for (int i = 1; i < sheet.Columns.Length; i++)
            {
                //AutoFit columns
                sheet.AutoFitColumn(i);
            }

            //Save the worksheet to PDF
            sheet.SaveToPdf("toPDF.pdf");
        }
    }
}

C#/VB.NET: Convert CSV 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.

page 184