Knowledgebase (2344)
Children categories
In some cases, checking the size, orientation, and rotation of PDF pages can be part of the quality control process. For example, before publishing or distributing a document, you might need to verify this information to ensure that all pages in the document are correctly presented. In this article, you will learn how to get PDF page size, orientation and rotation angle 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
Get PDF Page Size in C#
Spire.PDF for .NET offers the PdfPageBase.Size.Width and PdfPageBase.Size.Height properties to get the width and height of a PDF page in points. If you want to convert the default unit of measure to other units, you can use the PdfUnitConvertor class. The following are the detailed steps.
- Create a PdfDocument instance.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Get a specified page using PdfDocument.Pages[] property.
- Get the width and height of the PDF page using PdfPageBase.Size.Width and PdfPageBase.Size.Height properties.
- Create a PdfUnitConvertor instance, and then convert the size units from points to other units of measure using PdfUnitConvertor.ConvertUnits() method.
- Add the page size information to a StringBuilder instance, and then save the result to a TXT file.
- C#
using System.Text;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace GetPDFPageSize
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a PDF file from disk
pdf.LoadFromFile("SamplePDF.pdf");
//Get the first page
PdfPageBase page = pdf.Pages[0];
//Get the width and height of the page in "point"
float pointWidth = page.Size.Width;
float pointHeight = page.Size.Height;
//Create PdfUnitConvertor to convert the unit
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
//Convert size units from points to pixels
float pixelWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);
float pixelHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);
//Convert size units from points to inches
float inchWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch);
float inchHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch);
//Convert size units from points to centimeters
float centimeterWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);
float centimeterHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);
//Create a StringBuilder instance
StringBuilder content = new StringBuilder();
//Add the page size information to the StringBuilder instance
content.AppendLine("The page size of the file in points is (width: " + pointWidth + "pt, height: " + pointHeight + "pt).");
content.AppendLine("The page size of the file in pixels is (width: " + pixelWidth + "pixel, height: " + pixelHeight + "pixel).");
content.AppendLine("The page size of the file in inches is (width: " + inchWidth + "inch, height: " + inchHeight + "inch).");
content.AppendLine("The page size of the file in centimeters is (width: " + centimeterWidth + "cm, height: " + centimeterHeight + "cm).");
//Save to a txt file
File.WriteAllText("GetPageSize.txt", content.ToString());
}
}
}

Detect PDF Page Orientation in C#
To detect the orientation of a PDF page, you can compare the width and height of the page. If the page width is greater than the height, then the page orientation is landscape, otherwise it is portrait. The following are the detailed steps.
- Create a PdfDocument instance.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Get a specified page using PdfDocument.Pages[] property.
- Get the width and height of the PDF page using PdfPageBase.Size.Width and PdfPageBase.Size.Height properties.
- Compare the values of page width and height to detect the page orientation.
- Output the result using Console.WriteLine() method.
- C#
using Spire.Pdf;
namespace GetPDFPageOrientation
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a PDF file from disk
pdf.LoadFromFile("SamplePDF.pdf");
//Get the first page
PdfPageBase page = pdf.Pages[0];
//Get the width and height of the page
float width = page.Size.Width;
float height = page.Size.Height;
//Compare the values of page width and height
if (width > height)
{
Console.WriteLine("The page orientation is Landscape.");
}
else
{
Console.WriteLine("The page orientation is Portrait.");
}
}
}
}

Detect PDF Page Rotation Angle in C#
The rotation angle of a PDF page can be obtained through the PdfPageBase.Rotation property. The following are the detailed steps.
- Create a PdfDocument instance.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Get a specified page using PdfDocument.Pages[] property.
- Get the rotation angle of the page using PdfPageBase.Rotation property, and then convert it to text string.
- Output the result using Console.WriteLine() method.
- C#
using Spire.Pdf;
namespace GetPDFPageRotationAngle
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a PDF file from disk
pdf.LoadFromFile("E:\\PythonPDF\\Sample.pdf");
//Get the first page
PdfPageBase page = pdf.Pages[0];
//Get the rotation angle of the current page
PdfPageRotateAngle rotationAngle = page.Rotation;
string rotation = rotationAngle.ToString();
//Output the page rotation angle information
Console.WriteLine("The rotation angle of the current page is: " + rotation);
}
}
}

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.
Column charts, also known as bar charts, provide a visual comparison of data points across different categories. Whether you're summarizing sales figures, tracking project milestones, or visualizing survey results, column charts in Word provide a powerful way to translate complex data into an accessible, engaging format within your written materials.
In this article, you will learn how to create a clustered column chart and a stacked column chart in a Word document using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.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.doc</artifactId>
<version>14.6.0</version>
</dependency>
</dependencies>
Create a Clustered Column Chart in Word in Java
To insert a chart into a Microsoft Word document, you can use the Paragraph.appendChart(ChartType chartType, float width, float height) method. The ChartType enumeration provides various pre-defined chart types available in MS Word. To create a clustered column chart, you would specify the chart type as Column.
The steps to add a clustered column chart to a Word document using Java are as follows:
- Create a Document object.
- Add a section and a paragraph to the document.
- Add a clustered column chart to the paragraph using Paragraph.appendChart() method.
- Add series to the chart using Chart.getSeries().add() method.
- Set the chart title using Chart.getTilte().setText() method.
- Set other attributes of the chart using the methods available in the Chart object.
- Save the document to a different Word file.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.ShapeObject;
import com.spire.doc.fields.shapes.charts.*;
public class CreateClusteredColumnChart {
public static void main(String[] args) {
// Create a Document object
Document document = new Document();
// Add a section
Section section = document.addSection();
// Add a paragraph
Paragraph paragraph = section.addParagraph();
// Add a column chart
ShapeObject shape = paragraph.appendChart(ChartType.Column, 490, 250);
// Get the chart
Chart chart = shape.getChart();
// Clear the default data
chart.getSeries().clear();
// Add a series including series name, category names, and series values to chart
chart.getSeries().add("June",
new String[] { "Cuba", "Mexico", "France"},
new double[] { 5000, 8000, 9000 });
// Add two more series
chart.getSeries().add("July",
new String[] { "Cuba", "Mexico", "France"},
new double[] { 4000, 5000, 7000 });
chart.getSeries().add("August",
new String[] { "Cuba", "Mexico", "France"},
new double[] { 3500, 7000, 5000 });
// Set the chart title
chart.getTitle().setText("Sales by Country");
// Set the number format of the Y-axis
chart.getAxisY().getNumberFormat().setFormatCode("#,##0");
// Set the legend position
chart.getLegend().setPosition(LegendPosition.Bottom);
// Save to file
document.saveToFile("ClusteredColumnChart.docx", FileFormat.Docx_2019);
// Dispose resources
document.dispose();
}
}

Create a Stacked Column Chart in Word in Java
Creating a stacked column chart in a Word document follows a similar process to the clustered column chart. The only difference is specifying the chart type as Column_Stacked instead of Column.
The detailed steps to add a stacked column chart are:
- Create a Document object.
- Add a section and a paragraph to the document.
- Add a stacked column chart to the paragraph using Paragraph.appendChart() method.
- Add series to the chart using Chart.getSeries().add() method.
- Set the chart title using Chart.getTilte().setText() method.
- Set other attributes of the chart using the methods available in the Chart object.
- Save the document to a different Word file.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.ShapeObject;
import com.spire.doc.fields.shapes.charts.Chart;
import com.spire.doc.fields.shapes.charts.ChartType;
import com.spire.doc.fields.shapes.charts.LegendPosition;
public class CreateStackedColumnChart {
public static void main(String[] args) {
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.addSection();
//Add a paragraph
Paragraph paragraph = section.addParagraph();
//Add a stacked column chart
ShapeObject shape = paragraph.appendChart(ChartType.Column_Stacked, 490, 250);
//Get the chart
Chart chart = shape.getChart();
//Clear the default data
chart.getSeries().clear();
//Add a series including series name, category names, and series values to chart
chart.getSeries().add("Store A",
new String[] { "Diet Coke", "Mountain Dew", "Diet Pesi", "Cherry Coke" },
new double[] { 2500, 4600, 2800, 5100 });
//Add another series
chart.getSeries().add("Store B",
new String[] { "Diet Coke", "Mountain Dew", "Diet Pesi", "Cherry Coke" },
new double[] { 4100, 3200, 3800, 4000 });
//Set the chart title
chart.getTitle().setText("Store Wise Soda Soft Drink Sales");
//Set the number format of the Y-axis
chart.getAxisY().getNumberFormat().setFormatCode("#,##0");
//Set the legend position
chart.getLegend().setPosition(LegendPosition.Bottom);
//Save to file
document.saveToFile("StackedColumnChart.docx", FileFormat.Docx_2019);
// Dispose resources
document.dispose();
}
}

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.
Digital signatures are vital for maintaining the authenticity and integrity of PDF documents. They provide a reliable way to verify the signer's identity and ensure that the document's content has not been tampered with since the signature was applied. By using digital signatures, you can enhance the security and trustworthiness of your documents. In this article, we will explore how to add and remove digital signatures in PDF files in Python using Spire.PDF for Python.
- Add a Digital Signature to PDF in Python
- Add an Invisible Digital Signature to PDF in Python
- Remove Digital Signature from PDF in Python
Install Spire.PDF for Python
This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.PDF
If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows
Add a Digital Signature to PDF in Python
You can use the PdfOrdinarySignatureMaker.MakeSignature(sigFieldName: str, page: PdfPageBase, x: float, y: float, width: float, height: float, signatureAppearance: IPdfSignatureAppearance) method to add a visible digital signature with a custom appearance to a specific page of a PDF document. The detailed steps are as follows.
- Create a PdfDocument instance.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Create a PdfOrdinarySignatureMaker instance and pass the PdfDocument object, certificate (.pfx) file path and certificate password to the class instructor as parameters.
- Set signature details, such as the signer’s name, contact information, location, and signature reason, using the properties of the PdfOrdinarySignatureMaker class.
- Create a PdfSignatureAppearance instance for the signature, and then customize the labels for the signature and set the signature image.
- Get a specific page in the PDF document using PdfDocument.Pages[] property.
- Call the PdfOrdinarySignatureMaker.MakeSignature(sigFieldName: str, page: PdfPageBase, x: float, y: float, width: float, height: float, signatureAppearance: IPdfSignatureAppearance) method to add the digital signature to a specific location of the page.
- Save the result document using the PdfDocument.SaveToFile() method.
- Python
from spire.pdf.common import *
from spire.pdf import *
# Create a PdfDocument instance
doc = PdfDocument()
# Load a PDF file
doc.LoadFromFile("Sample.pdf")
# Create a signature maker
signatureMaker = PdfOrdinarySignatureMaker(doc, "gary.pfx", "e-iceblue")
# Configure the signature properties like the signer's name, contact information, location and signature reason
signature = signatureMaker.Signature
signature.Name = "Gary"
signature.ContactInfo = "+86 12345678"
signature.Location = "China"
signature.Reason = "I am the author."
# Create a custom signature appearance
appearance = PdfSignatureAppearance(signature)
# Set label for the signer's name
appearance.NameLabel = "Signer: "
# Set label for the contact information
appearance.ContactInfoLabel = "Phone: "
# Set label for the location
appearance.LocationLabel = "Location: "
# Set label for the signature reason
appearance.ReasonLabel = "Reason: "
# Set signature image
appearance.SignatureImage = PdfImage.FromFile("SigImg.png")
# Set the graphic render/display mode for the signature
appearance.GraphicMode = GraphicMode.SignImageAndSignDetail
# Set the layout for the signature image
appearance.SignImageLayout = SignImageLayout.none
# Get the first page
page = doc.Pages.get_Item(0)
# Add the signature to a specified location of the page
signatureMaker.MakeSignature("Signature by Gary", page, 90.0, 600.0, 260.0, 100.0, appearance)
# Save the signed document
doc.SaveToFile("Signed.pdf")
doc.Close()

Add an Invisible Digital Signature to PDF in Python
An invisible signature in a PDF is a type of digital signature that provides all the security and authentication benefits of a standard digital signature but does not appear visibly on the document itself. Using the PdfOrdinarySignatureMaker.MakeSignature(sigFieldName: str) method of Spire.PDF for Python, you can add an invisible digital signature to a PDF document. The detailed steps are as follows.
- Create a PdfDocument instance.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Create a PdfOrdinarySignatureMaker instance and pass the PdfDocument object, certificate (.pfx) file path and password to the class instructor as parameters.
- Add an invisible digital signature to a PDF document using the PdfOrdinarySignatureMaker.MakeSignature(sigFieldName: str) method
- Save the result document using the PdfDocument.SaveToFile() method.
- Python
from spire.pdf.common import *
from spire.pdf import *
# Create a PdfDocument instance
doc = PdfDocument()
# Load a PDF document
doc.LoadFromFile("test2.pdf")
# Create a signature maker
signatureMaker = PdfOrdinarySignatureMaker(doc, "gary.pfx", "e-iceblue")
# Add an invisible signature to the document
signatureMaker.MakeSignature("Signature by Gary")
# Save the signed document
doc.SaveToFile("InvisibleSignature.pdf")
doc.Close()

Remove Digital Signature from PDF in Python
To remove digital signatures from a PDF document, you need to iterate through all form fields in the document, find the form fields that are of PdfSignatureFieldWidget type and then remove them from the document. The detailed steps are as follows.
- Create a PdfDocument instance.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the form field collection of the document using PdfDocument.Form property.
- Iterate through the form fields in the collection from the last to the first.
- Check if the field is a PdfSignatureFieldWidget object.
- If the result is True, remove the field from the document using PdfFormFieldWidgetCollection.FieldsWidget.RemoveAt(index) method.
- Save the result document using the PdfDocument.SaveToFile() method.
- Python
from spire.pdf.common import *
from spire.pdf import *
# Create a PdfDocument instance
doc = PdfDocument()
# Load a PDF document
doc.LoadFromFile("Signed.pdf")
# Get form field collection from the document
pdfForm = doc.Form
formWidget = PdfFormWidget(pdfForm)
# Check if there are any form fields in the collection
if formWidget.FieldsWidget.Count > 0:
# Loop through all form fields from the last to the first
for i in range(formWidget.FieldsWidget.Count - 1, -1, -1):
field = formWidget.FieldsWidget.get_Item(i)
# Check if the field is a PdfSignatureFieldWidget
if isinstance(field, PdfSignatureFieldWidget):
# Remove the field
formWidget.FieldsWidget.RemoveAt(i)
# Save the document
doc.SaveToFile("RemoveSignature.pdf")
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.