PDFtoolkit VCL
Edit, enhance, secure, merge, split, view, print PDF and AcroForms documents
Compatibility
Delphi C++Builder

How To Rearrange Pages In A PDF Document

Learn how to move pages around in a PDF document using PDFtoolkit VCL.
Mohammed Najeemudheen and Suraj C. B.

In this article, we will see how to rearrange pages in an existing PDF document. For an example, we will use a document with say 10 pages. We will try to move page 2 to the position of page 5.

First, we use the ExtractPagesTo() method of TgtPDFDocument class to extract Page 2. Next, we delete Page 2 from the document using DeletePages() method. Finally, we copy the extracted page from the temporary document to location of Page 5 in the original document using the method InsertPagesFrom().

var
 LFrom: String;
 LToPage: Integer;
 LTempDoc, PDFDoc: TgtPDFDocument;
 Lstream:TMemoryStream;
begin
 try
  PDFDoc := TgtPDFDocument.Create(Nil);

  // Load input PDF document
  PDFDoc.LoadFromFile('Input.pdf');

  // Disable interactivity
  PDFDoc.ShowSetupDialog := False;
  PDFDoc.OpenAfterSave := False;

  // Create a memory stream
  Lstream :=TMemoryStream.Create;

  // Create a temporary PDF document object
  LTempDoc := TgtPDFDocument.Create(self);

  LTempDoc.ShowSetupDialog := False;
  LTempDoc.OpenAfterSave := False;

  // Page position from a page
  // needs to be moved
  LFrom := '2';

  // Page position to which a page
  // needs to moved
  LToPage := 5;

  if LToPage <= PDFDoc.PageCount then
   begin
    
    // Extract page 1
    // to a temporary document
    PDFDoc.ExtractPagesTo(LTempDoc,LFrom);

    // Save extracted page to a memory stream
    LTempDoc.SaveToStream(Lstream);

    // Delete extracted page from input
    // PDF document
    PDFDoc.DeletePages(LFrom);

    // Load the temporary PDF document
    LTempDoc.LoadFromStream(Lstream);

    // Insert the extracted page (currently
    // page 1 in the temporary document) at 
    // position page 5
    PDFDoc.InsertPagesFrom(LTempDoc,'1',LToPage-1);

    // Save modified document
    PDFDoc.SaveToFile('Output.pdf');
   end
  else
   ShowMessage('Page out of range ! Max Page =
               '+IntToStr(PDFDoc.PageCount));
 finally
  LTempDoc.free;
  Lstream.Free;
  PDFDoc.Free;
end;

You may have wondered why we are saving the temporary document object to a memory stream. When we call ExtractPagesTo(), it does retrieve Page 2 to the LTempDoc object, holding just a reference to that page but not yet representing the structure of a wholesome PDF document.

For this reason, we save it to a memory stream and load it again from that memory stream. When we do this, LTempDoc represents a wholesome independent PDF document with a very retrievable Page 1 (originally Page 2), which we can then insert at position of Page 5.

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2017 Gnostice Information Technologies Private Limited. All rights reserved.