Save to stream marks document dirty...

PDF-XChange Viewer SDK for Developer's
(ActiveX and Simple DLL Versions)

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

DSSavant
User
Posts: 216
Joined: Thu Jul 08, 2004 7:29 pm

Save to stream marks document dirty...

Post by DSSavant »

Bug summary:

When saving a document to a stream, if the document was previously modified and then told to be un-modified, the save operation will revert its dirty state back to modified.

How to reproduce:

1. Load a document.
2. Check the modified state:
2.1 The modified state is un-modified.
>> Viewer.DoVerb("Documents[#" + ActiveDocumentId.ToString() + "].Modified", "get", dataIn, out dataOut);
3. Save to a stream.
4. Check the modified state.
4.1 The modified state has not changed.

5. Update the document somehow.
6. Check the modified state:
6.1 The modified state is modified.
7. Save to a stream.
8. Check the modified state.
8.1 The modified state has not changed.

9. Set the modified state to be un-modified.
>> Viewer.DoVerb("Documents[#" + ActiveDocumentId.ToString() + "].Modified", "set", 0, out dataOut);
10. Check the modified state:
10.1 The modified state is un-modified.
11. Save to a stream.
12. Check the modified state.
12.1 The modified state has has changed and is modified.

----------------------------------------

My code:

Code: Select all

		public string DebugTestRoutine()
		{
			string output = "";

			try
			{
				object dataIn = new object();
				object dataOut = new object();

				// See if we are modified or not going in...
				Viewer.DoVerb("Documents[#" + ActiveDocumentId.ToString() + "].Modified", "get", dataIn, out dataOut);
				Log.Write(Log.Level.Information, "---------------");
				Log.Write(Log.Level.Information, "Document is {0}dirty.", dataOut.ToString() == "0" ? "NOT " : "");

				MemoryStream documentStream = new MemoryStream();
				StreamWrapper streamWrapper = new StreamWrapper(documentStream);

				dataIn = new object[] { ActiveDocumentId, streamWrapper } ;

				Viewer.DoVerb(null, "SaveDocument", dataIn, out dataOut, (int) PXCVA_Flags.PXCVA_NoUI);

				Viewer.DoVerb("Documents[#" + ActiveDocumentId.ToString() + "].Modified", "get", dataIn, out dataOut);
				Log.Write(Log.Level.Information, "Document is {0}dirty.", dataOut.ToString() == "0" ? "NOT " : "");

				documentStream.Position = 0;
				documentStream.Close();

				Log.Write(Log.Level.Information, "Resetting Document to not be dirty.");
				Viewer.DoVerb("Documents[#" + ActiveDocumentId.ToString() + "].Modified", "set", 0, out dataOut);
				Log.Write(Log.Level.Information, "Done resetting Document to not be dirty.");
				Log.Write(Log.Level.Information, "---------------");
			}
			catch (Exception exception)
			{
				Log.Write(Log.Level.Error, "BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOM.");
			}

			return output;
		}
The log statements below shows the following bug. You can correlate the logging statements from the code above to the lines below.

*** A document is loaded.
*** The code above is executed three times.
*** Notice that before and after save operation, the document is NOT dirty.

:: ---------------
:: Document is NOT dirty.
Save Operation …
:: Document is NOT dirty.
:: Resetting Document to not be dirty.
:: Done resetting Document to not be dirty.
:: ---------------
:: ---------------
:: Document is NOT dirty.
Save Operation …
:: Document is NOT dirty.
:: Resetting Document to not be dirty.
:: Done resetting Document to not be dirty.
:: ---------------
:: ---------------
:: Document is NOT dirty.
Save Operation …
:: Document is NOT dirty.
:: Resetting Document to not be dirty.
:: Done resetting Document to not be dirty.
:: ---------------


*** I now dirty the document by rotating the page or using the Pencil Tool or ….
*** The code above is executed three more times.
*** Notice that before the save operation, the document is not dirty but after, it becomes dirty. The save operation is ‘remembering’ that originally the document went dirty in its lifetime and is resetting the value back to dirty rather than honoring the current value.

:: ---------------
:: Document is dirty. ******* Note that this is dirty here because I modified the document above with the Pencil Tool.
Save Operation …
:: Document is dirty.
:: Resetting Document to not be dirty.
:: Done resetting Document to not be dirty.
:: ---------------
:: ---------------
:: Document is NOT dirty.
Save Operation …
:: Document is dirty. ******* The Save operation has changed the state of the document from NOT dirty to dirty.
:: Resetting Document to not be dirty.
:: Done resetting Document to not be dirty.
:: ---------------
:: ---------------
:: Document is NOT dirty.
Save Operation …
:: Document is dirty.
:: Resetting Document to not be dirty.
:: Done resetting Document to not be dirty.
:: ---------------
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2445
Joined: Thu Jun 30, 2005 4:11 pm

Re: Save to stream marks document dirty...

Post by Vasyl - PDF-XChange »

Hi, DSSavant.
9. Set the modified state to be un-modified.
>> Viewer.DoVerb("Documents[#" + ActiveDocumentId.ToString() + "].Modified", "set", 0, out dataOut);
10. Check the modified state:
10.1 The modified state is un-modified.
11. Save to a stream.
12. Check the modified state.
12.1 The modified state has has changed and is modified.
The Doc.Modified property works in End-User level only - the PDF-viewer reacts to your changes of this property (remove/add the modification marker "*", when this property is "true" - asks the end-user about save modified document before close, etc) but document keeps the real state of modification. The real state of modification can be changed when:
1. document did save to an original file or stream.
2. document did save by "Save As" operation to other file (but not by "Save As Copy").

In your case: the viewer did save the document to the stream, but it was not switched to this stream (like to "Save As Copy"), so real state is "Document Modified" and viewer synchronizes it with ui-property "Document.Modified" after save operation.

Solution for your case - you may intercept the new named notification "Notifications.DocSaved":

Code: Select all

eventhandler OnEnvet(type, name... )
{
    if ((type == PXCVA_OnNamedNotify) AND (name == "Notifications.DocSaved"))
    {
        int docId;
        ctrl.GetProperty("Notifications.DocSaved.DocID", out docId);
        int saveMod;
        ctrl.GetProperty("Notifications.DocSaved.SaveMode", out saveMod);
        if (saveMod == 3) // "SavedToStream"
        {
            ctrl.SetDocumentProperty(docId, "Modified", "false"); // clear dirty flag after saving to stream
        }
    }
}
HTH
PDF-XChange Co. LTD (Project Developer)

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
DSSavant
User
Posts: 216
Joined: Thu Jul 08, 2004 7:29 pm

Re: Save to stream marks document dirty...

Post by DSSavant »

I understand. The dirty bit stays with the document regardless of being saved. Thanks for the work-around.
User avatar
Paul - PDF-XChange
Site Admin
Posts: 7362
Joined: Wed Mar 25, 2009 10:37 pm

Re: Save to stream marks document dirty...

Post by Paul - PDF-XChange »

:)
Best regards

Paul O'Rorke
PDF-XChange Support
http://www.pdf-xchange.com