Tutorials

How to Edit PDF Pages in C#

By ComPDFKit | Thu. 14 Nov. 2024
C#Document Editor

Digital documents make it possible to edit pages. Just imagine that you are facing a paper document, and how can you manage these pages? How to delete an unneeded page? Probably the only way is to tear the page from the document. And it is more impossible for rotating pages, exchanging pages, etc. While when it comes to digital files, pages can be edited effortlessly with just a few lines of code. In this blog, we will introduce different operations on PDF pages with ComPDFKit PDF SDK.

 

 

Reasons for Editing Pages

    

If you are a teacher, only online lessons are available during the COVID-19. After class, you need to assign homework that is contained in a PDF file to students. However, only several pages need to be assigned. Then you can extract the specified pages rather than sending the whole file.

 

When scanning images, a stack of documents or a book, it might happen that you notice several pages have been rotated by 180° —  they were accidentally scanned upside down. Instead of repeating the scan, you can easily rotate the related pages to make it a normal PDF file, which greatly saves your time and simplifies your work.

 

Different PDF documents have different page patterns. So it’s a common phenomenon that there are several different page sizes in a merged PDF document. You can crop the pages to make their sizes uniform by setting the length and width of the crop area.

 

 

Specific Types of Editing Pages

 

Page editing is just a general term, in fact it also includes multiple specific functions like deleting, inserting, moving, etc. Let’s go through them one by one.

 

Delete pages

Deleting pages is a basic operation. You can arbitrarily choose the pages you want to delete, whether it is a single page, several random pages, or several pages in a continuous range. To delete pages from a PDF document, use function CPDFDocument.RemovePages(int[] pageIndexs). The following code shows how to delete page 3~5.

 

CPDFDocument document = CPDFDocument.InitWithFilePath("***");
int[] pages = { 2, 3, 4 };
document.RemovePages(pages);
document.WriteToLoadedPath();
document.Release();

 

Insert pages 

You can insert pages anywhere in the document. One page or several pages are both supported. The following code shows how to insert a blank page in front of page 3.

 

CPDFDocument document = CPDFDocument.InitWithFilePath("***");
document.InsertPage(2,600,800,null);
document.WriteToLoadedPath();
document.Release();

 

Move pages

The ability to move PDF pages matters for long document revisions or PDF presentations where content order is especially important. To move a page to a new location, use function CPDFDocument.MovePage(int startIndex, int endIndex). The following code shows how to move page 3~5 to the front of page 7.

 

CPDFDocument document = CPDFDocument.InitWithFilePath("***");
document.MovePage(4, 5);
document.MovePage(3, 4);
document.MovePage(2, 3);
document.WriteToLoadedPath();
document.Release();

 

Rotate pages

Pages can be rotated 90, 180, 270, or 360 degrees by this feature. You can choose to rotate right or rotate left, which is equivalent to clockwise or counterclockwise. To rotate a page in a PDF document, refer to the method in the CPDFPage class. The following code shows how to rotate page 3 by 90 degrees counterclockwise.

 

CPDFDocument document = CPDFDocument.InitWithFilePath("***");
document.RotatePage(2,-1);
document.WriteToLoadedPath();
document.Release();

 

Exchange pages

Exchanging pages occurs within a document, which means exchanging the location of two pages.

 

To exchange the location of two document pages, use function CPDFDocument.ExchangePage(int firstIndex, int secondIndex). The following code shows how to exchange page 3 and page 5.

 

CPDFDocument document = CPDFDocument.InitWithFilePath("***");
document.ExchangePage(2,4);
document.WriteToLoadedPath();
document.Release();

 

Replace pages

Replacing pages occurs between two documents. Using the following two functions, you can replace original document pages with new pages from a different document, and the original pages will be deleted. 

         - CPDFDocument.RemovePages(int[] pageIndexs)

         - CPDFDocument.ImportPagesAtIndex(CPDFDocument otherDocument, string pageRange,int pageIndex)

 

The following code shows how to replace page 3 in document A with page 5 in document B.

CPDFDocument document = CPDFDocument.InitWithFilePath("***");
CPDFDocument otherDocument = CPDFDocument.InitWithFilePath("***");
int[] deletePage = { 2 };
document.RemovePages(deletePage);
document.ImportPagesAtIndex(otherDocument,"4",2);
document.WriteToLoadedPath();
otherDocument.Release();
document.Release();

 

 

Related Operations

 

Apart from the functions mentioned above, there are many other features you can use to manage PDF pages:

         - PDF manipulation includes split pages, extract pages, and merge pages.

         - Document information setting.

         - Extract images.

If you are interested in these operations, please visit our guides to learn more.

 

 

Conclusion

 

In this blog, we introduced all specific features of editing pages. Hope it will help you have a deeper understanding of editing pages. If you are interested in this function, please click here to get a free trial and technology support.