Spire.Office Knowledgebase Page 26 | E-iceblue

C#: Create Actions in PDF Documents

2024-11-25 01:09:22 Written by Koohji

Enhancing the interactivity of PDF files is a crucial aspect of document management and user engagement. Creating actions within PDFs using C# in the .NET framework allows developers to add dynamic elements such as file links, web links, and audio that can execute various functions like navigating to different pages, launching external applications, or playing background music, which improves the user experience by making PDFs more functional and engaging. This article demonstrates how to use the Spire.PDF for .NET library to create actions in PDF documents with C#.

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

General Steps for Adding Actions to PDF with C#

Adding actions to a PDF using C# involves integrating interactive elements that enhance user experience, such as navigation buttons, file links, web links, or sound triggers. With the Spire.PDF for .NET library, developers can create various actions in PDF documents with C# code. Below is a table of the classes for commonly used actions and their descriptions:

Class Decryption
PdfGoToAction Represents an action that navigates to a destination within the current document.
PdfLaunchAction Represents an action that opens a file.
PdfSoundAction Represents an action that plays a sound.
PdfJavaScriptAction Represents an action that executes JavaScript code in a PDF document.
PdfUriAction Represents an action that resolves a Uniform Resource Identifier (URI).
PdfGoToAction Represents an action that navigates to a destination within the current document.

For more action classes and their descriptions, refer to Spire.PDF for .NET action API references.

Actions can be added to PDF documents in two primary ways:

1. Using Action Annotations

This method involves creating an action and linking it to an annotation on the page. The action is displayed and triggered when the annotation is clicked.

General Steps:

  • Create an instance of PdfDocument class and load a PDF document using PdfDocument.LoadFromFile() method.
  • Get a page using PdfDocument.Pages[] property.
  • Create an instance of the class that represents the action and set the action properties.
  • Create an instance of PdfActionAnnotation class in a rectangular area on the page using the action.
  • Add the cue word for the action to the page (optional).
  • Add the action annotation to the page using PdfPageBase.Annotations.Add() method.
  • Save the result document using PdfDocument.SaveToFile() method.

2. Assigning Actions to Document Events

Actions can also be assigned to document-level events such as opening, closing, or printing the document. These actions are triggered automatically when the specified events occur.

General Steps:

  • Create an instance of PdfDocument class and load a PDF document using PdfDocument.LoadFromFile() method.
  • Create an instance of the class that represents the action and set the action properties.
  • Assign the action to a document event using the following properties:
    • PdfDocument.AfterOpenAction
    • PdfDocument.AfterSaveAction
    • PdfDocument.AfterPrintAction
    • PdfDocument.BeforeCloseAction
    • PdfDocument.BeforeSaveAction
    • PdfDocument.BeforePrintAction
  • Save the result document using PdfDocument.SaveToFile() method.

Create Navigation Actions in PDF with C#

Navigation actions can be created using the PdfGoToAction class, which defines navigation within the document to a specified destination. To achieve this, developers can create a PdfDestination object and pass it as a parameter to a PdfGoToAction instance.

The following is a code example of adding a navigation action to a PDF:

  • C#
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using System.Drawing;

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

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

            // Create a PdfDestination instance and set the destination
            PdfDestination destination = new PdfDestination(pdf.Pages[1]);
            destination.Location = new PointF(0, 0);
            destination.Mode = PdfDestinationMode.Location;
            destination.Zoom = 0.8f;

            // Create a PdfGoToAction based on the destination
            PdfGoToAction action = new PdfGoToAction(destination);

            // Create a rectangle and draw it on the first page
            RectangleF rect = new RectangleF(70, pdf.PageSettings.Size.Height - 120, 140, 20);
            pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.LightGray, rect);
            // Draw cue words on the rectangle
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 14);
            PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            pdf.Pages[0].Canvas.DrawString("To Page 2", font, PdfBrushes.Green, rect, stringFormat);

            // Create a PdfActionAnnotation instance based on the rectangle and the action
            PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);

            // Add the action annotation to the first page
            pdf.Pages[0].Annotations.Add(actionAnnotation);

            // Save the document
            pdf.SaveToFile("output/NavigationButton.pdf");
            pdf.Close();
        }
    }
}

Result of Creating Navigation Buttons with C#

Create File Launch Actions in PDF with C#

The PdfLaunchAction class defines a file open action in a PDF that enables users to open a specific file by clicking a button embedded on a PDF page. When implementing this action, developers can set the file path (absolute or relative) and decide whether the file should open in a new window. Here is a code example for adding a file launch action in a PDF document:

  • C#
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;

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

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

            // Get the first page
            PdfPageBase page = pdf.Pages[0];

            // Draw a rectangle on the page
            RectangleF rect = new RectangleF(50, 50, 180, 20);
            page.Canvas.DrawRectangle(PdfBrushes.LightGray, rect);
            // Darw the cue words in the rectangle
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 14);
            PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            pdf.Pages[0].Canvas.DrawString("Click to launch Sample2", font, PdfBrushes.Green, rect, stringFormat);

            // Create a PdfLaunchAction instance
            PdfLaunchAction action = new PdfLaunchAction("C:/Sample2.pdf", PdfFilePathType.Absolute);
            // Set the launch mode to open in new window
            action.IsNewWindow = true;

            // Create a PdfActionAnnotation instance based on the rectangle and the launch action
            PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);

            // Add the action annotation to the first page
            page.Annotations.Add(actionAnnotation);

            // Save the document
            pdf.SaveToFile("output/LaunchAction.pdf");
            pdf.Close();
        }
    }
}

Result of Creating File Launch Actions with C#

Create Sound Actions in PDF with C#

Developers can embed audio as an action in PDF documents with the PdfSoundAction class, enabling the audio to play in response to specific triggers, such as opening the file or clicking a button. Below is a code example for creating a sound action in a PDF document:

  • C#
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using Spire.Pdf.General;
using System.Drawing;

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

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

            // Get the first page
            PdfPageBase page = pdf.Pages[0];

            // Darw the cue image on the page
            PdfImage image = PdfImage.FromFile("Sound.png");
            page.Canvas.DrawImage(image, new PointF(30, 30));

            // Create a PdfSoundAction instance and set its property
            PdfSoundAction action = new PdfSoundAction("Wave.wav");
            // Set the sound parameters
            action.Sound.Bits = 16;
            action.Sound.Channels = PdfSoundChannels.Stereo;
            action.Sound.Encoding = PdfSoundEncoding.Signed;
            action.Sound.Rate = 44100;
            // Set the play options
            action.Volume = 0;
            action.Repeat = true;
            action.Mix = true;
            action.Synchronous = true;

            // Create a PdfActionAnnotation instance using the sound action at the location of the cue image
            RectangleF rect = new RectangleF(30, 30, image.Width, image.Height);
            PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);

            // Add the action annotation to the first page
            page.Annotations.Add(actionAnnotation);

            // Set the sound action to play after the document is opened
            pdf.AfterOpenAction = action;

            // Save the document
            pdf.SaveToFile("output/SoundAction.pdf");
            pdf.Close();
        }
    }
}

Result of Creating Sound Actions with C#

Create Web Link Actions in PDF with C#

Developers can use the PdfUriAction class to create a web link action in PDF documents, allowing users to open a web link when performing specific actions, such as clicking a button. Below is a code example for creating a web link action in a PDF document:

  • C#
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;

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

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

            // Get the first page
            PdfPageBase page = pdf.Pages[0];

            // Draw a rectangle on the page
            RectangleF rect = new RectangleF(30, 30, 120, 20);
            page.Canvas.DrawRectangle(PdfBrushes.LightGray, rect);
            // Draw the cue words in the rectangle
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 14);
            PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            page.Canvas.DrawString("Go to Google Search", font, PdfBrushes.LightSkyBlue, rect);

            // Create a PdfUriAction instance and set its property
            PdfUriAction action = new PdfUriAction();
            action.Uri = "https://www.google.com/";

            // Create a PdfActionAnnotation instance using the web link action and the rectangle
            PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);

            // Add the action annotation to the first page
            page.Annotations.Add(actionAnnotation);

            // Save the document
            pdf.SaveToFile("output/WebLinkAction.pdf");
            pdf.Close();
        }
    }
}

Result of Creating Web Link Actions with C#

Create JavaScript Actions in PDF with C#

The PdfJavaScriptAction represents a JavaScript action in PDF, which allows developers to create complex interactions such as form validations, data calculations, and custom user interfaces. Below is a code example for adding a simple JavaScript action to a PDF document with C#:

  • C#
using Spire.Pdf;
using Spire.Pdf.Actions;

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

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

            // Define JavaScript code
            var jsCode =
            "app.alert({" +
            "    cMsg: 'Welcome to the article about the Evolution and Advantages of LCD Screens!\\n\\nThis article explores the history and benefits of LCD technology, including its impact on visual experiences across various devices.', " +
            "    nIcon: 3, " +
            "    cTitle: 'Document Introduction'" +
            "});";

            // Create a PdfJavaScriptAction instance using the JavaScript code
            PdfJavaScriptAction action = new PdfJavaScriptAction(jsCode);

            // Set the action to be executed when the PDF document is launched
            pdf.AfterOpenAction = action;

            // Save the document
            pdf.SaveToFile("output/PDFJavaScriptAction.pdf");
            pdf.Close();
        }
    }
}

Result of Creating JavaScript Actions with C#

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.

Python: Extract Annotations from PDF

2024-11-22 08:40:38 Written by Koohji

Annotations in PDF documents play a crucial role in enhancing collaboration, emphasizing key points, or providing additional context. Extracting annotations is essential for efficiently analyzing PDF content, but manual extraction can be tedious. This guide demonstrates how to extract annotations from PDF with Python using Spire.PDF for Python, providing a faster and more flexible solution to access important information.

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 it, please refer to this tutorial: How to Install Spire.PDF for Python on Windows.

Extract Specified Annotations from PDF Documents

Although Adobe Acrobat offers a built-in one-click annotation extraction feature, it lacks flexibility when handling specific annotations. If you only need to extract one or a few annotations, you must manually locate and copy them, which can be inefficient, especially when working with PDFs containing multiple annotations. Spire.PDF (short for Spire.PDF for Python), however, provides the PdfAnnotationCollection.get_item() method, enabling targeted extraction of specific annotations, making PDF annotation management more flexible and efficient.

Steps to extract specified annotations from PDF:

  • Create an object of PdfDocument class.
  • Load a PDF document from the local storage with PdfDocument.LoadFromFile() method.
  • Get a page using PdfDocument.Pages[] property, and access the annotations collection with PdfPageBase.AnnotationsWidget property.
  • Create a list to store annotation information.
  • Access the specified annotation using PdfAnnotationCollection.get_Item() method.
  • Append annotation details to the list.
  • Save the list as a Text file.

Here is the code example of exporting the first annotation on the third page:

  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a new PDF document
pdf = PdfDocument()

# Load the file from disk
pdf.LoadFromFile( "Sample.pdf")

# Get the third page 
page = pdf.Pages[2]

# Access the annotations on the page
annotations = page.AnnotationsWidget

# Create a list to save information of annotations
sb = []

# Access the first annotation on the page
annotation = annotations.get_Item(0)

# Append the annotation details to the list
sb.append("Annotation information: ")
sb.append("Text: " + annotation.Text)
modifiedDate = annotation.ModifiedDate.ToString()
sb.append("ModifiedDate: " + modifiedDate)

# Save the list as a Text file
with open("GetSpecificAnnotation.txt", "w", encoding="utf-8") as file:
    file.write("\n".join(sb))

# Close the PDF file
pdf.Close()

Extract Specified Annotations from PDF

Extract All Annotations from a PDF Page

To export all annotations from a specified PDF page, you can still use the PdfPageBase.AnnotationsWidget property along with the PdfAnnotationCollection.get_item() method. However, you will need to iterate through all the annotations on the page to ensure none are missed. Below are the steps and code examples to guide you through the process.

Steps to extract annotations from PDF pages:

  • Create a PdfDocument instance.
  • Read a PDF document from the local storage with PdfDocument.LoadFromFile() method.
  • Access the annotation collection on the specified page using PdfDocument.Pages.AnnotationsWidget property.
  • Create a list to store annotation information.
  • Loop through annotations on a certain page.
    • Retrieve each annotation using PdfAnnotationCollection.get_Item() method.
    • Add annotation details to the list.
  • Save the list as a Text file.

Below is the code example of extracting all annotations on the second page:

  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a new PDF document
pdf = PdfDocument()

# Load the file from disk
pdf.LoadFromFile("Sample.pdf")

# Get all annotations from the second page
annotations = pdf.Pages[1].AnnotationsWidget

# Create a list to maintain annotation details
sb = []

# Loop through annotations on the page
if annotations.Count > 0:
    for i in range(annotations.Count):
        # Get the current annotation
        annotation = annotations.get_Item(i)

        # Get the annotation details
        if isinstance(annotation, PdfPopupAnnotationWidget):
            continue
        sb.append("Annotation information: ")
        sb.append("Text: " + annotation.Text)
        modifiedDate = annotation.ModifiedDate.ToString()
        sb.append("ModifiedDate: " + modifiedDate)

# Save annotations as a Text file
with open("GetAllAnnotationsFromPage.txt", "w", encoding="utf-8") as file:
    file.write("\n".join(sb))

# Release resources
pdf.Close()

Export All Annotations on a PDF Page

Extract All Annotations from PDF Files

The final section of this guide illustrates how to extract all annotations from a PDF document using Python. The process is similar to exporting annotations from a single page but involves iterating through each page, traversing all annotations, and accessing their details. Finally, the extracted annotation details are saved to a text file for further use. Let’s take a closer look at the detailed steps.

Steps to extract all annotations from a PDF document:

  • Create an instance of PdfDocument class.
  • Read a PDF document from the disk with PdfDocument.LoadFromFile() method.
  • Initialize a list to store annotation information.
  • Loop through all pages and access the annotation collection with PdfDocument.Pages.AnnotationsWidget property.
    • Iterate each annotation in the collection and get annotations using PdfAnnotationCollection.get_item() method.
    • Append annotation details to the list.
  • Output the list as a Text file.

Here is an example of exporting all annotations from a PDF file:

  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a new PDF document
pdf = PdfDocument()

# Load the file from disk 
pdf.LoadFromFile("Sample.pdf")

# Create a list to save annotation details
sb = []

# Iterate through all pages in the PDF document
for pageIndex in range(pdf.Pages.Count):
    sb.append(f"Page {pageIndex + 1}:")

    # Access the annotation collection of the current page
    annotations = pdf.Pages[pageIndex].AnnotationsWidget
   
    # Loop through annotations in the collection
    if annotations.Count > 0:
        for i in range(annotations.Count):
            # Get the annotations of the current page
            annotation = annotations.get_Item(i)

            # Skip invalid annotations (empty text and default date)
            if not annotation.Text.strip() and annotation.ModifiedDate.ToString() == "0001/1/1 0:00:00":
                continue
           
            # Extract annotation information
            sb.append("Annotation information: ")
            sb.append("Text: " + (annotation.Text.strip() or "N/A"))
            modifiedDate = annotation.ModifiedDate.ToString()
            sb.append("ModifiedDate: " + modifiedDate)
    else:
        sb.append("No annotations found.")

    # Add a blank line after each page
    sb.append("")

# Save all annotations to a file
with open("GetAllAnnotationsFromDocument.txt", "w", encoding="utf-8") as file:
    file.write("\n".join(sb))

# Close the PDF document
pdf.Close()

Extract All Annotations from a PDF Document

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.

XML is widely used for storing and exchanging data due to its flexibility and self-descriptive nature. However, many users find it challenging to work with XML files directly. Excel, on the other hand, is a familiar and user-friendly tool for data analysis. Converting XML to Excel not only makes data more accessible but also enhances its usability for various applications.

In this article, you will learn how to convert XML to Excel as well as XML to PDF in Python using Spire.XLS for Python.

Install Spire.XLS for Python

This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.XLS

If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows

Understanding XML Structure: Elements, Attributes, and Data

Before converting XML to Excel, it's crucial to understand the structure of XML files. XML is a markup language that uses tags to define elements, attributes, and data. Here’s a breakdown of these components:

  • Elements: These are the building blocks of XML. They are defined by start and end tags and can contain data or other elements.
<person>
    <name>John Doe</name>
    <age>30</age>
</person>
  • Attributes: These provide additional information about elements. They are specified within the start tag of an element.
<person id="1">
    <name>John Doe</name>
    <age>30</age>
</person>
  • Data: This is the content enclosed within the start and end tags of an element.

Understanding these components will help you map XML data to Excel effectively.

Convert XML to Excel in Python

To load an XML file into Python, you can use the xml.etree.ElementTree library, which is included in Python's standard library. This library provides methods to navigate and manipulate the XML tree. Here is an example:

import xml.etree.ElementTree as ET

# Load the XML file
tree = ET.parse('data.xml')
root = tree.getroot()

# Iterate through elements
for person in root.findall('person'):
    name = person.find('name').text
    age = person.find('age').text

After parsing the XML data, the next step is to map it to an Excel worksheet. You can utilize Spire.XLS for Python to create a new workbook, input data into specific cells, and apply styles and formatting to the worksheet. These formatting options include auto-fitting column widths, adjusting text alignment and making the header bold.

To convert XML to Excel in Python, follow these steps:

  • Use the xml.etree.ElementTree library to retrieve data from an XML file.
  • Create a Workbook object.
  • Add a worksheet using the Workbook.Worksheets.Add() method.
  • Write data extracted from the XML file into the cells of the worksheet using the Worksheet.SetValue() method.
  • Apply styles and formatting to enhance the worksheet appearance.
  • Save the workbook to an Excel file.

The following code provides a more intelligent and advanced way to read data from XML and import it into an Excel file.

  • Python
from spire.xls import *
from spire.xls.common import *
import xml.etree.ElementTree as ET

# Create a Workbook object
workbook = Workbook()

# Remove default worksheets
workbook.Worksheets.Clear()

# Add a worksheet and name it
worksheet = workbook.Worksheets.Add("Books")

# Load an XML file
xml_tree = ET.parse("C:\\Users\\Administrator\\Desktop\\Books.xml")

# Get the root element of the XML tree
xml_root = xml_tree.getroot()

# Get the first the "book" element
first_book = xml_root.find("book")

# Extract header information and convert it into a list
header = list(first_book.iter())[1:]  

# Write header to Excel
for col_index, header_node in enumerate(header, start=1):
    header_text = header_node.tag
    worksheet.SetValue(1, col_index, header_text)

# Write other data to Excel by iterating over each book element and each data node within it
row_index = 2
for book in xml_root.iter("book"):
    for col_index, data_node in enumerate(list(book.iter())[1:], start=1):  
        value = data_node.text
        header_text = list(header[col_index - 1].iter())[0].tag
        worksheet.SetValue(row_index, col_index, value)
    row_index += 1

# Set column width
worksheet.AllocatedRange.AutoFitColumns()

# Set alignment
worksheet.AllocatedRange.HorizontalAlignment = HorizontalAlignType.Left

# Set font style
worksheet.Range["A1:F1"].Style.Font.IsBold = True

# Save the workbook to an Excel file
workbook.SaveToFile("output/XmlToExcel.xlsx")

# Dispose resources
workbook.Dispose()

Convert XML to Excel in Python

Convert XML to PDF in Python

The previous example successfully imports data from an XML file into an Excel worksheet. You can convert this worksheet to a PDF using the Worksheet.SaveToPdf() method. To create a well-structured PDF, consider adjusting page layout settings, such as margins and gridline preservation, during the conversion process.

The steps to convert XML to PDF using Python are as follows:

  • Use the xml.etree.ElementTree library to retrieve data from an XML file.
  • Create a Workbook object.
  • Add a worksheet using the Workbook.Worksheets.Add() method.
  • Write data extracted from the XML file into the cells of the worksheet using the Worksheet.SetValue() method.
  • Apply styles and formatting to enhance the worksheet appearance.
  • Configure page settings using the properties under the PageSetup object, which is returned by the Worksheet.PageSetup property.
  • Save the worksheet as a PDF file using the Worksheet.SaveToPdf() method.

The following code snippet demonstrates how to import data from XML into a worksheet and save that worksheet as a PDF file.

  • Python
from spire.xls import *
from spire.xls.common import *
import xml.etree.ElementTree as ET

# Create a Workbook object
workbook = Workbook()

# Remove default worksheets
workbook.Worksheets.Clear()

# Add a worksheet and name it
worksheet = workbook.Worksheets.Add("Books")

# Load an XML file
xml_tree = ET.parse("C:\\Users\\Administrator\\Desktop\\Books.xml")

# Get the root element of the XML tree
xml_root = xml_tree.getroot()

# Get the first the "book" element
first_book = xml_root.find("book")

# Extract header information and convert it into a list
header = list(first_book.iter())[1:]  

# Write header to Excel
for col_index, header_node in enumerate(header, start=1):
    header_text = header_node.tag
    worksheet.SetValue(1, col_index, header_text)

# Write other data to Excel by iterating over each book element and each data node within it
row_index = 2
for book in xml_root.iter("book"):
    for col_index, data_node in enumerate(list(book.iter())[1:], start=1):  
        value = data_node.text
        header_text = list(header[col_index - 1].iter())[0].tag
        worksheet.SetValue(row_index, col_index, value)
    row_index += 1

# Set column width
worksheet.AllocatedRange.AutoFitColumns()

# Set alignment
worksheet.AllocatedRange.HorizontalAlignment = HorizontalAlignType.Left

# Set font style
worksheet.Range["A1:F1"].Style.Font.IsBold = True

# Fit worksheet on one page
workbook.ConverterSetting.SheetFitToPage = True

# Get the PageSetup object
pageSetup = worksheet.PageSetup

# Set page margins
pageSetup.TopMargin = 0.3
pageSetup.BottomMargin = 0.3
pageSetup.LeftMargin = 0.3
pageSetup.RightMargin = 0.3

# Preserve gridlines 
pageSetup.IsPrintGridlines = True

# Save the worksheet to a PDF file
worksheet.SaveToPdf("output/XmlToPdf.pdf")

# Dispose resources
workbook.Dispose()

Convert XML to PDF in Python

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 26