C#/VB.NET: Convert Word or Excel Files to Text

Compared with Word or Excel files, text files are smaller in size because they only consist of plain text without formatting. Besides, text files are also cross-platform as they are compatible with almost all applications. Due to such reasons, you may need to convert Word or Excel files to Text. In this article, I will introduce how to convert Word or Excel files to text using C# and VB.NET.

Installation

To convert Word and Excel files to text, this article uses Spire.Office for .NET.

You can install the API via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then execute the following command:

PM> Install-Package Spire.Office

Convert Word Doc or Docx to Text using C# and VB.NET

Spire.Office provides a Document class to work with Word documents. This class exposes a Document.SaveToTxt() method to convert a Word Doc or Docx document to text.

The following steps show you how to convert a Word document to text:

  • Initialize an instance of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Save the document as a text file using Document.SaveToTxt() method.

C#

using Spire.Doc;
using System.Text;

namespace ConvertWordToText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();
            //Load the Word document
            document.LoadFromFile("Sample.docx");

            //Convert the document to text
            document.SaveToTxt("WordToText.txt", Encoding.UTF8);
        }
    }
}

VB.NET

Imports Spire.Doc
Imports System.Text

Namespace ConvertWordToText
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a Document instance
            Dim document As Document = New Document()
            'Load the Word document
            document.LoadFromFile("Sample.docx")

            'Convert the document to text
            document.SaveToTxt("WordToText.txt", Encoding.UTF8)
        End Sub
    End Class
End Namespace

The input Word file:

The input Word file

The converted text file:

Convert Word Doc or Docx to text using C# or VB.NET

Convert Excel XLS or XLSX to Text using C# and VB.NET

The Workbook class is used to work with Excel files. You can convert a specific worksheet or all worksheets in an Excel file to text by getting the specific worksheet or looping through each worksheet in the file, then call Worksheet.SaveToFile() method to convert the worksheet to text.

The following steps show you how to convert a specific Excel worksheet to text:

  • Initialize an instance of Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the desired worksheet by its index through Workbook.Worksheets[sheetIndex] property.
  • Convert the worksheet to text using Worksheet.SaveToFile() method.

C#

using Spire.Xls;
using System.Text;

namespace ConvertExcelToText
{
    class Program
    {
        static void Main(string[] args)
        {           
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load the Excel file
            workbook.LoadFromFile("Sample.xlsx");

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

            //Save the worksheet to text
            worksheet.SaveToFile("ExcelToText.txt", " ", Encoding.UTF8);
        }
    }
}

VB.NET

Imports Spire.Xls
Imports System.Text

Namespace ConvertExcelToText
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a Workbook instance
            Dim workbook As Workbook = New Workbook()
            'Load the Excel file
            workbook.LoadFromFile("Sample.xlsx")

            'Get the first worksheet
            Dim worksheet As Worksheet = workbook.Worksheets(0)

            'Save the worksheet to text
            worksheet.SaveToFile("ExcelToText.txt", " ", Encoding.UTF8)
        End Sub
    End Class
End Namespace

The input Excel file:

The input Excel file

The converted text file:

Convert Excel XLS or XLSX to text using C# or VB.NET

Add or Remove Watermarks in Word Documents using C# and VB.NET

A watermark is a piece of text or an image that appears behind the document content. You can add a watermark to a Word document to inform other people about the document’s copyright or proprietary information. In this article, I will demonstrate how to add or remove watermarks in Word documents using C# and VB.NET.

The following topics will be discussed:

  • Add a Text Watermark to a Word Document
  • Add an Image Watermark to a Word Document
  • Remove Text or Image Watermark from a Word Document

Installation

To add or remove watermarks in Word documents, this article uses Free Spire.Doc for .NET. You can download the DLL file of the API from this website or install it via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then execute the following command:

Install-Package FreeSpire.Doc

Add a Text Watermark to a Word Document using C# and VB.NET

In Free Spire.Doc for Java, a text watermark is represented by an instance of TextWatermark class. To add a text watermark to a Word document, you need to create an instance of TextWatermark class, then set text, font, and layout properties for the text watermark, after that assign the TextWatermark instance to the Document.Watermark property.

The following steps describe how to add a text watermark to a Word document:

  • Initialize an instance of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Initialize an instance of TextWatermark class.
  • Set text, font size, font color, and layout properties for the text watermark.
  • Add the text watermark to the document by assigning the TextWatermark instance to Document.Watermark property.
  • Save the result document using Document.SaveToFile() method.

C#

using Spire.Doc;
using Spire.Doc.Documents;

namespace TextWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Document class
            Document doc = new Document();
            //Load a Word document
            doc.LoadFromFile("Sample.docx");

            //create an instance of TextWatermark class
            TextWatermark txtWatermark = new TextWatermark();

            //Set text, font size, font color and layout for the text watermark
            txtWatermark.Text = "Confidential";
            txtWatermark.FontSize = 45;
            txtWatermark.Color = System.Drawing.Color.Green;
            txtWatermark.Layout = WatermarkLayout.Diagonal;

            //Add the text watermark to the document
            doc.Watermark = txtWatermark;

            //Save the result document
            doc.SaveToFile("TextWatermark.docx", FileFormat.Docx2013);
        }
    }
}

VB.NET

Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace TextWatermark
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create an instance of Document class
            Dim doc As Document = New Document()
            'Load a Word document
            doc.LoadFromFile("Sample.docx")

            'create an instance of TextWatermark class
            Dim txtWatermark As TextWatermark = New TextWatermark()

            'Set text, font size, font color and layout for the text watermark
            txtWatermark.Text = "Confidential"
            txtWatermark.FontSize = 45
            txtWatermark.Color = Drawing.Color.Green
            txtWatermark.Layout = WatermarkLayout.Diagonal

            'Add the text watermark to the document
            doc.Watermark = txtWatermark

            'Save the result document
            doc.SaveToFile("TextWatermark.docx", FileFormat.Docx2013)
        End Sub
    End Class
End Namespace
Add text watermark to a Word document using C# and VB.NET

Add an Image Watermark to a Word Document using C# and VB.NET

An image watermark is represented by an instance of PictureWatermark class. To add an image watermark to a Word document, you need to create an instance of PictureWatermark class, then set image, scaling, and washout properties for the image watermark, after that assign the PictureWatermark instance to the Document.Watermark property.

The following steps show you how to add an image watermark to a Word document:

  • Initialize an instance of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Initialize an instance of PictureWatermark class.
  • Set image, scaling, and washout properties for the image watermark.
  • Add the image watermark to the document by assigning the PictureWatermark instance to Document.Watermark property.
  • Save the result document using Document.SaveToFile() method.

C#

using Spire.Doc;

namespace ImageWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            //create a new instance of Document and load the document from file.
            Document doc = new Document();
            doc.LoadFromFile("Sample.docx");

            //create a new instance of the PictureWatermark and load the picture from file.
            PictureWatermark picture = new PictureWatermark();
            picture.Picture = System.Drawing.Image.FromFile("logo.png");

            //set the image watermark scaling and Washout property
            picture.Scaling = 20;
            picture.IsWashout = false;

            //add the picture watermark
            doc.Watermark = picture;

            //save the document to file
            doc.SaveToFile("ImageWatermark.docx", FileFormat.Docx2013);
        }
    }
}

VB.NET

Imports Spire.Doc

Namespace ImageWatermark
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'create a new instance of Document and load the document from file.
            Dim doc As Document = New Document()
            doc.LoadFromFile("Sample.docx")

            'create a new instance of the PictureWatermark and load the picture from file.
            Dim picture As PictureWatermark = New PictureWatermark()
            picture.Picture = Drawing.Image.FromFile("logo.png")

            'set the image watermark scaling and Washout property
            picture.Scaling = 20
            picture.IsWashout = False

            'add the picture watermark
            doc.Watermark = picture

            'save the document to file
            doc.SaveToFile("ImageWatermark.docx", FileFormat.Docx2013)
        End Sub
    End Class
End Namespace
Add image watermark to a Word document using C# and VB.NET

Remove Text or Image Watermark from a Word Document using C# and VB.NET

Removing a watermark from a Word document is extremely easy with Free Spire.Doc for .NET. You just need to set the Document.Watermark property as null.

The following steps show you how to remove a watermark from a Word document:

  • Initialize an instance of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Remove the watermark from the document by setting the Document.Watermark property as null.
  • Save the result document using Document.SaveToFile() method.

C#

using Spire.Doc;

namespace RemoveWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();
            //Load a Word document
            doc.LoadFromFile("Sample.docx");

            //Remove the watermark from the document
            doc.Watermark = null;

            //Save the result document
            doc.SaveToFile("RemoveWatermark.docx", FileFormat.Docx2013);
        }
    }
}

VB.NET

Imports Spire.Doc

Namespace RemoveWatermark
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a Document instance
            Dim doc As Document = New Document()
            'Load a Word document
            doc.LoadFromFile("Sample.docx")

            'Remove the watermark from the document
            doc.Watermark = Nothing

            'Save the result document
            doc.SaveToFile("RemoveWatermark.docx", FileFormat.Docx2013)
        End Sub
    End Class
End Namespace
Remove watermark from a Word document using C# and VB.NET

C# or VB.NET: Insert, Delete, Reorder, Rotate or Extract Pages in PDF

Most PDF readers (like Adobe Acrobat or Foxit) allow users to organize PDF pages. For example, inserting, deleting, reordering, rotating and extracting pages. This article will introduce how to implement these functionalities programmatically in C# and VB.NET.

The following topics will be covered in this article:

  • Insert Pages into a PDF Document
  • Delete Pages from a PDF Document
  • Reorder Pages in a PDF Document
  • Rotate Pages in a PDF Document
  • Extract Pages from a PDF Document

Installation

Spire.PDF for .NET is used here to organize PDF pages. It’s a commercial library for creating, editing, converting and printing PDF documents. A free version of Spire.PDF for .NET is also available, but it should be pointed out that it’s limited to 10 pages while loading PDF files.

You can install either of them via NuGet:

Commercial version: Install-Package Spire.PDF

Free version: Install-Package FreeSpire.PDF

Insert Pages into a PDF Document in C# and VB.NET

Spire.PDF offers a PdfDocument.Pages.Insert(pageIndex) method to insert a page at a specified location of a PDF document. If you want to add a page to the end the document, use the PdfDocument.Pages.Add() method instead.

The following steps show how to insert a page and add a page to a PDF document:

  • Create an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Insert a blank page at the specified location of the document using PdfDocument.Pages.Insert(pageIndex) method.
  • Add a Page to the end of the document using PdfDocument.Pages.Add() method.
  • Save the result document using PdfDocument.SaveToFile() method.

C#

using Spire.Pdf;

namespace InsertPages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Sample.Pdf");

            //Insert a page at the first index position
            pdf.Pages.Insert(0);

            //Add a page to the end of the document
            pdf.Pages.Add();

            //Save the result document
            pdf.SaveToFile("InsertPages.pdf");
        }
    }
}

VB.NET

Imports Spire.Pdf

Namespace InsertPages
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim pdf As PdfDocument = New PdfDocument()
            'Load a PDF document
            pdf.LoadFromFile("Sample.Pdf")

            'Insert a page at the first index position
            pdf.Pages.Insert(0)

            'Add a page to the end of the document
            pdf.Pages.Add()

            'Save the result document
            pdf.SaveToFile("InsertPages.pdf")
        End Sub
    End Class
End Namespace

Delete Pages from a PDF Document in C# and VB.NET

The PdfDocument.Pages.RemoveAt(pageIndex) method is used to remove a specific page from a PDF document.

The following are the steps to remove a page from a PDF document:

  • Create an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Delete a specific page by page index using PdfDocument.Pages.RemoveAt(pageIndex) method.
  • Save the result document using PdfDocument.SaveToFile() method.

C#

using Spire.Pdf;

namespace DeletePages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Sample.Pdf");

            //Delete the first page by page index
            pdf.Pages.RemoveAt(0);

            //Save the result document
            pdf.SaveToFile("DeletePages.pdf");
        }
    }
}

VB.NET

Imports Spire.Pdf

Namespace DeletePages
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim pdf As PdfDocument = New PdfDocument()
            'Load a PDF document
            pdf.LoadFromFile("Sample.Pdf")

            'Delete the first page by page index
            pdf.Pages.RemoveAt(0)

            'Save the result document
            pdf.SaveToFile("DeletePages.pdf")
        End Sub
    End Class
End Namespace

Reorder Pages in a PDF Document in C# and VB.NET

To reorder or rearrange the pages in a PDF document, you can use the PdfDocument.Pages.ReArrange() method.

The following steps show how to reorder pages in a PDF document:

  • Create an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Create an int array for new page order.
  • Reorder the pages in the document using PdfDocument.Pages.ReArrange(orderArray) method.
  • Save the result document using PdfDocument.SaveToFile() method.

C#

using Spire.Pdf;

namespace ReorderPages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Sample.Pdf");

            //Create an int array for new page order 
            //Original page order: 0, 1, 2, 3. Changed page order: 1, 0, 2, 3
            int[] orderArray = new int[] { 1, 0, 2, 3 };
            //Reorder the pages
            pdf.Pages.ReArrange(orderArray);

            //Save the result document
            pdf.SaveToFile("ReorderPages.pdf");
        }
    }
}

VB.NET

Imports Spire.Pdf

Namespace ReorderPages
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim pdf As PdfDocument = New PdfDocument()
            'Load a PDF document
            pdf.LoadFromFile("Sample.Pdf")

            'Create an int array for new page order 
            'Original page order: 0, 1, 2, 3. Changed page order: 1, 0, 2, 3
            Dim orderArray = New Integer() {1, 0, 2, 3}
            'Reorder the pages
            pdf.Pages.ReArrange(orderArray)

            'Save the result document
            pdf.SaveToFile("ReorderPages.pdf")
        End Sub
    End Class
End Namespace

Rotate Pages in a PDF Document in C# and VB.NET

The PdfPageBase.Rotation property is used to get or set the rotation angle of a PDF page. You can rotate a page by 0/90/180/270 degrees.

The following are the steps to rotate a page in a PDF document:

  • Create an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the desired page by page index through PdfDocument.Pages[pageIndex] property.
  • Get the original rotation angle of the page through PdfPageBase.Rotation property.
  • Increase the original rotation angle by desired degrees.
  • Apply the new rotation angle to the page through PdfPageBase.Rotation property.
  • Save the result document using PdfDocument.SaveToFile() method.

C#

using Spire.Pdf;

namespace RotatePages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Sample.pdf");

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

            //Get the original rotation angle of the page
            int rotation = (int)page.Rotation;

            //Rotate the page 180 degrees clockwise based on the original rotation angle
            rotation += (int)PdfPageRotateAngle.RotateAngle180;
            page.Rotation = (PdfPageRotateAngle)rotation;

            //Save the result document
            pdf.SaveToFile("RotatePages.pdf");
        }
    }
}

VB.NET

Imports Spire.Pdf

Namespace RotatePages
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim pdf As PdfDocument = New PdfDocument()
            'Load a PDF document
            pdf.LoadFromFile("Sample.pdf")

            'Get the first page
            Dim page As PdfPageBase = pdf.Pages(0)

            'Get the original rotation angle of the page
            Dim rotation = CInt(page.Rotation)

            'Rotate the page 180 degrees clockwise based on the original rotation angle
            rotation += CInt(PdfPageRotateAngle.RotateAngle180)
            page.Rotation = CType(rotation, PdfPageRotateAngle)

            'Save the result document
            pdf.SaveToFile("RotatePages.pdf")
        End Sub
    End Class
End Namespace

Extract Pages from a PDF Document in C# and VB.NET

You can extract a specific page or a range of pages from a PDF document to another by using the PdfDocument.InsertPage(PdfDocument, pageIndex) method or the PdfDocument.InsertPageRange(PdfDocument, startPageIndex, endPageIndex) method.

The following steps show how to extract a page or a range of pages from an existing PDF document to a new PDF document:

  • Create an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Create an instance of PdfDocument class.
  • Extract a specific page or a range of pages from the source PDF document to the new PDF document using PdfDocument.InsertPage(PdfDocument, pageIndex) method or PdfDocument.InsertPageRange(PdfDocument, startPageIndex, endPageIndex) method.
  • Save the new PDF document using PdfDocument.SaveToFile() method.

C#

using Spire.Pdf;

namespace ExtractPages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Sample.Pdf");

            //Create a new PDF
            PdfDocument newPdf = new PdfDocument();

            //Extract the first page of the source PDF to the new PDF
            newPdf.InsertPage(pdf, 0);

            //Extract a range of pages (page 1 - 4) of the source PDF to the new PDF
            //newPdf.InsertPageRange(pdf, 0, 3);

            //Save the new PDF document to file
            newPdf.SaveToFile("ExtractPages.pdf");
        }
    }
}

VB.NET

Imports Spire.Pdf

Namespace ExtractPages
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim pdf As PdfDocument = New PdfDocument()
            'Load a PDF document
            pdf.LoadFromFile("Sample.Pdf")

            'Create a new PDF
            Dim newPdf As PdfDocument = New PdfDocument()

            'Extract the first page of the source PDF to the new PDF
            newPdf.InsertPage(pdf, 0)

            'Extract a range of pages (page 1 - 4) of the source PDF to the new PDF
            'newPdf.InsertPageRange(pdf, 0, 3);

            'Save the new PDF document to file
            newPdf.SaveToFile("ExtractPages.pdf")
        End Sub
    End Class
End Namespace

Merge or Split PowerPoint Presentations in Java

When working with PowerPoint Presentations, you may need to merge several presentations into one or split a large presentation into smaller ones. In this article, I will introduce how to merge or split PowerPoint Presentations in Java using Spire.Presentation for Java.

Add Dependencies

Method 1. If you are using maven, you can install the jar of Spire.Presentation for Java into your project by adding the following code to your project’s pom.xml file.

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

Method 2. If you are not using maven, you can download Spire.Presentation for Java from this website, extract the package and then import the Spire.Presentation.jar under the lib folder into your project as a dependency.

Using The Code

Spire.Presentation for Java provides a Presentation.getSlides.append(ISlide) method which enables you to merge or split PowerPoint presentations by cloning slides from one PowerPoint presentation to another. The following examples will show you how to merge PowerPoint presentations into one and how to split a PowerPoint presentation into multiple presentations in Java.

Example 1. Merge PowerPoint Presentations into One in Java

You can extract all or specified slides of a PowerPoint presentation, and then add them to the end or specified position of a destination presentation. The following are the steps to do so:

  • Create an instance of Presentation class.
  • Load the first PowerPoint presentation using Presentation.loadFromFile() method.
  • Create an instance of Presentation class.
  • Load the second PowerPoint presentation using Presentation.loadFromFile() method.
  • Loop through the slides in the first presentation, then add each slide to the end of the second presentation using Presentation.getSlides().append() method. If you want to add each slide to a specified position, use the Presentation.getSlides().insert() method instead.
  • Save the result presentation to another file using Presentation.saveToFile() method.
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

public class MergePresentations {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt1= new Presentation();
        //Load the first presentation
        ppt1.loadFromFile("Sample1.pptx");

        //Create a Presentation instance
        Presentation ppt2 = new Presentation();
        //Load the second presentation
        ppt2.loadFromFile("Sample2.pptx");

        //Loop through the slides of the first presentation
        for(int i = 0; i < ppt1.getSlides().getCount(); i++) {
            //Append the slides of the first presentation to the end of the second presentation
            ppt2.getSlides().append(ppt1.getSlides().get(i));

            //Insert the slides of the first presentation into the specified position of second presentation
            //ppt2.getSlides().insert(0, ppt1.getSlides().get(i));
        }

        //Save the result presentation to another file
        ppt2.saveToFile("MergePresentations.pptx", FileFormat.PPTX_2013);
    }
}
Merge two or more PowerPoint presentations into one using Java

Example 2. Split a PowerPoint Presentation into Multiple Presentations in Java

You can split a PowerPoint presentation by each slide or by slide range. The following are the steps to split a PowerPoint presentation by each slide:

  • Create an instance of Presentation class.
  • Load the first PowerPoint presentation using Presentation.loadFromFile() method.
  • Loop through the slides in the presentation.
  • Within the loop, create an instance of Presentation class, remove the default slide, then add each slide of the source presentation to the new presentation using Presentation.getSlides().append() method. After that, save the result presentation to file using Presentation.saveToFile() method.
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

public class SplitPresentationByEachPage {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load the presentation
        ppt.loadFromFile("Sample1.pptx");

        //Loop through the slides in the presentation
        for (int i = 0; i < ppt.getSlides().getCount(); i++)
        {
            //Create a new Presentation
            Presentation newPPT = new Presentation();
            //Remove the default slide
            newPPT.getSlides().removeAt(0);

            //Extract each slide from the source presentation and append it to the new presentation
            newPPT.getSlides().append(ppt.getSlides().get(i));

            //Save the result presentation to file
            newPPT.saveToFile(String.format("split/split-%d.pptx",i), FileFormat.PPTX_2013);
        }
    }
}
Split a PowerPoint presentation to multiple presentations using Java

Java: Change PDF Page Margins

For various reasons, you may need to change page margins of a PDF document. For instance, you want to add additional information such as a stamp to a PDF page, but the page is full of content and has no extra margin spaces for you to do so. This article will demonstrate how to change PDF page margins using Java.

The following topics will be discussed:

  • Change PDF Page Margins without Changing Page Size
  • Change PDF Page Margins and Also Change Page Size

Add Dependencies

To change PDF page margins, this article uses Spire.PDF for Java. If you are using maven, you can install the jar of Spire.PDF for Java into your project by adding the following code to your project’s pom.xml file.

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

If you are not using maven, you can download Spire.PDF for Java from this website, extract the package and then import the Spire.Pdf.jar under the lib folder into your project as a dependency.

Change PDF Page Margins without Changing Page Size in Java

It is possible to change the margins without changing the page size, but the page content may be scaled. The following steps show how to increase the page margins of a PDF document without changing page size using Java:

  • Load the source PDF document using PdfDocument class.
  • Create a new PDF document using PdfDocument class. Then set the increasing or decreasing value of margins as margins of the new PDF document.
  • Loop through the pages in the source PDF document.
  • Add a page that has the same page size with the source page to the new PDF document.
  • Create a new instance of PdfTextLayout class and specify the layout type as one page using PdfTextLayout.setLayout(PdfLayoutType.One_Page) method (if not set the content will not scale to fit page size).
  • Create a template based on the source page using PdfPageBase.createTemplate() method.
  • Draw the template on the page of the new PDF document with one page layout using PdfTemplate.draw() method.
  • Save the new PDF document to file using PdfDocument.saveToFile() method.

Code example:

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfLayoutType;
import com.spire.pdf.graphics.PdfTemplate;
import com.spire.pdf.graphics.PdfTextLayout;

import java.awt.geom.Point2D;

public class IncreasePageMargins {
    public static void main(String[] args) {

        //Load the source PDF document
        PdfDocument sourcePdf = new PdfDocument("Input.pdf");

        //Create a new PDF document
        PdfDocument newPdf = new PdfDocument();

        //Set increasing value of page margins as page margins of the new PDF document
        //if you want to decrease the margins, set the value as negative
        newPdf.getPageSettings().getMargins().setLeft(40);
        newPdf.getPageSettings().getMargins().setRight(40);
        newPdf.getPageSettings().getMargins().setTop(40);
        newPdf.getPageSettings().getMargins().setBottom(40);

        //Loop through the pages in the source PDF document
        for (PdfPageBase sourcePage : (Iterable<PdfPageBase>) sourcePdf.getPages())
        {
            //Add a page with the same page size to the new PDF document
            PdfPageBase newPage = newPdf.getPages().add(sourcePage.getSize());

            //Set text layout as 1 page, if not set the content will not scale to fit page size
            PdfTextLayout layout = new PdfTextLayout();
            layout.setLayout(PdfLayoutType.One_Page);

            //Create a template based on the source page and draw it on the page of the new PDF document
            PdfTemplate template = sourcePage.createTemplate();
            template.draw(newPage, new Point2D.Float(0, 0), layout);
        }

        //Save the new PDF document to file
        newPdf.saveToFile("ChangeMargins1.pdf");
    }
}

The source PDF:

The source PDF document

The result PDF:

The result PDF document after changing page margins

Change PDF Page Margins and Also Change Page Size in Java

The page size can be adjusted accordingly if you don’t want to scale the page content when changing margins.

The following steps show how to change PDF page margins and also change page size using Java:

  • Load the source PDF document using PdfDocument class.
  • Create a new PDF document using PdfDocument class. Then set the increasing or decreasing value of margins as margins of the new PDF document.
  • Loop through the pages in the source PDF document.
  • Create an instance of Dimension class which will then be used as the page size of the new PDF document. Then set its size using Dimension.setSize() method.
  • Add a page with the specified size to the new PDF document.
  • Create a template based on the source page using PdfPageBase.createTemplate() method.
  • Draw the template on the page of the new PDF document using PdfTemplate.draw() method.
  • Save the new PDF document to file using PdfDocument.saveToFile() method.

Code example:

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfTemplate;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;

public class ChangePageMargins {
    public static void main(String[] args) {
        //Load the source PDF document
        PdfDocument sourcePdf = new PdfDocument("Input.pdf");

        //Create a new PDF document
        PdfDocument newPdf = new PdfDocument();

        //Set increasing value of margins as margins of the new PDF document 
        //if you want to decrease the margins, set the value as negative
        newPdf.getPageSettings().getMargins().setLeft(40);
        newPdf.getPageSettings().getMargins().setRight(40);
        newPdf.getPageSettings().getMargins().setTop(40);
        newPdf.getPageSettings().getMargins().setBottom(40);

        //Loop through the pages in the source PDF document
        for (PdfPageBase sourcePage : (Iterable<PdfPageBase>) sourcePdf.getPages())
        {
            double width = sourcePage.getSize().getWidth() + newPdf.getPageSettings().getMargins().getLeft() + newPdf.getPageSettings().getMargins().getRight();
            double height = sourcePage.getSize().getHeight() + newPdf.getPageSettings().getMargins().getTop() + newPdf.getPageSettings().getMargins().getBottom();
            //Create a SizeF instance which will then be used as the page size of the new PDF document
            Dimension2D size = new Dimension();
            //Set its width and height 
            size.setSize(width, height);
            //Add a page with larger page size to the new PDF document
            PdfPageBase newPage = newPdf.getPages().add(size);
            //Create a template based on the source page and draw it on the page of the new PDF document
            PdfTemplate template = sourcePage.createTemplate();
            template.draw(newPage, new Point2D.Float(0, 0));
        }

        //Save the new PDF document to file
        newPdf.saveToFile("ChangeMargins2.pdf");
    }
}

The result PDF:

The result PDF after changing page margins and page size
Design a site like this with WordPress.com
Get started