private async void Button_Click(object sender, RoutedEventArgs e)
{
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("SplitPDF.Assets.sample.pdf");
//load the existing document
PdfLoadedDocument ldoc = new PdfLoadedDocument(docStream);
List<MemoryStream> ms = new List<MemoryStream>();
for (int i = 0; i < ldoc.Pages.Count; i++)
{
//Create a new Pdf document
PdfDocument doc = new PdfDocument();
//import the page
doc.ImportPage(ldoc, i);
MemoryStream file = new MemoryStream();
//save the document
doc.Save(file);
ms.Add(file);
//Close the document
doc.Close(true);
}
Save(ms);
}
private async void Save(List<MemoryStream> stream)
{
//select the folder to store the PDF files.
FolderPicker picker = new FolderPicker();
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
picker.FileTypeFilter.Add(".pdf");
StorageFolder stFile = await picker.PickSingleFolderAsync();
//save the PDF document
for (int i = 0; i < stream.Count;i++)
{
StorageFile file = await stFile.CreateFileAsync("sample"+i+".pdf",Windows.Storage.CreationCollisionOption.ReplaceExisting);
stream[i].Position = 0;
var buffer = new byte[(int)stream[i].Length];
await stream[i].ReadAsync(buffer, 0, buffer.Length);
await Windows.Storage.FileIO.WriteBytesAsync(file, buffer);
}
}
|