Пример #1
0
        private async void SaveButton_OnClick(object sender, RoutedEventArgs e)
        {
            string filename = "output.pdf";
            Stream stream   = pdfViewerControl.Save();

            stream.Position = 0;

            FileSavePicker savePicker = new FileSavePicker
            {
                DefaultFileExtension = ".pdf",
                SuggestedFileName    = filename
            };

            savePicker.FileTypeChoices.Add("Adobe PDF Document", new List <string>()
            {
                ".pdf"
            });
            StorageFile stFile = await savePicker.PickSaveFileAsync();

            if (stFile != null)
            {
                Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);

                Stream streamForWrite = fileStream.AsStreamForWrite();
                streamForWrite.SetLength(0);
                streamForWrite.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
                streamForWrite.Flush();
                streamForWrite.Dispose();
                fileStream.Dispose();
                MessageDialog msgDialog = new MessageDialog("File has been saved successfully.");
                IUICommand    cmd       = await msgDialog.ShowAsync();
            }
        }
Пример #2
0
        private async void SaveAsFileViewerCommand()
        {
            Stream stream = pdfViewer.Save();

            stream.Position = 0;

            FileSavePicker savePicker = new FileSavePicker();

            savePicker.DefaultFileExtension = ".pdf";
            savePicker.FileTypeChoices.Add("Adobe PDF Document", new List <string>()
            {
                ".pdf"
            });
            StorageFile stFile = await savePicker.PickSaveFileAsync();

            if (stFile != null)
            {
                Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);

                Stream st = fileStream.AsStreamForWrite();
                st.SetLength(0);
                st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
                st.Flush();
                st.Dispose();
                fileStream.Dispose();
            }
        }
Пример #3
0
        async void Save(Stream stream, string filename)
        {
            stream.Position = 0;

            StorageFile stFile;

            if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
            {
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.DefaultFileExtension = ".pdf";
                savePicker.SuggestedFileName    = "Sample";
                savePicker.FileTypeChoices.Add("Adobe PDF Document", new List <string>()
                {
                    ".pdf"
                });
                stFile = await savePicker.PickSaveFileAsync();
            }
            else
            {
                StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
                stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
            }
            if (stFile != null)
            {
                Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);

                Stream st = fileStream.AsStreamForWrite();
                st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
                st.Flush();
                st.Dispose();
                fileStream.Dispose();
            }
        }
Пример #4
0
        private async void SaveDocx(WordDocument doc)
        {
            StorageFile stgFile;

            if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
            {
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                // Dropdown of file types the user can save the file as
                savePicker.FileTypeChoices.Add("Word Document", new List <string>()
                {
                    ".docx"
                });
                // Default file name if the user does not type one in or select a file to replace
                savePicker.SuggestedFileName = "Sample";
                stgFile = await savePicker.PickSaveFileAsync();
            }
            else
            {
                StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
                stgFile = await local.CreateFileAsync("WordDocument.docx", CreationCollisionOption.ReplaceExisting);
            }
            if (stgFile != null)
            {
                MemoryStream stream = new MemoryStream();
                await doc.SaveAsync(stream, FormatType.Docx);

                doc.Close();
                stream.Position = 0;
                Windows.Storage.Streams.IRandomAccessStream fileStream = await stgFile.OpenAsync(FileAccessMode.ReadWrite);

                Stream st = fileStream.AsStreamForWrite();
                st.SetLength(0);
                st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
                st.Flush();
                st.Dispose();
                fileStream.Dispose();

                //Save as Docx Format

                MessageDialog msgDialog = new MessageDialog("Do you want to view the Document?", "File has been created successfully.");
                UICommand     yesCmd    = new UICommand("Yes");
                msgDialog.Commands.Add(yesCmd);
                UICommand noCmd = new UICommand("No");
                msgDialog.Commands.Add(noCmd);
                IUICommand cmd = await msgDialog.ShowAsync();

                if (cmd == yesCmd)
                {
                    // Launch the retrieved file
                    bool success = await Windows.System.Launcher.LaunchFileAsync(stgFile);
                }
            }
        }
Пример #5
0
        public static async void Save(Stream stream, string filename)
        {
            stream.Position = 0;

            StorageFile stFile;

            if (!Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
            {
                FileSavePicker savePicker = new FileSavePicker {
                    DefaultFileExtension = ".pdf",
                    SuggestedFileName    = filename
                };
                savePicker.FileTypeChoices.Add("Adobe PDF Document", new List <string>()
                {
                    ".pdf"
                });
                stFile = await savePicker.PickSaveFileAsync();
            }
            else
            {
                StorageFolder local = ApplicationData.Current.LocalFolder;
                stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
            }
            if (stFile != null)
            {
                Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);

                Stream st = fileStream.AsStreamForWrite();
                st.SetLength(0);
                st.Write((stream as MemoryStream)?.ToArray(), 0, (int)stream.Length);
                st.Flush();
                st.Dispose();
                fileStream.Dispose();
                MessageDialog msgDialog = new MessageDialog("Do you want to view the Document?", "File created.");
                UICommand     yesCmd    = new UICommand("Yes");
                msgDialog.Commands.Add(yesCmd);
                UICommand noCmd = new UICommand("No");
                msgDialog.Commands.Add(noCmd);
                IUICommand cmd = await msgDialog.ShowAsync();

                if (cmd == yesCmd)
                {
                    // Launch the retrieved file
                    bool success = await Windows.System.Launcher.LaunchFileAsync(stFile);
                }
            }
        }
Пример #6
0
        private async void SaveAsync()
        {
            folder = ApplicationData.Current.LocalFolder;
            file   = await folder.CreateFileAsync("SavedDocument.pdf", CreationCollisionOption.ReplaceExisting);

            if (file != null)
            {
                Windows.Storage.Streams.IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite);

                Stream st = fileStream.AsStreamForWrite();
                st.SetLength(0);
                st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
                st.Flush();
                st.Dispose();
                fileStream.Dispose();
            }
        }
Пример #7
0
        private async void button1_Click(object sender, RoutedEventArgs e)
        {
            StorageFile    stFile     = null;
            FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            openPicker.ViewMode = PickerViewMode.List;
            openPicker.FileTypeFilter.Add(".pdf");

            stFile = await openPicker.PickSingleFileAsync();

            SourcePDF.Text = stFile.Name;
            if (stFile != null)
            {
                Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);

                documentStream = fileStream.AsStreamForWrite();
            }
        }
Пример #8
0
        private async Task <StorageFolder> SaveAsync(string name)
        {
            FolderPicker picker = new FolderPicker {
                SuggestedStartLocation = PickerLocationId.ComputerFolder
            };

            picker.FileTypeFilter.Add("*");
            StorageFolder folder = await picker.PickSingleFolderAsync();

            if (folder != null)
            {
                using (UserDialogs.Instance.Loading("Téléchargement du document", null, null, true))
                {
                    file = await folder.CreateFileAsync(name, CreationCollisionOption.GenerateUniqueName);

                    if (file != null)
                    {
                        Windows.Storage.Streams.IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite);

                        Stream st = fileStream.AsStreamForWrite();
                        st.SetLength(0);
                        st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
                        st.Flush();
                        st.Dispose();
                        fileStream.Dispose();
                        return(folder);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            else
            {
                return(null);
            }
        }
Пример #9
0
        private async Task <StorageFolder> extractFiles(StorageFile zipfilename)
        {
            StorageFolder storfolder = null;

            try
            {
                // Create stream for compressed files in memory
                using (MemoryStream zipMemoryStream = new MemoryStream())
                {
                    using (Windows.Storage.Streams.IRandomAccessStream zipStream = await zipfilename.OpenAsync(FileAccessMode.Read))
                    {
                        // Read compressed data from file to memory stream
                        using (Stream instream = zipStream.AsStreamForRead())
                        {
                            byte[] buffer = new byte[1024];
                            while (instream.Read(buffer, 0, buffer.Length) > 0)
                            {
                                zipMemoryStream.Write(buffer, 0, buffer.Length);
                            }
                        }
                    }
                    storfolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(zipfilename.DisplayName, CreationCollisionOption.GenerateUniqueName);

                    // Create zip archive to access compressed files in memory stream
                    using (ZipArchive zipArchive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Read))
                    {
                        // For each compressed file...
                        foreach (ZipArchiveEntry entry in zipArchive.Entries)
                        {
                            // ... read its uncompressed contents
                            using (Stream entryStream = entry.Open())
                            {
                                if (entry.Name != "")
                                {
                                    string fileName = entry.FullName.Replace("/", @"\");
                                    byte[] buffer   = new byte[entry.Length];
                                    entryStream.Read(buffer, 0, buffer.Length);

                                    // Create a file to store the contents
                                    StorageFile uncompressedFile = await storfolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

                                    // Store the contents
                                    using (Windows.Storage.Streams.IRandomAccessStream uncompressedFileStream = await uncompressedFile.OpenAsync(FileAccessMode.ReadWrite))
                                    {
                                        using (Stream outstream = uncompressedFileStream.AsStreamForWrite())
                                        {
                                            outstream.Write(buffer, 0, buffer.Length);
                                            outstream.Flush();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                //this.pbar.IsIndeterminate = false;
                //this.Add.IsEnabled = true;
                //this.imageGrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                if (storfolder != null)
                {
                    await storfolder.DeleteAsync();
                }
                return(null);
            }
            finally
            {
                zipfilename.DeleteAsync();
            }
            return(storfolder);
        }