Overview

Merging PDF files is a frequent requirement for many developers, whether to consolidate reports, compile documents, or simplify file sharing. With the GroupDocs.Merger for Python via .NET library, developers can efficiently merge multiple PDF files programmatically. This article will guide you through the process of merging PDF files using Python, covering essential functions and providing practical code examples.

You can explore the following sections to learn more about the merging process:

How to merge PDF files in Python

The GroupDocs.Merger for Python via .NET library allows developers to effortlessly merge PDF files without any third-party software involvement. This functionality is essential for tasks such as compiling multiple documents into one comprehensive file, which enhances document management and sharing.

Steps to Merge PDF Files

  1. Load the Source PDF File: Begin by creating an instance of the Merger class and passing the path of the source PDF file.
  2. Add Additional PDF Files: Use the join method of the Merger class to append other PDF files you wish to combine.
  3. Save the Merged PDF: Finally, call the save method to specify the filename for the merged PDF file and save the result.

Here’s a simple example of how to merge two PDF files:

import groupdocs.merger as gm
import constants

def run():
    print(f"----------------------------------------------------------------------------")
    print(f"[Example Basic Usage] # MultipleDocumentOperations # Merge : Pdf")

    # Create Merger instance and load the first PDF file
    with gm.Merger(constants.sample_pdf) as merger:
        print(f"Document info retrieved successfully")
        # Add another PDF file to merge
        merger.join(constants.sample_pdf)
        # Save the merged PDF file
        merger.save(constants.output_pdf)
        print(f"Merge to: {constants.output_pdf}")
    
    print(f"----------------------------------------------------------------------------")

How to merge PDF files with bookmarks in Python

Merging PDFs that contain bookmarks is essential for preserving document navigation. The GroupDocs.Merger library provides an option to include bookmarks during the merging process.

Steps to Merge PDF Files with Bookmarks

  1. Load the Source PDF File: Initialize the Merger class and load your first PDF file.
  2. Set Bookmark Options: Create a PdfJoinOptions object with the use_bookmarks flag set to True.
  3. Add Additional PDF Files: Join other PDF files to the merger while ensuring bookmarks are preserved.
  4. Save the Merged PDF: Call the save method to save the results.

Here’s how to accomplish this in code:

import groupdocs.merger as gm
import constants

def run():
    print(f"----------------------------------------------------------------------------")
    print(f"[Example Basic Usage] # MultipleDocumentOperations # Merge # Pdf : MergePdfUseBookmarks")

    # Create Merger instance and load the first PDF file
    with gm.Merger(constants.sample_pdf) as merger:
        print(f"Document info retrieved successfully")
        
        # Initialize PdfJoinOptions with the UseBookmarks flag
        pdf_join_options = gm.domain.options.PdfJoinOptions()
        pdf_join_options.use_bookmarks = True
        
        # Add another PDF file to merge with bookmarks
        merger.join(constants.sample_pdf_bookmarks, pdf_join_options)
        
        # Save the merged PDF file
        merger.save(constants.output_pdf_bookmarks)
        print(f"Merge to: {constants.output_pdf_bookmarks}")
    
    print(f"----------------------------------------------------------------------------")

How to correctly load PDF streams in Python

Merging PDF files from streams expands the flexibility of the GroupDocs.Merger library, allowing you to manage PDF content dynamically.

Steps to Load PDF Streams

  1. Load PDF from Stream: Use a file stream as the input for the Merger class.
  2. Perform Operations: Once the document is loaded from the stream, you can proceed with your merging operations.

Here’s an example:

from turtle import update
import groupdocs.merger as gm
import constants

def run():
    print(f"----------------------------------------------------------------------------")
    print(f"[Example Advanced Usage] # Loading # LoadDocumentFromStream")

    # Get the file stream for loading
    stream = get_file_stream()
    # Merge PDF files from stream
    with gm.Merger(stream) as merger:
        print(f"Document loaded from stream successfully")
    
    print(f"----------------------------------------------------------------------------")

def get_file_stream():
    # Load the PDF file as a binary stream
    file_path = constants.sample_pdf
    return open(file_path, "rb")

See also

  • Explore the full documentation for more details on the GroupDocs.Merger library.
  • Check the API Reference for in-depth information on classes and methods.
  • Browse through GitHub examples for practical implementations.
  • Visit the Releases page to get the latest version of the library.

You can download a free trial of GroupDocs.Merger for Python via .NET from here and acquire a temporary license at Temporary License for unrestricted usage of our library.

Code Examples

Here are some more detailed examples demonstrating different use cases for merging PDFs using the GroupDocs.Merger for Python via .NET library.

Example 1: Basic Merge of Multiple PDF Files

This example illustrates how to merge multiple PDF files into one.

import groupdocs.merger as gm
import constants

def run():
    print(f"----------------------------------------------------------------------------")
    print(f"[Example Basic Usage] # MultipleDocumentOperations # Merge : Pdf")

    # Create a Merger instance and load the first PDF file
    with gm.Merger(constants.sample_pdf) as merger:
        print(f"Document info retrieved successfully")
        
        # Add another PDF file to merge
        merger.join(constants.sample_pdf)
        
        # Save the merged PDF file
        merger.save(constants.output_pdf)
        print(f"Merge to: {constants.output_pdf}")
    
    print(f"----------------------------------------------------------------------------")

Example 2: Merge PDFs with Bookmarks

This example shows how to merge PDF files while preserving bookmarks.

import groupdocs.merger as gm
import constants

def run():
    print(f"----------------------------------------------------------------------------")
    print(f"[Example Basic Usage] # MultipleDocumentOperations # Merge # Pdf : MergePdfUseBookmarks")

    # Create a Merger instance and load the first PDF file
    with gm.Merger(constants.sample_pdf) as merger:
        print(f"Document info retrieved successfully")
        
        # Initialize PdfJoinOptions with the UseBookmarks flag
        pdf_join_options = gm.domain.options.PdfJoinOptions()
        pdf_join_options.use_bookmarks = True
        
        # Add another PDF file to merge with bookmarks
        merger.join(constants.sample_pdf_bookmarks, pdf_join_options)
        
        # Save the merged PDF file
        merger.save(constants.output_pdf_bookmarks)
        print(f"Merge to: {constants.output_pdf_bookmarks}")
    
    print(f"----------------------------------------------------------------------------")

Example 3: Load PDF Streams

This example demonstrates how to load PDFs using file streams, allowing for dynamic input.

from turtle import update
import groupdocs.merger as gm
import constants

def run():
    print(f"----------------------------------------------------------------------------")
    print(f"[Example Advanced Usage] # Loading # LoadDocumentFromStream")

    # Get the file stream for loading
    stream = get_file_stream()
    
    # Merge PDF files from the stream
    with gm.Merger(stream) as merger:
        print(f"Document loaded from stream successfully")
    
    print(f"----------------------------------------------------------------------------")

def get_file_stream():
    # Load the PDF file as a binary stream
    file_path = constants.sample_pdf
    return open(file_path, "rb")

See also

  • Explore the full documentation for more details about the GroupDocs.Merger library.
  • Check the API Reference for in-depth information on classes and methods.
  • Browse through GitHub examples for practical implementations.
  • Visit the Releases page to get the latest version of the library.

You can download a free trial of GroupDocs.Merger for Python via .NET from here and acquire a temporary license at Temporary License for unrestricted usage of our library.