page 277

C#/VB.NET: Delete Pages from PDF

2022-06-13 01:50:00 Written by Koohji

A PDF document can contain multiple pages with different text content, images, or other objects. Occasionally, users may need to delete certain pages with incorrectly drawn objects or pages that are irrelevant to the topic of the document. This article will demonstrate how to programmatically delete/remove pages from an existing PDF document 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 DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Delete/Remove Pages from PDF

  • Create a PdfDocument object.
  • Load a sample PDF document using PdfDocument.LoadFromFile() method.
  • Get the pages in the PDF document using PdfDocument.Pages property.
  • Delete a specified page by index using PdfPageCollection.RemoveAt(int index)method.
  • Save the document to another file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace RemovePage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument document = new PdfDocument();

            //Load a sample PDF document
            document.LoadFromFile(@"E:\Files\input.pdf");

            //Remove the second page
            document.Pages.RemoveAt(1);

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

C#/VB.NET: Delete Pages from PDF

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.

Create PDF Booklet in C#/VB.NET

2013-01-24 03:08:46 Written by Koohji

PDF booklet is pretty helpful when people print a huge PDF document. It enjoys special popularity among book, newspaper and magazine editors. This section will introduce a very simple way to create PDF booklet via a .NET PDF component in C#, VB.NET.

Spire.PDF for .NET is a .NET PDF library which can manipulate PDF documents without Adobe Acrobat or any third party library. Using this PDF component, you can quickly create PDF booklet in your .NET applications. After setting the PDF page width and height by a class Spire.Pdf.PdfPageSize, you can create your PDF booklet through implementing PdfDocument.CreateBooklet(string fileName, float width, float height, bool doubleSide) directly. Below picture shows the effect of this task:

Create PDF Booklet

Here you can quickly download Spire.PDF for .NET and install it on your system. After adding Spire.Pdf reference, please see the detail code of PDF booklet below.

Draw PDF Barcode in C#, VB.NET

[C#]
using System.Drawing;
using Spire.Pdf;

namespace PDF_Booklet
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load a PDF file
            PdfDocument doc = new PdfDocument();
            String srcPdf = @"..\read PDF.pdf";

            //Create PDF booklet
            float width = PdfPageSize.A4.Width * 2;
            float height = PdfPageSize.A4.Height;
            doc.CreateBooklet(srcPdf, width, height, true);

            //Save pdf file.
            doc.SaveToFile("Booklet.pdf");
            doc.Close();
            //Launching the Pdf file.
            System.Diagnostics.Process.Start("Booklet.pdf");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Pdf

Namespace PDF_Booklet
	Class Program
		Private Shared Sub Main(args As String())
			'Load a PDF file
			Dim doc As New PdfDocument()
			Dim srcPdf As [String] = "..\read PDF.pdf"

			'Create PDF booklet
			Dim width As Single = PdfPageSize.A4.Width * 2
			Dim height As Single = PdfPageSize.A4.Height
			doc.CreateBooklet(srcPdf, width, height, True)

			'Save pdf file.
			doc.SaveToFile("Booklet.pdf")
			doc.Close()
			'Launching the Pdf file.
			System.Diagnostics.Process.Start("Booklet.pdf")
		End Sub
	End Class
End Namespace

Spire.PDF for .NET is a PDF component that enables you to create, read, edit and manipulate PDF files in C#, VB.NET

Set Table Layout in PDF with C#/VB.NET

2013-01-23 08:34:33 Written by Koohji

Table Layout decides how the table displays in PDF page. People always set PDF table layout in order to let the table perfectly fit PDF page according to their own like. In this section, I will introduce a solution to set table layout in PDF via this .NET PDF component Spire.PDF for .NET with C#, VB.NET. First let us view the target PDF file as below picture:

Set PDF Table Layout

In Spire.PDF for .NET, there is a class called: Spire.Pdf.Tables.PdfTableLayoutFormat. By using this class, we can set table layout type and break type. Here Spire.PDF provided two layout types: Paginate and OnePage and two break type: FitElement and Fit Page. When you set the break type to be FitElement, PDF table will display according to the table length, while for FitPage choice, the table will automatically fit the PDF to every page ignoring table length.

Here let us see this method: PdfLayoutResult.Draw(PdfPageBase page, PointF location, PdfTextLayout format).There are three parameters passed. The first parameter determines the page size and margin. By setting the second parameter, we can set the horizontal and vertical distance between the table and PDF margin. While the last parameter is the table layout we just set. Obviously, we can set table layout by calling this method directly.

Now, you can download Spire.PDF for .NET and start table layout task by below key code:

[C#]
table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout);
            PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
            tableLayout.Break = PdfLayoutBreakType.FitElement;
            tableLayout.Layout = PdfLayoutType.Paginate;
            PdfLayoutResult result = table.Draw(page, new PointF(0, y), tableLayout);
            y = result.Bounds.Bottom + 5;
[VB.NET]
table.BeginRowLayout += New BeginRowLayoutEventHandler(table_BeginRowLayout)
Dim tableLayout As New PdfTableLayoutFormat()
tableLayout.Break = PdfLayoutBreakType.FitElement
tableLayout.Layout = PdfLayoutType.Paginate
Dim result As PdfLayoutResult = table.Draw(page, New PointF(0, y), tableLayout)
y = result.Bounds.Bottom + 5

Spire.PDF for .NET is a PDF api that enables users to create, edit, read and handle PDF files in .NET applications.

page 277