Thursday, 19 October 2017 03:17

How to add and delete layer in PDF

Developers can use PDF layer to set some content to be visible and others to be invisible in the same PDF file. It makes the PDF Layer widely be used to deal with related contents within the same PDF. With Spire.PDF, we can add several kinds of layers such as line, image, string, ellipse, rectangle and pie to any page of a new or an existing pdf document. At the same time, it also supports us to delete specific layer from a pdf document.

This article will show you how to use Spire.PDF (from version 3.9.360 or above) to add and remove the layers and set the visibility of the layer in C#. If you use the version older than 3.9.360, please refer to the articles of how to add PDF layer, delete layer and toggle the visibility of PDF layer in C#.

Add PDF Layer and toggle the visibility

How to add and delete layer in PDF

//create a new PDF document
PdfDocument doc = new PdfDocument();

//add a new page to the PDF document.
PdfPageBase page = doc.Pages.Add();

//add a layer named "red line"
PdfLayer layer = doc.Layers.AddLayer("red line");
PdfCanvas pcA = layer.CreateGraphics(doc.Pages[0].Canvas);
pcA.DrawLine(new PdfPen(PdfBrushes.Red, 1), new PointF(0, 100), new PointF(100, 100));

//add a layer named "Ellipse" and toggle the visibility as On
layer = doc.Layers.AddLayer("Ellipse", PdfVisibility.On);
PdfCanvas pcB = layer.CreateGraphics(doc.Pages[0].Canvas);
PdfPen pen = new PdfPen(Color.Green, 1f);
PdfBrush brush = new PdfSolidBrush(Color.Green);
pcB.DrawEllipse(pen,brush, 100,120,200,60 );
 
//add a layer named "Blue line" and toggle the visibility as OFF
layer = doc.Layers.AddLayer("Blue line",PdfVisibility.Off);
PdfCanvas pcC = layer.CreateGraphics(doc.Pages[0].Canvas);
pcC.DrawLine(new PdfPen(PdfBrushes.Blue, 1), new PointF(0, 200), new PointF(100, 200));

//save the document to file
doc.SaveToFile("AddLayers.pdf", FileFormat.PDF)

Delete PDF Layer

How to add and delete layer in PDF

//create a new PDF document and load the document from file
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("AddLayers.pdf");

//Delete the layer named “red line”
doc.Layers.RemoveLayer(("red line"));

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

Spire.Presentation supports to convert PowerPoint documents to a variety kinds of file formats such as Image, PDF, SVG and HTML. This article focuses on demonstrating how to convert PowerPoint document to HTML using Spire.Presentation.

Below screenshot shows what the example document looks like:

Convert PowerPoint Document to HTML in C#, VB.NET

Detail steps:

Step 1: Initialize an object of Presentation class.

Presentation ppt = new Presentation();

Step 2: Load the PowerPoint document.

ppt.LoadFromFile("Sample.pptx");

Step 3: Save the document to HTML format.

ppt.SaveToFile(@"E:\Program Files\ToHtml.html", FileFormat.Html);

Screenshot:

Convert PowerPoint Document to HTML in C#, VB.NET

Full code:

[C#]
using Spire.Presentation;

namespace Convert_PowerPoint_To_Html
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an object of Presentation class
            Presentation ppt = new Presentation();
            //Load the PowerPoint document
            ppt.LoadFromFile("Sample.pptx");

            //Save the PowerPoint document to HTML format
            ppt.SaveToFile(@"E:\Program Files\ToHtml.html", FileFormat.Html);
        }
[VB.NET]
Imports Spire.Presentation

Namespace Convert_PowerPoint_To_Html
	Class Program
		Private Shared Sub Main(args As String())
			'Initialize an object of Presentation class
			Dim ppt As New Presentation()
			'Load the PowerPoint document
			ppt.LoadFromFile("Sample.pptx")

			'Save the PowerPoint document to HTML format
			ppt.SaveToFile("E:\Program Files\ToHtml.html", FileFormat.Html)
		End Sub
	End Class
End Namespace
Tuesday, 10 October 2017 08:46

How to Convert CSV to PDF in C#, VB.NET

Spire.XLS supports to load a CSV file and save as an Excel or PDF file. This tutorial shows how to convert CSV to PDF using Spire.XLS with C# and VB.NET.

The sample CSV file looks as follows.

How to Convert CSV to PDF in C#, VB.NET

Step 1: Create a Workbook instance and load a sample CSV file.

Workbook wb = new Workbook();
wb.LoadFromFile("SampleCSVFile.csv", ",",1,1);

Step 2: To render content of a worksheet into a single PDF page, set the SheetFitToPage property as true.

wb.ConverterSetting.SheetFitToPage = true;

Step 3: Autofit a column if the characters in the column exceed column width.

Worksheet sheet = wb.Worksheets[0];
for (int i = 1; i < sheet.Columns.Length; i++)
{
    sheet.AutoFitColumn(i);
}

Step 4: Save the specified worksheet to PDF.

sheet.SaveToPdf("toPDF.pdf");

Output:

How to Convert CSV to PDF in C#, VB.NET

Full Code:

[C#]
Workbook wb = new Workbook();
wb.LoadFromFile("SampleCSVFile.csv", ",",1,1);
wb.ConverterSetting.SheetFitToPage = true;
Worksheet sheet = wb.Worksheets[0];
for (int i = 1; i < sheet.Columns.Length; i++)
{
    sheet.AutoFitColumn(i);
}
sheet.SaveToPdf("toPDF.pdf");
[VB.NET]
Dim wb As New Workbook()
wb.LoadFromFile("SampleCSVFile.csv", ",", 1, 1)
wb.ConverterSetting.SheetFitToPage = True
Dim sheet As Worksheet = wb.Worksheets(0)
For i As Integer = 1 To sheet.Columns.Length - 1
	sheet.AutoFitColumn(i)
Next
sheet.SaveToPdf("toPDF.pdf")
Tuesday, 10 October 2017 07:34

Insert Shapes to Excel Worksheet in C#

Spire.XLS supports to add up to 186 kinds of ready-made shapes, such as triangles, arrows and callouts to Excel worksheet. This article demonstrates how to insert shapes to excel worksheet and fill the shapes with color and picture using Spire.XLS and C#.

Detail steps:

Step 1: Instantiate a Workbook object and get the first worksheet.

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];

Step 2: Add a triangle shape and fill the shape with solid color.

//Add a triangle shape
IPrstGeomShape triangle = sheet.PrstGeomShapes.AddPrstGeomShape(2, 2, 100, 100, PrstGeomShapeType.Triangle);

//Fill the triangle with solid color
triangle.Fill.ForeColor = Color.Yellow;
triangle.Fill.FillType = ShapeFillType.SolidColor;

Step 3: Add a heart shape and fill the shape with gradient color.

//Add a heart shape
IPrstGeomShape heart = sheet.PrstGeomShapes.AddPrstGeomShape(2, 5, 100, 100, PrstGeomShapeType.Heart);

//Fill the heart with gradient color
heart.Fill.ForeColor = Color.Red;
heart.Fill.FillType = ShapeFillType.Gradient;

Step 4: Add an arrow shape with default color.

IPrstGeomShape arrow = sheet.PrstGeomShapes.AddPrstGeomShape(10, 2, 100, 100, PrstGeomShapeType.CurvedRightArrow);

Step 5: Add a cloud shape and fill the shape with custom picture.

//Add a cloud shape
IPrstGeomShape cloud = sheet.PrstGeomShapes.AddPrstGeomShape(10, 5, 100, 100, PrstGeomShapeType.Cloud);

//Fill the cloud with picture
cloud.Fill.CustomPicture(Image.FromFile("Hydrangeas.jpg"), "Hydrangeas.jpg");
cloud.Fill.FillType = ShapeFillType.Picture;

Step 6: Save the file.

workbook.SaveToFile("AddShapes.xlsx", ExcelVersion.Version2013);

Screenshot:

Insert Shapes to Excel Worksheet in C#

Full code:

using System.Drawing;
using Spire.Xls;
using Spire.Xls.Core;

namespace Add_shapes_to_Excel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate a workbook object
            Workbook workbook = new Workbook();
            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Add a triangle shape
            IPrstGeomShape triangle = sheet.PrstGeomShapes.AddPrstGeomShape(2, 2, 100, 100, PrstGeomShapeType.Triangle);
            //Fill the triangle with solid color
            triangle.Fill.ForeColor = Color.Yellow;
            triangle.Fill.FillType = ShapeFillType.SolidColor;

            //Add a heart shape
            IPrstGeomShape heart = sheet.PrstGeomShapes.AddPrstGeomShape(2, 5, 100, 100, PrstGeomShapeType.Heart);
            //Fill the heart with gradient color
            heart.Fill.ForeColor = Color.Red;
            heart.Fill.FillType = ShapeFillType.Gradient;

            //Add an arrow shape with default color
            IPrstGeomShape arrow = sheet.PrstGeomShapes.AddPrstGeomShape(10, 2, 100, 100, PrstGeomShapeType.CurvedRightArrow);

            //Add a cloud shape
            IPrstGeomShape cloud = sheet.PrstGeomShapes.AddPrstGeomShape(10, 5, 100, 100, PrstGeomShapeType.Cloud);
            //Fill the cloud with custom picture
            cloud.Fill.CustomPicture(Image.FromFile("Hydrangeas.jpg"), "Hydrangeas.jpg");
            cloud.Fill.FillType = ShapeFillType.Picture;
            
            //Save the file
            workbook.SaveToFile("AddShapes.xlsx", ExcelVersion.Version2013);
        }
    }
}

Spire.Presentation has a powerful function to print PowerPoint document. From Spire.Presentation v 2.8.59, it supports to use the PrintDocument object to print the presentation slides. This example shows how to print presentation slides using C# via the following print methods:

  • Print PowerPoint document to default printer.
  • Print PowerPoint document to virtual printer.
  • Print some pages from the PowerPoint document and set the document name, number of copies while printing the presentation slides.

Print presentation slides to default printer and print all the pages on the PowerPoint document.

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

PresentationPrintDocument document = new PresentationPrintDocument(ppt);
document.PrintController = new StandardPrintController();

ppt.Print(document);

Print PowerPoint document to virtual printer (Microsoft XPS Document Writer).

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

PresentationPrintDocument document = new PresentationPrintDocument(ppt);
document.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
     
ppt.Print(document);

Print some pages from the PowerPoint document and set the document name, number of copies while printing the presentation slides.

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

PresentationPrintDocument document = new PresentationPrintDocument(ppt);
            
//Set the document name to display while printing the document  
document.DocumentName = "Print PowerPoint";

//Choose to print some pages from the PowerPoint document
document.PrinterSettings.PrintRange = PrintRange.SomePages;
document.PrinterSettings.FromPage = 1;
document.PrinterSettings.ToPage = 2;

//Set the number of copies of the document to print
document.PrinterSettings.Copies = 2;

ppt.Print(document);

To extract text by line from PDF document, use the ExtractText() method without passing any parameter. However, this method also can take a SimpleTextExtractionStrategy object as an argument, which keeps track of the current Y position of each string and inserts a line break into the output if the Y position has changed.

Step 1: Load a sample PDF document to PdfDocument object.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");

Step 2: Get the first page.

PdfPageBase page = doc.Pages[0];

Step 3: Create an instance of SimpleTextExtractionStrategy class.

SimpleTextExtractionStrategy strategy = new SimpleTextExtractionStrategy();

Step 4: Extract text from the given PDF page by SimpleTextExtractionStrategy.

string text = page.ExtractText(strategy)

Step 5: Write text to a TXT file.

FileStream fs = new FileStream("result.txt",FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(text);

Output:

Extract Text from PDF Document using SimpleTextExtractionStrategy

Full Code:

[C#]
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");
PdfPageBase page = doc.Pages[0];

SimpleTextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string text = page.ExtractText(strategy);
FileStream fs = new FileStream("result.txt",FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(text);
sw.Flush();
sw.Close();
[VB.NET]
Dim doc As New PdfDocument()
doc.LoadFromFile("sample.pdf")
Dim page As PdfPageBase = doc.Pages(0)

Dim strategy As New SimpleTextExtractionStrategy()
Dim text As String = page.ExtractText(strategy)
Dim fs As New FileStream("result.txt", FileMode.Create)
Dim sw As New StreamWriter(fs)
sw.Write(text)
sw.Flush()
sw.Close()
Wednesday, 27 September 2017 07:38

How to print PDF document in C#

Spire.PDF has a powerful function to print PDF document. We have done some adjustments by the code to print the PDF files. If you are using the Spire.PDF Version before 3.9.360, please refer to tutorial here for print the PDF files in C#. From Spire.PDF v 3.9.360, you should use the new method offers in this article. This example shows how to print a PDF file using C# via the following print methods:

  • Print PDF to default printer.
  • Print PDF to virtual printer.
  • Print PDF to a specified printer and select some pages in the PDF file to print.

Print PDF to default printer and print all the pages on the PDF document.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");
pdf.Print();      

Print PDF to virtual printer (Microsoft XPS Document Writer).

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");

pdf.PrintSettings.PrinterName = "Microsoft XPS Document Writer";   
pdf.PrintSettings.PrintToFile("PrintToXps.xps");

pdf.Print();    

Print PDF to a specified printer and select some pages in the PDF file to print.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");

//Set the printer 
pdf.PrintSettings.PrinterName = "HP LasterJet P1007";

//Only print the second and fourth page
pdf.PrintSettings.SelectSomePages(new int[] { 2,4 });

//Print the pages from 1 to 15
pdf.PrintSettings.SelectPageRange(1,15);

pdf.Print();
Monday, 25 September 2017 06:43

Hide or Show Excel Comments in C#

At some point, you may want to display or hide comments in an Excel document. This article shows you how to hide or show an existing comment in an excel document using Spire.XLS.

Detail steps:

Step 1: Instantiate a Workbook instance and load the excel document.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Comments.xlsx");

Step 2: Get the first worksheet.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Hide or show the specific comment in the worksheet.

//Hide comment
sheet.Comments[0].IsVisible = false;

//Show comment
//sheet.Comments[0].IsVisible = true;

Step 4: Save the document.

workbook.SaveToFile("HideComment.xlsx", ExcelVersion.Version2013);

Screenshot:

Hide or Show Excel Comments in C#

Full code:

//Instantiate a Workbook instance and load the excel document
Workbook workbook = new Workbook();
workbook.LoadFromFile("Comments.xlsx");

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

//Hide the specific comment in the worksheet
sheet.Comments[0].IsVisible = false;

//Show the specific comment in the worksheet
//sheet.Comments[0].IsVisible = true;

//Save the document
workbook.SaveToFile("HideComment.xlsx", ExcelVersion.Version2013);

You may sometimes want your text to display vertically (letters stacked one on top of the other), horizontally, or rotated facing the right margin or the left margin. This tutorial shows you how to change the text direction using VerticalTextType property in Spire.Presentation.

Step 1: Initialize an instance of Prensentation class.

Presentation ppt = new Presentation();

Step 2: Append a shape with text to the first slide.

IAutoShape textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 100, 400));
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;
textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textboxShape.Fill.SolidColor.Color = Color.OrangeRed;
textboxShape.TextFrame.Text = "You Are Welcome Here";

Step 3: Set the text direction to vertical.

textboxShape.TextFrame.VerticalTextType = VerticalTextType.Vertical;

Step 4: Append another shape with text to the slide.

textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(150, 70, 100, 400));
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;
textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textboxShape.Fill.SolidColor.Color = Color.Orange;
textboxShape.TextFrame.Text = "欢迎光临";

Step 5: For asian characters, you can set the VerticalTextType as EastAsianVertical to aviod rotating text 90 degrees.

textboxShape.TextFrame.VerticalTextType = VerticalTextType.EastAsianVertical;

Step 6: Save the file.

ppt.SaveToFile("output.pptx", FileFormat.Pptx2013);

Output:

How to Change Text Direction in PowerPoint in C#, VB.NET

Full Code:

[C#]
Presentation ppt = new Presentation();

IAutoShape textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 100, 400));
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;
textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textboxShape.Fill.SolidColor.Color = Color.OrangeRed;
textboxShape.TextFrame.Text = "You Are Welcome Here";
textboxShape.TextFrame.VerticalTextType = VerticalTextType.Vertical;

textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(150, 70, 100, 400));
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;
textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textboxShape.Fill.SolidColor.Color = Color.Orange;
textboxShape.TextFrame.Text = "欢迎光临";
textboxShape.TextFrame.VerticalTextType = VerticalTextType.EastAsianVertical;

ppt.SaveToFile("output.pptx", FileFormat.Pptx2013);
[VB.NET]
Dim ppt As New Presentation()

Dim textboxShape As IAutoShape = ppt.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(50, 70, 100, 400))
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent
textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid
textboxShape.Fill.SolidColor.Color = Color.OrangeRed
textboxShape.TextFrame.Text = "You Are Welcome Here"
textboxShape.TextFrame.VerticalTextType = VerticalTextType.Vertical

textboxShape = ppt.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(150, 70, 100, 400))
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent
textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid
textboxShape.Fill.SolidColor.Color = Color.Orange
textboxShape.TextFrame.Text = "欢迎光临"
textboxShape.TextFrame.VerticalTextType = VerticalTextType.EastAsianVertical

ppt.SaveToFile("output.pptx", FileFormat.Pptx2013)
Tuesday, 19 September 2017 08:35

How to toggle the visibility of PDF layer in C#

We have already demonstrated how to use Spire.PDF to add multiple layers to PDF file and delete layer in PDF in C#. We can also toggle the visibility of a PDF layer while creating a new page layer with the help of Spire.PDF. In this section, we're going to demonstrate how to toggle the visibility of layers in new PDF document in C#.

Step 1: Create a new PDF document and add a new page to the PDF document.

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

Step 2: Add a layer named "Blue line" to the PDF page and set the layer invisible.

PdfPageLayer layer = page.PageLayers.Add("Blue line", false);
layer.Graphics.DrawLine(new PdfPen(PdfBrushes.Blue, 1), new PointF(0, 30), new PointF(300, 30));

Step 3: Add a layer named "Ellipse" to the PDF page and set the layer visible.

layer = page.PageLayers.Add("Ellipse", true);
PdfPen pen = new PdfPen(Color.Green, 1f);
PdfBrush brush = new PdfSolidBrush(Color.Green);
layer.Graphics.DrawEllipse(pen, brush, 50, 70, 200, 60);

Step 4: Save the document to file.

pdf.SaveToFile("LayerVisibility.pdf", FileFormat.PDF);

Effective screenshot after toggle the visibility of PDF layer:

How to toggle the visibility of PDF layer in C#

Full codes:

static void Main(string[] args)
{

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

    PdfPageLayer layer = page.PageLayers.Add("Blue line", false);
    layer.Graphics.DrawLine(new PdfPen(PdfBrushes.Blue, 1), new PointF(0, 30), new PointF(300, 30));

    layer = page.PageLayers.Add("Ellipse", true);
    PdfPen pen = new PdfPen(Color.Green, 1f);
    PdfBrush brush = new PdfSolidBrush(Color.Green);
    layer.Graphics.DrawEllipse(pen, brush, 50, 70, 200, 60);
    
    pdf.SaveToFile("LayerVisibility.pdf", FileFormat.PDF);
            
}
Page 10 of 95