Q: How do I save a file using javascript with PDFNet for Windows Store apps.
A: The functions below describe a hypothetical save button and save as button.
Note that pdfDoc is a global variable of the current document, and pdfDocStream is the current variable for the stream that is opened as the doc.
function save_click()
{
if (!pdfDoc)
{
return;
}
pdfDoc.saveAsync().then(function () {
});
return;
}
function saveAs_click()
{
if (!pdfDoc) {
return;
}
// Create the picker object and set options
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.viewMode = Windows.Storage.Pickers.PickerViewMode.list;
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
savePicker.fileTypeChoices.insert("PDF", [".pdf"]);
// Open the picker for the user to pick a file
savePicker.pickSaveFileAsync().then(function (file) {
if (file) {
file.openAsync(Windows.Storage.FileAccessMode.readWrite).then
(
function (filestream) {
var filter = new pdftron.Filters.RandomAccessStreamFilter(filestream);
pdfDoc.saveAsync(filter, pdftron.SDF.SDFDocSaveOptions.e_remove_unused).then(function () {
if (pdfDocStream) { // close the old stream
pdfDocStream.close();
}
pdfDocStream = filestream;
});
}
);
} else {
// file didn't load.
}
});
}