Syncfusion Feedback


Trusted by the world’s leading companies

Overview

The Syncfusion Xamarin PDF library 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 Xamarin


How to split PDF files using Xamarin

  1. Install the Syncfusion.Pdf.Net.Core 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 in C# using the Syncfusion Xamarin PDF library. Users can split a PDF file into a single page or multiple-page PDF document with just a few lines of code.

  1. //Create a zip file
  2. Syncfusion.Compression.Zip.ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();
  3. //Load an existing PDF document
  4. Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Input.pdf");
  5. //Load the PDF document from stream
  6. using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
  7. {
  8. //Iterate over the pages
  9. for (int pageIndex = 0; pageIndex < loadedDocument.PageCount; pageIndex++)
  10. {
  11. //Create a new PdfDocument object
  12. using (PdfDocument outputDocument = new PdfDocument())
  13. {
  14. //Import the page from the loadedDocument to the outputDocument
  15. outputDocument.ImportPage(loadedDocument, pageIndex);
  16. //Save the document into a memory object
  17. MemoryStream ms = new MemoryStream();
  18. outputDocument.Save(ms);
  19. zipArchive.AddItem("Files" + pageIndex + ".pdf", ms, false, Syncfusion.Compression.FileAttributes.Normal);
  20. ms.Position = 0;
  21. }
  22. }
  23. //Zip the filename
  24. MemoryStream memoryStream = new MemoryStream();
  25. zipArchive.Save(memoryStream, false);
  26. //Save the stream as a file in the device and invoke it for viewing
  27. Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("SplitDocuments.zip", "application/zip", memoryStream);
  28. }

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. //Create a zip file
  2. Syncfusion.Compression.Zip.ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();
  3. //Load an existing PDF document
  4. Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Input.pdf");
  5. //Load the PDF document from stream
  6. using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
  7. {
  8. //Subscribe to the document split event.
  9. loadedDocument.DocumentSplitEvent += LoadDocument_DocumentSplitEvent;
  10. //Spit the document by ranges
  11. loadedDocument.SplitByRanges(new int[,] { { 0, 2 }, { 2, 4 } });
  12. }
  13. //Zip the filename
  14. MemoryStream memoryStream = new MemoryStream();
  15. zipArchive.Save(memoryStream, false);
  16. //Save the stream as a file in the device and invoke it for viewing
  17. Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("SplitDocuments.zip", "application/zip", memoryStream);
  18.  
  19. void LoadDocument_DocumentSplitEvent(object sender, PdfDocumentSplitEventArgs args)
  20. {
  21. //Save the resulting document
  22. MemoryStream ms = new MemoryStream();
  23. args.PdfDocumentData.CopyTo(ms);
  24. zipArchive.AddItem(Guid.NewGuid().ToString() + ".pdf", ms, false, System.IO.FileAttributes.Normal);
  25. }

Import or delete certain pages from a PDF file

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

  1. //Create a zip file
  2. Syncfusion.Compression.Zip.ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();
  3. //Load an existing PDF document
  4. Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Input.pdf");
  5. //Load the PDF document from stream
  6. using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
  7. {
  8.  
  9. //Create a new PdfDocument object
  10. using (PdfDocument outputDocument = new PdfDocument())
  11. {
  12. //Remove the first page in the PDF document
  13. loadedDocument.Pages.RemoveAt(0);
  14. //Import certain pages to the new PDF document
  15. outputDocument.ImportPage(loadedDocument, 0);
  16. //Save the document into a memory object
  17. MemoryStream ms = new MemoryStream();
  18. outputDocument.Save(ms);
  19. zipArchive.AddItem("Files.pdf", ms, false, Syncfusion.Compression.FileAttributes.Normal);
  20. ms.Position = 0;
  21. }
  22. //Zip the filename
  23. MemoryStream memoryStream = new MemoryStream();
  24. zipArchive.Save(memoryStream, false);
  25. //Save the stream as a file in the device and invoke it for viewing
  26. Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("SplitDocuments.zip", "application/zip", memoryStream);
  27. }

Split a document based on bookmarks

Split a PDF document based on bookmarks.

  1. //Create a zip file
  2. Syncfusion.Compression.Zip.ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();
  3. //Load an existing PDF document
  4. Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Input.pdf");
  5. //Load the PDF document from stream
  6. using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
  7. {
  8. //Create a bookmark object
  9. PdfBookmarkBase bookmarks = loadedDocument.Bookmarks;
  10. //Load the PDF document from stream
  11. foreach (PdfBookmark bookmark in bookmarks)
  12. {
  13. if (bookmark.Destination != null)
  14. {
  15. if (bookmark.Destination.Page != null)
  16. {
  17. int endIndex = bookmark.Destination.PageIndex;
  18. //Create a new PDF document
  19. using (PdfDocument document = new PdfDocument())
  20. {
  21. foreach (PdfLoadedBookmark childBookmark in bookmark)
  22. {
  23. endIndex = childBookmark.Destination.PageIndex;
  24. }
  25. //Import the pages to the new PDF document
  26. document.ImportPageRange(loadedDocument, bookmark.Destination.PageIndex, endIndex);
  27. //Save the document into a memory object
  28. MemoryStream ms = new MemoryStream();
  29. document.Save(ms);
  30. zipArchive.AddItem(bookmark.Title, ms, false, Syncfusion.Compression.FileAttributes.Normal);
  31. ms.Position = 0;
  32. }
  33. }
  34. }
  35. }
  36. //Zip the filename
  37. MemoryStream memoryStream = new MemoryStream();
  38. zipArchive.Save(memoryStream, false);
  39. //Save the stream as a file in the device and invoke it for viewing
  40. Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("SplitDocuments.zip", "application/zip", memoryStream);
  41. }




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