Wednesday, 29 June 2022 07:04

Java: Encrypt or Decrypt PDF Files

For PDF documents that contain confidential or sensitive information, you may want to password protect these documents to ensure that only the designated person can access the information. This article will demonstrate how to programmatically encrypt a PDF document and decrypt a password-protected document using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>12.3.9</version>
    </dependency>
</dependencies>

Encrypt a PDF File with Password

There are two kinds of passwords for encrypting a PDF file - open password and permission password. The former is set to open the PDF file, while the latter is set to restrict printing, contents copying, commenting, etc. If a PDF file is secured with both types of passwords, it can be opened with either password.

The PdfDocument.getSecurity().encrypt(java.lang.String openPassword, java.lang.String permissionPassword, java.util.EnumSet<PdfPermissionsFlags> permissions, PdfEncryptionKeySize keySize) method offered by Spire.PDF for Java allows you to set both open password and permission password to encrypt PDF files. The detailed steps are as follows.

  • Create a PdfDocument instance.
  • Load a sample PDF file using PdfDocument.loadFromFile() method.
  • Set open password, permission password, encryption key size and permissions.
  • Encrypt the PDF file using PdfDocument.getSecurity().encrypt(java.lang.String openPassword, java.lang.String permissionPassword, java.util.EnumSet<PdfPermissionsFlags> permissions, PdfEncryptionKeySize keySize) method.
  • Save the result file using PdfDocument.saveToFile () method.
  • Java
import java.util.EnumSet;

import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

public class EncryptPDF {

    public static void main(String[] args) {

        // Input file path
        String input = "data/encryption.pdf";

        // Output file path
        String output = "output/encryption_output.pdf";

        // Create a new PDF document object
        PdfDocument doc = new PdfDocument();

        // Load the PDF document from the input file path
        doc.loadFromFile(input);

        // Create a password-based security policy with open and permission passwords
        PdfSecurityPolicy securityPolicy = new PdfPasswordSecurityPolicy("openPwd", "permissionPwd");

        // Set the encryption algorithm to AES 256-bit
        securityPolicy.setEncryptionAlgorithm(PdfEncryptionAlgorithm.AES_256);

        // Set document privilege to forbid all actions
        securityPolicy.setDocumentPrivilege(PdfDocumentPrivilege.getForbidAll());

        // Allow degraded printing
        securityPolicy.getDocumentPrivilege().setAllowDegradedPrinting(true);

        // Allow modification of annotations
        securityPolicy.getDocumentPrivilege().setAllowModifyAnnotations(true);

        // Allow document assembly
        securityPolicy.getDocumentPrivilege().setAllowAssembly(true);

        // Allow modification of document contents
        securityPolicy.getDocumentPrivilege().setAllowModifyContents(true);

        // Allow filling form fields
        securityPolicy.getDocumentPrivilege().setAllowFillFormFields(true);

        // Allow printing
        securityPolicy.getDocumentPrivilege().setAllowPrint(true);

        // Allow printing
        doc.encrypt(securityPolicy);

        // Save the encrypted document to the output file path
        doc.saveToFile(output, FileFormat.PDF);

        // Dispose of the document resources
        doc.dispose();

    }

}

Java: Encrypt or Decrypt PDF Files

Remove Password to Decrypt a PDF File

When you need to remove the password from a PDF file, you can set the open password and permission password to empty while calling the PdfDocument.getSecurity().encrypt(java.lang.String openPassword, java.lang.String permissionPassword, java.util.EnumSet<PdfPermissionsFlags> permissions, PdfEncryptionKeySize keySize, java.lang.String originalPermissionPassword) method. The detailed steps are as follows.

  • Create a PdfDocument object.
  • Load the encrypted PDF file with password using PdfDocument.loadFromFile(java.lang.String filename, java.lang.String password) method.
  • Decrypt the PDF file by setting the open password and permission password to empty using PdfDocument.getSecurity().encrypt(java.lang.String openPassword, java.lang.String permissionPassword, java.util.EnumSet<PdfPermissionsFlags> permissions, PdfEncryptionKeySize keySize, java.lang.String originalPermissionPassword) method.
  • Save the result file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

public class DecryptPDF {

    public static void main(String[] args) throws Exception {

        // Specify the input and output file paths
        String input = "data/decryption.pdf";
        String output = "output/decryption_result.pdf";

        //load the pdf document.
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile(input, "test");

        //decrypt the document
        doc.decrypt();

        //save the file
        doc.saveToFile(output, FileFormat.PDF);

        // Close the PDF document to release resources
        doc.close();

        // Dispose of the PDF document to free up system resources
        doc.dispose();

    }
}

Java: Encrypt or Decrypt PDF Files

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.

By using Spire.Doc, you can not only retrieve the style names of all paragraphs in a Word document, but also get the paragraphs with a specific style name. This is useful especially when you need to get the text in Title, Heading 1, Subtitle, etc.

Paragraph Style Names in Word Paragraph Style Names in Spire.Doc
Title Title
Heading 1 Heading1
Heading 2 Heading2
Heading 3 Heading3
Heading 4 Heading3
Subtitle Subtitle

Step 1: Load a sample Word file when initializing the Document object.

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

Step 2: Traverse the sections and paragraphs in the document and determine if the paragraph style name is "Heading1", if so, write the paragraph text on screen.

foreach (Section section in doc.Sections)
{
    foreach (Paragraph paragraph in section.Paragraphs)
    {
        if (paragraph.StyleName == "Heading1")
        {
            Console.WriteLine(paragraph.Text);
        }
    }
}

Output:

Get Paragraphs by Style Name in Word in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using System;
namespace GetParagh
{
 class Program
    {

     static void Main(string[] args)
     {
         Document doc = new Document("sample.docx");
         foreach (Section section in doc.Sections)
         {
             foreach (Paragraph paragraph in section.Paragraphs)
             {
                 if (paragraph.StyleName == "Heading1")
                 {
                     Console.WriteLine(paragraph.Text);
                 }
             }
         }
     }

    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace GetParagh
	Class Program

		Private Shared Sub Main(args As String())
			Dim doc As New Document("sample.docx")
			For Each section As Section In doc.Sections
				For Each paragraph As Paragraph In section.Paragraphs
					If paragraph.StyleName = "Heading1" Then
						Console.WriteLine(paragraph.Text)
					End If
				Next
			Next
		End Sub

	End Class
End Namespace

Bookmarks are a great way to specify important locations on a Word document. Spire.Doc supports to access the bookmarks within the document and insert objects such as text, image and table at the bookmark location. This article will show you how to access a specific bookmark and replace the current bookmark content with a table.

Step 1: Load a template Word document.

Document doc = new Document();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\employee.docx");

Step 2: Create a Table object.

Table table = new Table(doc,true);

Step 3: Fill the table with sample data.

DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("job", typeof(string));
dt.Columns.Add("email", typeof(string));
dt.Columns.Add("salary", typeof(string));
dt.Rows.Add(new string[] { "Emp_ID", "Name", "Job", "E-mail", "Salary" });
dt.Rows.Add(new string[] { "0241","Andrews", "Engineer", "andrews@outlook.com" ,"3.8K"});
dt.Rows.Add(new string[] { "0242","White", "Manager", "white@outlook.com","4.2K" });
dt.Rows.Add(new string[] { "0243","Martin", "Secretary", "martin@gmail.com", "3.5K" });
table.ResetCells(dt.Rows.Count, dt.Columns.Count);          
for (int i = 0; i < dt.Rows.Count; i++)
{
    for (int j = 0; j < dt.Columns.Count; j++)
    {
        table.Rows[i].Cells[j].AddParagraph().AppendText(dt.Rows[i][j].ToString());
    }
}

Step 4: Get the specific bookmark by its name.

BookmarksNavigator navigator = new BookmarksNavigator(doc);
navigator.MoveToBookmark("bookmark_employee");

Step 5: Create a TextBodyPart instance and add the table to it.

TextBodyPart part = new TextBodyPart(doc);
part.BodyItems.Add(table);

Step 6: Replace the current bookmark content with the TextBodyPart object.

navigator.ReplaceBookmarkContent(part);

Step 7: Save the file.

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

Result:

Replace Bookmark with a Table in Word Documents in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using System.Data;
namespace ReplaceBookmark
{
    class Program
    {

        static void Main(string[] args)
        {

            Document doc = new Document();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\employee.docx");

            Table table = new Table(doc, true);
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(string));
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("job", typeof(string));
            dt.Columns.Add("email", typeof(string));
            dt.Columns.Add("salary", typeof(string));
            dt.Rows.Add(new string[] { "Emp_ID", "Name", "Job", "E-mail", "Salary" });
            dt.Rows.Add(new string[] { "0241", "Andrews", "Engineer", "andrews@outlook.com", "3.8K" });
            dt.Rows.Add(new string[] { "0242", "White", "Manager", "white@outlook.com", "4.2K" });
            dt.Rows.Add(new string[] { "0243", "Martin", "Secretary", "martin@gmail.com", "3.5K" });
            table.ResetCells(dt.Rows.Count, dt.Columns.Count);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    table.Rows[i].Cells[j].AddParagraph().AppendText(dt.Rows[i][j].ToString());
                }
            }

            BookmarksNavigator navigator = new BookmarksNavigator(doc);
            navigator.MoveToBookmark("bookmark_employee");
            TextBodyPart part = new TextBodyPart(doc);
            part.BodyItems.Add(table);
            navigator.ReplaceBookmarkContent(part);
            doc.SaveToFile("output.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("output.docx"); 

        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Data
Namespace ReplaceBookmark
	Class Program

		Private Shared Sub Main(args As String())

			Dim doc As New Document()
			doc.LoadFromFile("C:\Users\Administrator\Desktop\employee.docx")

			Dim table As New Table(doc, True)
			Dim dt As New DataTable()
			dt.Columns.Add("id", GetType(String))
			dt.Columns.Add("name", GetType(String))
			dt.Columns.Add("job", GetType(String))
			dt.Columns.Add("email", GetType(String))
			dt.Columns.Add("salary", GetType(String))
			dt.Rows.Add(New String() {"Emp_ID", "Name", "Job", "E-mail", "Salary"})
			dt.Rows.Add(New String() {"0241", "Andrews", "Engineer", "andrews@outlook.com", "3.8K"})
			dt.Rows.Add(New String() {"0242", "White", "Manager", "white@outlook.com", "4.2K"})
			dt.Rows.Add(New String() {"0243", "Martin", "Secretary", "martin@gmail.com", "3.5K"})
			table.ResetCells(dt.Rows.Count, dt.Columns.Count)
			For i As Integer = 0 To dt.Rows.Count - 1
				For j As Integer = 0 To dt.Columns.Count - 1
					table.Rows(i).Cells(j).AddParagraph().AppendText(dt.Rows(i)(j).ToString())
				Next
			Next

			Dim navigator As New BookmarksNavigator(doc)
			navigator.MoveToBookmark("bookmark_employee")
			Dim part As New TextBodyPart(doc)
			part.BodyItems.Add(table)
			navigator.ReplaceBookmarkContent(part)
			doc.SaveToFile("output.docx", FileFormat.Docx2013)
			System.Diagnostics.Process.Start("output.docx")

		End Sub
	End Class
End Namespace
Tuesday, 16 January 2018 08:10

Detect if a PDF file is PDF/A in C#

Spire.PDF provides developers two methods to detect if a PDF file is PDF/A. The one is to use PdfDocument.Conformance property, the other is to use PdfDocument.XmpMetaData property. The following examples demonstrate how we can detect if a PDF file is PDF/A using these two methods.

Below is the screenshot of the sample file we used for demonstration:

Detect if a PDF file is PDF/A in C#

Using PdfDocument.Conformance

using Spire.Pdf;
using System;

namespace Detect
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize a PdfDocument object 
            PdfDocument pdf = new PdfDocument();
            //Load the PDF file
            pdf.LoadFromFile("Example.pdf");

            //Get the conformance level of the PDF file            
            PdfConformanceLevel conformance = pdf.Conformance;
            Console.WriteLine("This PDF file is " + conformance.ToString());
        }
    }
}

Output:

Detect if a PDF file is PDF/A in C#

A cross-reference refers to related information elsewhere in the same document. You can create cross-references to any existing items such as headings, footnotes, bookmarks, captions, and numbered paragraphs. This article will show you how to create a cross-reference to bookmark using Spire.Doc with C# and VB.NET.

Step 1: Create a Document instance.

Document doc = new Document();
Section section = doc.AddSection();

Step 2: Insert a bookmark.

Paragraph paragraph = section.AddParagraph();
paragraph.AppendBookmarkStart("MyBookmark");
paragraph.AppendText("Text inside a bookmark");
paragraph.AppendBookmarkEnd("MyBookmark");

Step 3: Create a cross-reference field, and link it to the bookmark through bookmark name.

Field field = new Field(doc);
field.Type = FieldType.FieldRef;
field.Code = @"REF MyBookmark \p \h";

Step 4: Add a paragraph, and insert the field to the paragraph.

paragraph = section.AddParagraph();
paragraph.AppendText("For more information, see ");
paragraph.ChildObjects.Add(field);

Step 5: Insert a FieldSeparator object to the paragraph, which works as separator in a field.

FieldMark fieldSeparator= new FieldMark(doc, FieldMarkType.FieldSeparator);
paragraph.ChildObjects.Add(fieldSeparator);

Step 6: Set the display text of the cross-reference field.

TextRange tr = new TextRange(doc);
tr.Text = "above";
paragraph.ChildObjects.Add(tr);

Step 7: Insert a FieldEnd object to the paragraph, which is used to mark the end of a field.

FieldMark fieldEnd = new FieldMark(doc, FieldMarkType.FieldEnd);
paragraph.ChildObjects.Add(fieldEnd);

Step 8: Save to file.

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

Output:

The cross-reference appears as a link that takes the reader to the referenced item.

Create a Cross-Reference to Bookmark in Word in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace CreatCR
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            Section section = doc.AddSection();
            //create a bookmark
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendBookmarkStart("MyBookmark");
            paragraph.AppendText("Text inside a bookmark");
            paragraph.AppendBookmarkEnd("MyBookmark");
            //insert line breaks
            for (int i = 0; i < 4; i++)
            {
                paragraph.AppendBreak(BreakType.LineBreak);
            }
            //create a cross-reference field, and link it to bookmark                    
            Field field = new Field(doc);
            field.Type = FieldType.FieldRef;
            field.Code = @"REF MyBookmark \p \h";
            //insert field to paragraph
            paragraph = section.AddParagraph();
            paragraph.AppendText("For more information, see ");
            paragraph.ChildObjects.Add(field);
            //insert FieldSeparator object
            FieldMark fieldSeparator = new FieldMark(doc, FieldMarkType.FieldSeparator);
            paragraph.ChildObjects.Add(fieldSeparator);
            //set display text of the field
            TextRange tr = new TextRange(doc);
            tr.Text = "above";
            paragraph.ChildObjects.Add(tr);
            //insert FieldEnd object to mark the end of the field
            FieldMark fieldEnd = new FieldMark(doc, FieldMarkType.FieldEnd);
            paragraph.ChildObjects.Add(fieldEnd);
            //save file
            doc.SaveToFile("output.docx", FileFormat.Docx2013);

        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace CreatCR
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New Document()
			Dim section As Section = doc.AddSection()
			'create a bookmark
			Dim paragraph As Paragraph = section.AddParagraph()
			paragraph.AppendBookmarkStart("MyBookmark")
			paragraph.AppendText("Text inside a bookmark")
			paragraph.AppendBookmarkEnd("MyBookmark")
			'insert line breaks
			For i As Integer = 0 To 3
				paragraph.AppendBreak(BreakType.LineBreak)
			Next
			'create a cross-reference field, and link it to bookmark                    
			Dim field As New Field(doc)
			field.Type = FieldType.FieldRef
			field.Code = "REF MyBookmark \p \h"
			'insert field to paragraph
			paragraph = section.AddParagraph()
			paragraph.AppendText("For more information, see ")
			paragraph.ChildObjects.Add(field)
			'insert FieldSeparator object
			Dim fieldSeparator As New FieldMark(doc, FieldMarkType.FieldSeparator)
			paragraph.ChildObjects.Add(fieldSeparator)
			'set display text of the field
			Dim tr As New TextRange(doc)
			tr.Text = "above"
			paragraph.ChildObjects.Add(tr)
			'insert FieldEnd object to mark the end of the field
			Dim fieldEnd As New FieldMark(doc, FieldMarkType.FieldEnd)
			paragraph.ChildObjects.Add(fieldEnd)
			'save file
			doc.SaveToFile("output.docx", FileFormat.Docx2013)

		End Sub
	End Class
End Namespace

Using Excel conditional formatting, we can quickly find and highlight the duplicate and unique values in a selected cell range. In this article, we’re going to show you how to programmatically highlight duplicate and unique values with different colors using Spire.XLS and conditional formatting.

Detail steps:

Step 1: Initialize an object of Workbook class and Load the Excel file.

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

Step 2: Get the first worksheet.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Use conditional formatting to highlight duplicate values in range "A2:A10" with IndianRed color.

XlsConditionalFormats xcfs1 = sheet.ConditionalFormats.Add();
xcfs1.AddRange(sheet.Range["A2:A10"]);

IConditionalFormat format1 = xcfs1.AddCondition();
format1.FormatType = ConditionalFormatType.DuplicateValues;
format1.BackColor = Color.IndianRed;

Step 4: Use conditional formatting to highlight unique values in range "A2:A10" with Yellow color.

IConditionalFormat format2 = xcfs1.AddCondition();
format2.FormatType = ConditionalFormatType.UniqueValues;
format2.BackColor = Color.Yellow;"

Step 5: Save the file.

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

Screenshot:

Highlight Duplicate and Unique Values in Excel Using C#

Full code:

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

namespace HighlightDuplicateandUniqueValues
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the Excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Input.xlsx");

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

            //Use conditional formatting to highlight duplicate values in range "A2:A10" with IndianRed color
            XlsConditionalFormats xcfs1 = sheet.ConditionalFormats.Add();
            xcfs1.AddRange(sheet.Range["A2:A10"]);

            IConditionalFormat format1 = xcfs1.AddCondition();
            format1.FormatType = ConditionalFormatType.DuplicateValues;
            format1.BackColor = Color.IndianRed;

            //Use conditional formatting to highlight unique values in range "A2:A10" with Yellow color
            IConditionalFormat format2 = xcfs1.AddCondition();
            format2.FormatType = ConditionalFormatType.UniqueValues;
            format2.BackColor = Color.Yellow;

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

        }
    }
}

We've have demonstrated how to convert PDF page to SVG file format in the previous post. This guidance shows you how we can specify the width and height of output file using the latest version of Spire.PDF with C# and VB.NET.

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

PdfDocument document = new PdfDocument();
document.LoadFromFile("pdf-sample.pdf");

Step 2: Specify the output file size through ConvertOptions.SetPdfToSvgOptions() method.

PdfToSvgConverter converter = new PdfToSvgConverter(inputFile);
    converter.SvgOptions.ScaleX = (float)0.5;
    converter.SvgOptions.ScaleY = (float)0.5;
    converter.Convert(outputFile);

Step 3: Save PDF to SVG file format.

document.SaveToFile("result.svg", FileFormat.SVG);

Output:

Convert PDF to SVG with Custom Width and Height in C#, VB.NET

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Conversion;

namespace ConvertPDFtoSVG
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument document = new PdfDocument();
            PdfToSvgConverter converter = new PdfToSvgConverter(inputFile);
            converter.SvgOptions.ScaleX = (float)0.5;
            converter.SvgOptions.ScaleY = (float)0.5;
            converter.Convert(outputFile);
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Conversion

Namespace ConvertPDFtoSVG
    Class Program
        Private Shared Sub Main(args As String())
            Dim document As New PdfDocument()
            document.LoadFromFile("pdf-sample.pdf")
            ' Specify the width And height of output SVG file
            Dim Converter As New PdfToSvgConverter(inputFile)
            Converter.SvgOptions.ScaleX = CSng(0.5)
            Converter.SvgOptions.ScaleY = CSng(0.5)
            Converter.Convert(outputFile)
            document.SaveToFile("result.svg", FileFormat.SVG)
        End Sub
    End Class
End Namespace
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.

PdfLayer layer = pdf.Layers.AddLayer(""Green line"", PdfVisibility.Off);
PdfPen pen = new PdfPen(Color.Green, 1f);
PdfCanvas pcA = layer.CreateGraphics(page.Canvas);
pcA.DrawLine(pen, 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 = pdf.Layers.AddLayer(""Ellipse"", PdfVisibility.On);
 PdfPen pen2 = new PdfPen(Color.Green, 1f);
 PdfBrush brush2 = new PdfSolidBrush(Color.Green);
 PdfCanvas pcB = layer.CreateGraphics(page.Canvas);
 pcB.DrawEllipse(pen2, brush2, 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:

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


namespace LayerVisibility
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

            PdfLayer layer = pdf.Layers.AddLayer("Green line", PdfVisibility.Off);
            PdfPen pen = new PdfPen(Color.Green, 1f);
            PdfCanvas pcA = layer.CreateGraphics(page.Canvas);
            pcA.DrawLine(pen,new PointF(0, 30), new PointF(300, 30));

            layer = pdf.Layers.AddLayer("Ellipse", PdfVisibility.On);
            PdfPen pen2 = new PdfPen(Color.Green, 1f);
            PdfBrush brush2 = new PdfSolidBrush(Color.Green);
            PdfCanvas pcB = layer.CreateGraphics(page.Canvas);
            pcB.DrawEllipse(pen2, brush2, 50, 70, 200, 60);


            pdf.SaveToFile("LayerVisibility.pdf", FileFormat.PDF);
        }
    }
}
Thursday, 07 September 2017 03:17

Remove conditional format from Excel in C#

With the help of Spire.XLS, we can set the conditional format the Excel cell in C# and VB.NET. We can also use Spire.XLS to remove the conditional format from a specific cell or the entire Excel worksheet. This article will demonstrate how to remove conditional format from Excel in C#.

Firstly, view the original Excel worksheet with conditional formats:

Remove conditional format from Excel in C#

Step 1: Create an instance of Excel workbook and load the document from file.

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

Step 2: Get the first worksheet from the workbook.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Remove the first conditional format.

sheet.ConditionalFormats.RemoveAt(0);

Step 4: Remove all the conditional formats from the whole Excel worksheet.

for (int i = sheet.ConditionalFormats.Count-1; i >= 0; i--)
{
sheet.ConditionalFormats.RemoveAt(i);
}

Step 5: Save the document to file.

workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);

Remove the conditional format from a special Excel range B2:

Remove conditional format from Excel in C#

Remove all the conditional formats from the entire Excel worksheet:

Remove conditional format from Excel in C#

Full codes of how to remove the conditional formats from Excel worksheet:

using Spire.Xls;

namespace RemoveConditionalFormat
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            Worksheet sheet = workbook.Worksheets[0];

            // Remove the first conditional format
            //sheet.ConditionalFormats.RemoveAt(0);

            // Remove all conditional formats
            for (int i = sheet.ConditionalFormats.Count-1; i >= 0; i--)
            {
                sheet.ConditionalFormats.RemoveAt(i);
            }

            workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);
        }
    }
}
Thursday, 04 July 2024 06:30

C#: Convert PDF to HTML

PDF documents have been a popular choice for sharing information due to their cross-platform compatibility and ability to preserve the original layout and formatting. However, as the web continues to evolve, there is an increasing demand for content that can be easily integrated into websites and other online platforms. In this context, converting PDF to HTML format has become highly valuable. By converting PDF files to more flexible and accessible HTML, users gain the ability to better utilize, share, and reuse PDF-based information within the web environment. In this article, we will demonstrate how to convert PDF files to HTML format in C# 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

Convert PDF to HTML in C#

To convert a PDF document to HTML format, you can use the PdfDocument.SaveToFile(string fileName, FileFormat.HTML) method provided by Spire.PDF for .NET. The detailed steps are as follows.

  • Create an instance of the PdfDocument class.
  • Load a PDF document using the PdfDocument.LoadFromFile(string fileName) method.
  • Save the PDF document to HTML format using the PdfDocument.SaveToFile(string fileName, FileFormat.HTML) method.
  • C#
using Spire.Pdf;

namespace ConvertPdfToHtml
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create an instance of the PdfDocument class
            PdfDocument doc = new PdfDocument();

            // Load a PDF document
            doc.LoadFromFile("Sample.pdf");

            // Save the PDF document to HTML format
            doc.SaveToFile("PdfToHtml.html", FileFormat.HTML);
            doc.Close();
        }
    }
}

C#: Convert PDF to HTML

Set Conversion Options When Converting PDF to HTML in C#

The PdfConvertOptions.SetPdfToHtmlOptions() method allows you to customize the conversion options when transforming PDF files to HTML. This method takes several parameters that you can use to configure the conversion process, such as:

  • useEmbeddedSvg (bool): Indicates whether to embed SVG in the resulting HTML file.
  • useEmbeddedImg (bool): Indicates whether to embed images in the resulting HTML file. This option is applicable only when useEmbeddedSvg is set to false.
  • maxPageOneFile (int): Specifies the maximum number of pages to be included per HTML file. This option is applicable only when useEmbeddedSvg is set to false.
  • useHighQualityEmbeddedSvg (bool): Indicates whether to use high-quality embedded SVG in the resulting HTML file. This option is applicable when useEmbeddedSvg is set to true.

The following steps explain how to customize the conversion options when transforming a PDF to HTML using Spire.PDF for .NET.

  • Create an instance of the PdfDocument class.
  • Load a PDF document using the PdfDocument.LoadFromFile(string fileName) method.
  • Get the PdfConvertOptions object using the PdfDocument.ConvertOptions property.
  • Set the PDF to HTML conversion options using PdfConvertOptions.SetPdfToHtmlOptions(bool useEmbeddedSvg, bool useEmbeddedImg, int maxPageOneFile, bool useHighQualityEmbeddedSvg) method.
  • Save the PDF document to HTML format using PdfDocument.SaveToFile(string fileName, FileFormat.HTML) method.
  • C#
using Spire.Pdf;

namespace ConvertPdfToHtmlWithCustomOptions
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create an instance of the PdfDocument class
            PdfDocument doc = new PdfDocument();

            // Load a PDF document
            doc.LoadFromFile("Sample.pdf");

            // Set the conversion options to embed images in the resulting HTML and limit one page per HTML file
            PdfConvertOptions pdfToHtmlOptions = doc.ConvertOptions;
            pdfToHtmlOptions.SetPdfToHtmlOptions(false, true, 1);

            // Save the PDF document to HTML format
            doc.SaveToFile("PdfToHtmlWithCustomOptions.html", FileFormat.HTML);
            doc.Close();
        }
    }
}

Convert PDF to HTML Stream in C#

Instead of saving a PDF document to an HTML file, you can save it to an HTML stream by using the PdfDocument.SaveToStream(Stream stream, FileFormat.HTML) method. The detailed steps are as follows.

  • Create an instance of the PdfDocument class.
  • Load a PDF document using the PdfDocument.LoadFromFile(string fileName) method.
  • Create an instance of the MemoryStream class.
  • Save the PDF document to an HTML stream using the PdfDocument.SaveToStream(Stream stream, FileFormat.HTML) method.
  • C#
using Spire.Pdf;
using System.IO;

namespace ConvertPdfToHtmlStream
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create an instance of the PdfDocument class
            PdfDocument doc = new PdfDocument();

            // Load a PDF document
            doc.LoadFromFile("Sample.pdf");

            // Save the PDF document to HTML stream
            using (var fileStream = new MemoryStream())
            {
                doc.SaveToStream(fileStream, FileFormat.HTML);

                // You can now do something with the HTML stream, such as Write it to a file
                using (var outputFile = new FileStream("PdfToHtmlStream.html", FileMode.Create))
                {
                    fileStream.Seek(0, SeekOrigin.Begin);
                    fileStream.CopyTo(outputFile);
                }
            }

            doc.Close();
        }
    }
}

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.