IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc  SOLVED

PDF-XChange Editor SDK for Developers

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
zarkogajic
User
Posts: 1370
Joined: Thu Sep 05, 2019 12:35 pm

IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by zarkogajic »

Hi Support,

I'm moving this to a new separate topic, as this is really not related to the original question, ref: https://forum.pdf-xchange.com/viewtopic.php?f=66&t=35186#p150385

From what I understand when the e.document.preSaveOptimizedData ends, the temporary (optimized) file is saved to (overwritten, if exists) DestFileName (then temporary one is deleted). For this, DestFileName must not exist or be opened / locked.
...Also you may use the TempOptimizedDoc.SrcInfo.File to read/write it...
The issue I am facing is that I cannot really write my edits to the temporary (optimized) file, "TempOptimizedDoc.SrcInfo.FileName", as it is locked.

Note: I'm not linearizing the file via your SDK / IPXC methods - I'm using a 3rd party library.

I can linearize a file from the file system (read, linearize, write) - meaning the file must not be locked. That's why I asked for "e.document.afterSave" - to linearize when no longer locked by PDFX.

I can also linearize from memory (buffer). To linearize from memory I can use IPXC_Document.WriteTo(IStream) - this works. Then I can either write back to a buffer (IStream) or write to a file.

Writing to a buffer is a no-go, since there's no IPXC_Document.ReadFrom(IStream) - having such a method would solve it.

After buffer linearization I could write to a different temporary file, but since TempOptimizedDoc is read-only I cannot change it to my linearized file (and let your code simply save/move it to where it needs to go: DestFileName).

So, I'm stuck again.

Or, I do not understand what you meant by "you may use the TempOptimizedDoc.SrcInfo.File to read/write it".



-žarko
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2351
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by Vasyl-Tracker Dev Team »

Hi žarko.

From what I understand when the e.document.preSaveOptimizedData ends, the temporary (optimized) file is saved to (overwritten, if exists) DestFileName (then temporary one is deleted). For this, DestFileName must not exist or be opened / locked.
Incorrect. When you receive this event you have:

1. TempOptimizedDoc - is true temp. document that is connected to the temp. file located in user's temp. folder and it is is result of optimization of the original document. This temp. file will be deleted after closing this doc. This doc will be closed by app after sending the preSaveOptimizedData event.
2. DestFileName - is file name specified by user in SaveAs dialog. After sending the preSaveOptimizedData event the app copies all data from TempOptimizedDoc.SrcInfo.File to the DestFileName-file. And then, if OriginalDoc.SrcInfo.FileName==DestFileName (i.e, user decided to overwrite the original doc by the optimized version) - app reopens the original doc from updated source file.
The issue I am facing is that I cannot really write my edits to the temporary (optimized) file, "TempOptimizedDoc.SrcInfo.FileName", as it is locked.
Sure, because this file it is blocked by opened TempOptimizedDoc. But you can read-write data via TempOptimizedDoc.SrcInfo.File object (or via stream-object from TempOptimizedDoc.SrcInfo.File.GetStream).

Otherwise you may use the following way:

1. Make another temp. copy of TempOptimizedDoc.SrcInfo.FileName
2. Change this copy-file by your pdf-linearization API.
3. Refill the TempOptimizedDoc.SrcInfo.File from this copy-file.
Or, I do not understand what you meant by "you may use the TempOptimizedDoc.SrcInfo.File to read/write it".
TempOptimizedDoc.SrcInfo.File - is the stream-like object(IAFS_File). You may use it for read or change data in corresponding file on the disk. Also you have option to use the standard stream-object obtained from TempOptimizedDoc.SrcInfo.File.GetStream, if your 3rd-api supports it.


Also: in the new 354 build we will make writable the TempOptimizedDoc-property, so you will be able to:

1. event.TempOptimizedDoc.DeleteOnClose = false;
tempFileName = event.TempOptimizedDoc.SrcInfo.FileName;
event.TempOptimizedDoc.Close(); // to unlock tempFileName
2. update tempFileName by your pdf-linearization API.
3. event.TempOptimizedDoc = pxcInst.OpenDocumentFrom(tempFileName);
event.TempOptimizedDoc.DeleteOnClose = true;

Pros: the additional copy-file isn't necessary
Cons: pxcInst.OpenDocumentFrom will fail if tempFileName has password-protected pdf. You may use the op.openDoc operation instead that will ask user for password, but I guess it might look strange for him to be asked for password again during optimization process...
I can linearize a file from the file system (read, linearize, write) - meaning the file must not be locked. That's why I asked for "e.document.afterSave" - to linearize when no longer locked by PDFX.
To be clear, in case when user overwrites the original file - this file is still locked for 3rd api all time even during the e.document.afterSave event.


Cheers.
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
zarkogajic
User
Posts: 1370
Joined: Thu Sep 05, 2019 12:35 pm

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by zarkogajic »

Hi Vasyl,

Omg, I completely failed to see Read/Write methods exist for iafs_file - those two should be enough for all I need.

Btw, any code samples for those two methods?

-žarko
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2351
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by Vasyl-Tracker Dev Team »

That is easy:

Code: Select all

while (true)
{
     int64 readedBytes = 0;
     file1.Read(buf, bufSizeInBytes, out readedBytes);
     if (readedBytes == 0)
         break;
     int64 writtenBytes = 0;
     file2.Write(buf, readedBytes, out writtenBytes);
}
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
zarkogajic
User
Posts: 1370
Joined: Thu Sep 05, 2019 12:35 pm

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by zarkogajic »

Hi Vasyl,

Yes, should be straightforward, but unfortunately, Writing part seems not to work - written bytes count is 0.

Here's my code:

Code: Select all

  pPv: Pointer;
  nSize, nRead, nWrite, nNewPos: Int64;
  pErrDetails: IString;
  
  ...

  eventData.TempOptimizedDoc.SrcInfo.File_.GetSize(nSize, nil, nil);

  pPv := AllocMem(nSize);

  eventData.TempOptimizedDoc.SrcInfo.File_.Read(pPv, nSize, nRead, nil, nil);

  pErrDetails := PDFXHelper.InstAUX.CreateString('');

  //no help from this line
  //eventData.TempOptimizedDoc.SrcInfo.File_.SetPos(0, 0, nNewPos);

  //nWrite is 0 after this line
  eventData.TempOptimizedDoc.SrcInfo.File_.Write(pPv, nRead, nWrite, pErrDetails, nil);

  pErrDetails.Value; //empty so no errors reported
The nWrite is 0 after the Write method call. The pErrDetails is empty.

Am not sure if important, but: nRead (after Read) is less than nSize (after GetSize)

p.s.
Yes, I'm trying to Read/Write the same data to the same file (to test Read/Write).

p.s.
I've also tried Writing from another_iafs_file to TempOptimizedDoc.SrcInfo.File_, but still nWrite is 0 and the file is not changed.


-žarko
zarkogajic
User
Posts: 1370
Joined: Thu Sep 05, 2019 12:35 pm

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by zarkogajic »

Hi Support,

I've checked OpenFlags for TempOptimizedDoc.SrcInfo.File and they are:

AFS_OpenFile_Read + AFS_OpenFile_OpenExisting + AFS_OpenFile_ShareRead + AFS_OpenFile_Cache + AFS_OpenFile_FullCache

Seems like no writing allowed ? As a result, Write method "fails" (writes 0 bytes)?

-žarko
zarkogajic
User
Posts: 1370
Joined: Thu Sep 05, 2019 12:35 pm

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by zarkogajic »

Hi Support,

After a lot of what-to-try-next ... I've "found" the Reopen(nNewOpenFlags) method. Included AFS_OpenFile_Write. Write works!!

-žarko
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by Sasha - Tracker Dev Team »

Hello zarkogajic,

Vasyl will reply a bit later. But from what I know from practice, if using reopen and doing your stuff, you should always revert flags to the previous state.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
zarkogajic
User
Posts: 1370
Joined: Thu Sep 05, 2019 12:35 pm

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by zarkogajic »

Hi Alex, Vasyl,

Bingo! All working how I wanted it :)

Using:

1. TempOptimizedDoc.SrcInfo.File.Read(buffer)
2. Send buffer to my linearization code
3. Receive buffer from linearization
4. Write received buffer back to TempOptimizedDoc.SrcInfo.File

Case finally closed :) :)

Further:
Also: in the new 354 build we will make writable the TempOptimizedDoc-property, so you will be able to...
Given the can of worms this could open - I would never use that :) And I do not really see a need for that (except doing weird stuff nobody loves).
To be clear, in case when user overwrites the original file - this file is still locked for 3rd api all time even during the e.document.afterSave event.
:) Thanks. That answer the question I kept to ask at a later time.

-žarko
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by Sasha - Tracker Dev Team »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
zarkogajic
User
Posts: 1370
Joined: Thu Sep 05, 2019 12:35 pm

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by zarkogajic »

Hi Alex, Vasyl,

Even thought ... :) ... one more question, given that using all the above I actually do not need the "DestFileName" from IPXV_DocPreSaveOptimizedDataEvent.

Since e.document.preSaveOptimizedData is raised after the user has selected the destination file, my code runs after SaveAs dialog appears, so this looks a bit weird for larger files:

1. sdk's optimization with progress
2. Save As dialog - ask user for destination
3. my linearization with progress

The order of the above would better be 1, 3, 2.

So, if possible to (also) raise this event when sdk's optimization is done, but before asking for destination file. Meaning this event would be raised 2 times: just before and after the Save As dialog (so once with DestFileName = null and once with the value set).

What say you ?

-žarko
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2351
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by Vasyl-Tracker Dev Team »

Hi zarko.
So, if possible to (also) raise this event when sdk's optimization is done, but before asking for destination file. Meaning this event would be raised 2 times: just before and after the Save As dialog (so once with DestFileName = null and once with the value set).
Seems it is possible to do it in that way. Please wait for next build...

Cheers.
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
zarkogajic
User
Posts: 1370
Joined: Thu Sep 05, 2019 12:35 pm

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by zarkogajic »

Hi Vasyl,

Great, thanks!

Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by Sasha - Tracker Dev Team »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
zarkogajic
User
Posts: 1370
Joined: Thu Sep 05, 2019 12:35 pm

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by zarkogajic »

Hi Alex, Vasyl,

Some more thingies to consider, please.

For the moment:
Setting IPXV_DocPreSaveOptimizedDataEvent's Handled to true will only stop it from propagating to other PXVControls listening to the event.

Getting IProgressMon's Cancel flag from IID_IPXV_DocPreSaveOptimizedDataEvent.Progress and setting it to true - does not stop the event from moving forward (saving the destination file).

So, for the moment, I can "cancel" the event by *closing the TempOptimizedDoc* - if I do so, the optimized document will not be saved (to DestFileName).

It would be ideal to include a dedicated "Cancel" IFlag to allow to stop the event from moving forward - by default its value is false. If I set it to true -> "abort the entire operation".

Or in short: add an IFlag "Cancel" as a property of IID_IPXV_DocPreSaveOptimizedDataEvent.

So, a series of "events" with possible cancel scenarios:

1. sdk's optimization is done
2. IID_IPXV_DocPreSaveOptimizedDataEvent is raised with DestFileName = null
2.1. my linearization code happens here
2.2. if I set Cancel = true -> the "Save as" dialog does not appear (of course, the saving part does not happen, TempOptimizedDoc and the entire operation is ignored).
2.3. If I leave Cancel = false -> the "Save as" does appear.
3. If Save As was not canceled: IID_IPXV_DocPreSaveOptimizedDataEvent is raised with DestFileName <> null
3.1. If Cancel = true, the document does not get really saved (essentially the same as if I close the TempOptimizedDoc).
3.2. If Cancel = false, the optimized document finally gets saved.

The most important moment is [2]. In here, while I'm linearizing the document, the user can decide to cancel linearization - same as if the entire optmization is canceled - therefore no "Save As" to be displayed.


-žarko
zarkogajic
User
Posts: 1370
Joined: Thu Sep 05, 2019 12:35 pm

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by zarkogajic »

Hi Support,
Vasyl-Tracker Dev Team wrote: Tue Apr 27, 2021 8:19 pm Seems it is possible to do it in that way. Please wait for next build...
Any plans for this for some next builds (not yet available in 9.1.356)?

-žarko
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2351
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc

Post by Vasyl-Tracker Dev Team »

Hi Zarko

Ok, the future behavior might be:

1. SDK's optimization is done.

2. IPXV_DocPreSaveOptimizedDataEvent is raised with TempOptimizedDoc!=null and DestFileName=null
(setting the IPXV_DocPreSaveOptimizedDataEvent.Progress.Canceled=true OR closing the TempOptimizedDoc - it will break whole operation here)

3. if IPXV_DocPreSaveOptimizedDataEvent.DestFileName==null - then SaveAs dialog is displayed for user

4. IPXV_DocPreSaveOptimizedDataEvent is raised again with TempOptimizedDoc!=null and DestFileName!=null
(setting the IPXV_DocPreSaveOptimizedDataEvent.Progress.Canceled=true OR closing the TempOptimizedDoc - it will break whole operation here)

Not sure about adding the IPXV_DocPreSaveOptimizedDataEvent.Cancel prop while IPXV_DocPreSaveOptimizedDataEvent.Progress.Canceled exists already and can be also used to cancel all work.

Cheers.
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
zarkogajic
User
Posts: 1370
Joined: Thu Sep 05, 2019 12:35 pm

Re: IPXV_DocPreSaveOptimizedDataEvent and altering the TempOptimizedDoc  SOLVED

Post by zarkogajic »

Hi Vasyl,

"Sold" :)

-žarko
Post Reply