Skip to content

Instantly share code, notes, and snippets.

@ernes32
Created May 21, 2020 11:49
Show Gist options
  • Save ernes32/44f27a7d66b97ed831212fedd268be5d to your computer and use it in GitHub Desktop.
Save ernes32/44f27a7d66b97ed831212fedd268be5d to your computer and use it in GitHub Desktop.
$("#run").click(getDocumentAsCompressed);
function getDocumentAsCompressed() {
// var type = Office.FileType.Compressed;
console.log("CompressedFile status: " + Office.context.requirements.isSetSupported("CompressedFile"));
console.log("PdfFile status: " + Office.context.requirements.isSetSupported("PdfFile"));
console.log("TextFile status: " + Office.context.requirements.isSetSupported("TextFile"));
var type = Office.FileType.Pdf;
Office.context.document.getFileAsync(type, { sliceSize: 65536 /*64 KB*/ }, function (result) {
console.log("getFileAsync status: " + result.status + " type: " + type);
if (result.status == Office.AsyncResultStatus.Succeeded) {
// If the getFileAsync call succeeded, then
// result.value will return a valid File Object.
var myFile = result.value;
var sliceCount = myFile.sliceCount;
var slicesReceived = 0,
gotAllSlices = true,
docdataSlices = [];
console.log("File size:" + myFile.size + " #Slices: " + sliceCount);
// Get the file slices.
getSliceAsync(myFile, 0, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
} else {
console.log("Error: " + result.error.message);
}
});
}
function getSliceAsync(file, nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived) {
file.getSliceAsync(nextSlice, function (sliceResult) {
if (sliceResult.status == "succeeded") {
if (!gotAllSlices) {
// Failed to get all slices, no need to continue.
return;
}
// Got one slice, store it in a temporary array.
// (Or you can do something else, such as
// send it to a third-party server.)
docdataSlices[sliceResult.value.index] = sliceResult.value.data;
if (++slicesReceived == sliceCount) {
// All slices have been received.
file.closeAsync();
onGotAllSlices(docdataSlices);
} else {
getSliceAsync(file, ++nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
}
} else {
gotAllSlices = false;
file.closeAsync();
console.log("getSliceAsync Error: " + sliceResult.error.message);
}
});
}
function onGotAllSlices(docdataSlices) {
var docdata = [];
for (var i = 0; i < docdataSlices.length; i++) {
docdata = docdata.concat(docdataSlices[i]);
}
var bytes = new Uint8Array(docdata.length);
for (var i = 0; i < docdata.length; i++) {
bytes[i] = docdata[i];
}
var blob = new Blob([bytes], { type: "application/pdf" });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment