Syncfusion Feedback


Trusted by the world’s leading companies

Overview

The Syncfusion .NET MAUI PDF library that allows users to split a PDF file into multiple sub-documents quickly and accurately. It also allows users to split, combine, import, and append PDFs.

Split PDF .NET MAUI


How to split PDF files using .NET MAUI

  1. Install the Syncfusion.Pdf.Net NuGet package in your project.
  2. Create FileStream objects to represent input PDF files.
  3. Create a PdfLoadedDocument object by passing the FileStream object.
  4. Create a new PDF document using the PdfDocument class.
  5. Use the ImportPage method of the PdfDocumentBase class to split a PDF document into multiple files.
  6. Save the PdfDocument object to the FileStream object.

Here is an example of how to split PDF files using the Syncfusion .NET MAUI PDF library. You can split a PDF file into a single page or multiple page PDF document with just a few lines of code.

  1. //Open an existing PDF document.
  2. Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;
  3. Stream docStream = assembly.GetManifestResourceStream("Input.pdf");
  4. //Load an existing PDF document
  5. PdfLoadedDocument document = new PdfLoadedDocument(docStream);
  6.  
  7. //Iterate over the pages
  8. for (int pageIndex = 0; pageIndex < document.PageCount; pageIndex++)
  9. {
  10. //Create a new PdfDocument object
  11. using (PdfDocument outputDocument = new PdfDocument())
  12. {
  13. //Import the page from the loadedDocument to the outputDocument
  14. outputDocument.ImportPage(document, pageIndex);
  15. string filePath = "Result" + pageIndex + ".pdf";
  16. //Save the document
  17. using (FileStream outputFileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  18. {
  19. outputDocument.Save(outputFileStream);
  20. }
  21. }
  22. }

PDF split options

Split a range of pages into a separate PDF document

A range of pages can be split into a separate PDF document.

  1. //Open an existing PDF document.
  2. Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;
  3. Stream inputFileStream = assembly.GetManifestResourceStream("Input.pdf");
  4. using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream))
  5. {
  6. int[,] values = new int[,] { { 0, 2 }, { 2, 4 } };
  7. loadedDocument.DocumentSplitEvent += (object sender, PdfDocumentSplitEventArgs args) =>
  8. {
  9. string filePath = Guid.NewGuid().ToString()+".pdf";
  10. //Save the resulting document.
  11. FileStream outputStream = new FileStream(filePath, FileMode.CreateNew);
  12. args.PdfDocumentData.CopyTo(outputStream);
  13. outputStream.Close();
  14. };
  15. //Spit the document by ranges.
  16. loadedDocument.SplitByRanges(values);
  17. }

Import or delete certain pages from a PDF file

Users can get specific pages or delete certain pages from a PDF document.

  1. //Open an existing PDF document.
  2. Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;
  3. Stream docStream = assembly.GetManifestResourceStream("Input.pdf");
  4. //Load the PDF document from stream
  5. using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
  6. {
  7. //Creates a new PDF document
  8. using (PdfDocument outputDocument = new PdfDocument())
  9. {
  10. //Remove the first page in the PDF document
  11. loadedDocument.Pages.RemoveAt(0);
  12. //Import certain pages to the new PDF document
  13. outputDocument.ImportPage(loadedDocument, 0);
  14. //Give file path
  15. string filePath = "Split.pdf";
  16. //Save the document into a filestream object
  17. using (FileStream outputFileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  18. {
  19. outputDocument.Save(outputFileStream);
  20. }
  21. }
  22. }

Split a document based on bookmarks

Split a PDF document based on bookmarks.

  1. //Open an existing PDF document.
  2. Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;
  3. Stream docStream = assembly.GetManifestResourceStream("Input.pdf");
  4. //Load the PDF document from stream
  5. using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
  6. {
  7. //Create bookmark object
  8. PdfBookmarkBase bookmarks = loadedDocument.Bookmarks;
  9. //Load the PDF document from stream
  10. foreach (PdfBookmark bookmark in bookmarks)
  11. {
  12. if (bookmark.Destination != null)
  13. {
  14. if (bookmark.Destination.Page != null)
  15. {
  16. int endIndex = bookmark.Destination.PageIndex;
  17. //Create a new PDF document
  18. using (PdfDocument document = new PdfDocument())
  19. {
  20. foreach (PdfLoadedBookmark childBookmark in bookmark)
  21. {
  22. endIndex = childBookmark.Destination.PageIndex;
  23. }
  24. //Import the pages to the new PDF document
  25. document.ImportPageRange(loadedDocument, bookmark.Destination.PageIndex, endIndex);
  26. //Give file path
  27. string filePath = bookmark.Title +".pdf";
  28. //Save the PDF file with the bookamark title's name.
  29. using (FileStream outputFileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  30. {
  31. document.Save(outputFileStream);
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }




Awards

Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion® is proud to hold the following industry awards.

Scroll up icon
Chat with us