Copy link to clipboard
Copied
Hello everyone, I noticed such a problem: after hiding comments in a document using code, the file size increases tenfold!
function Hide() {
this.delay = true;
this.syncAnnotScan();
var annots = this.getAnnots();
if (annots==null || annots.length==0) {
app.alert("there are no comments in this document", 1);
return;}
for (var i=annots.length-1; i>=0; i--) {
annots[i].setProps({readOnly: false, lock: false});
annots[i].hidden = true;}
this.delay = false;
app.alert("all comments hide",3);
}
After optimizing(File - Save as Other - Optimizide PDF) the document, the file size returns to its original size, but after displaying comments and saving, it again grows in size by + 3 MB, and so on each time after the hide/show/save cycle.
How to solve this problem? add file optimization to the main code? Or is there an option in the program settings that will clear this automatically?
Copy link to clipboard
Copied
Use Save As, rather than a simple Save. No need to optimise. Save alone always makes a document longer because changes are added to the end, leaving the original bytes untouched.
Copy link to clipboard
Copied
Thanks for the answer, but unfortunately it doesn't work for me. Maybe you know a function in the code that does this for the user?
Copy link to clipboard
Copied
There's no direct command in JS to optimize a file. The closest you can get to it is to run a Preflight Profile that does it.
Copy link to clipboard
Copied
Are you doing the save from the menu, or in code? What you posted does not save.
It is the specific job of SAVE AS to both ask for a new name AND remove unused objects, rearrange the file, and reclaim space.
Copy link to clipboard
Copied
after hiding the comments with the code, I save the file through the menu. Well, I most likely asked the question incorrectly, I will correct it: is there a function in the code similar to the Optimization button (I did not find it in the API)? I need that when my Hide button is pressed, the document is cleared at the same time, and the user does not need to do Save As.
Copy link to clipboard
Copied
> is there a function in the code similar to the Optimization button (I did not find it in the API)?
- No.
Copy link to clipboard
Copied
All the "space saving" functions work only as the file is saved; it's a complete rearrangement of the file, and the copy in memory used while you edit isn't what counts. You would need to tell your user to do a Save As.
I don't know if this is possible (someone may have a better idea than me) but I wonder if the comments could be on a layer (optional content) that is hidden or shown. This would have a very small impact on the number of objects changed, as well as giving the user control themselves.
Oh and one more thought. The increase in size is more than I'd expect, doubling is the most I would have predicted. Please check whether your code is being called multiple times - each one will make a new copy of the annots, and all copies will be saved with an ordinary save. Perhaps add an app.alert for testing purposes.
Copy link to clipboard
Copied
(Sorry, I see you have an app.alert - if you see the message only once it isn't the problem!)