Error on openDocument from File after multiple Insert page

A forum for questions or concerns related to the PDF-XChange Core API SDK

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, 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
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Error on openDocument from File after multiple Insert page

Post by Tom Princen »

I have this method:

public int MyJoinPages(string MyKey, string lsSourceFile1, string lsSourceFile2, string lsOutputFile)
{
InitPDFX(MyKey);
CDocAuthCallback clbk = new CDocAuthCallback();
clbk.m_pxsInst = MyIPXS;
IPXC_Document MydocSource1 = MyPXC.OpenDocumentFromFile(lsSourceFile1, clbk);
IPXC_Document MydocSource2 = MyPXC.OpenDocumentFromFile(lsSourceFile2, clbk);
MydocSource1.Pages.InsertPagesFromDoc(MydocSource2, MydocSource1.Pages.Count, 0, MydocSource2.Pages.Count, 0);

MydocSource1.WriteToFile(lsOutputFile);

return 1;
}

But when I run this more than 5 times it is throwing errors on the first opendocument from file

This is the code for generating the error:

string lsSourceFile = "C:\\Temp\\testwatermark.pdf";
string lsBackgroundPDF = "C:\\Temp\\split3.pdf";
string lsOutput = "C:\\Temp\\JoinBig.pdf";
myEditor.MyJoinPages(MyKey, lsSourceFile, lsBackgroundPDF, lsOutput);
for (int i = 1; i <= 10; i++)
{
myEditor.MyJoinPages(MyKey, lsOutput, lsBackgroundPDF, lsOutput);
}
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Error on openDocument from File after multiple Insert pa

Post by Tom Princen »

I found the solution
I did use the Init() function twice
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Error on openDocument from File after multiple Insert pa

Post by Sasha - Tracker Dev Team »

Hello Tom,

Indeed, the Initialization of the Instance should be done once per the program flow and also, the Finalize() method should be called after the project ends it's work. If multiple initializations are required (in some rare cases) then after each initialization and project's block work the Finalize() method should be called. Simply speaking the number of the Init() and Finalize() should be the same. Though the normal workflow requires one Init() and one Finalize(). It's written right here:
https://sdkhelp.pdf-xchange.com/view/PXV:CoClasses

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Error on openDocument from File after multiple Insert pa

Post by Tom Princen »

I use the core api to create a dll that I import in my program.

I have this code for the initialization of the PXC:
private void InitPDFX(string lsKey)
{
if (Initialized == false)
{
MyPXC.Init(lsKey);
MyIPXS = (IPXS_Inst)MyPXC.GetExtension("PXS");
MyIAUX = (IAUX_Inst)MyPXC.GetExtension("AUX");
Initialized = true;
MessageBox.Show("INITIALIZED");
}
}
this method is run the first time a core api function is called

I also have this method:
public void My_Finalize()
{
MyPXC.Finalize();
Initialized = false;
MessageBox.Show("FINALIZED");
}

This is run every time a core api function is finished.

As you see I added messageboxes to be shure there are no two successive calls to the Init() method.
When i run my program.
First I get a "INITIALIZED" message.
After that a FINALIZED message.
Then I start another function
So I get a "INITIALIZED" message.

But then I recieve an arror on this funtion
PDFXCoreAPI.IPXC_Inst.OpenDocumentFromFile(String lpszFileName, IPXC_DocAuthCallback pAuthCallback, IProgressMon pProgressMon, UInt32 nFlags, UInt32 nRestrictPerms)
The value is not within the expected range.

Am i missing something in MyFinalize method?

If you want i could mail my Visual studio project.
User avatar
Lzcat - Tracker Supp
Site Admin
Posts: 677
Joined: Thu Jun 28, 2007 8:42 am

Re: Error on openDocument from File after multiple Insert pa

Post by Lzcat - Tracker Supp »

Hi Tom.
Have you read previous answer? Explanation is right there!
Indeed, the Initialization of the Instance should be done once per the program flow and also, the Finalize() method should be called after the project ends it's work.
Please DO NOT call Init / Finalize function MORE THEN ONE TIME when program is running. You may call Init function whenever you need it, but NOT MORE THAN ONE TIME, and before program finish it is strongly recommended to call Finalize. But after you call Finalize function you cannot call Init or any other function except Finalize.
Victor
Tracker Software
Project manager

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Error on openDocument from File after multiple Insert pa

Post by Tom Princen »

Sorry but you stated:
"Simply speaking the number of the Init() and Finalize() should be the same."

I use use the dll I created from another application.

This is what I do in the other application.
(this is not c# code)

I have this function where I create a class from the dll I created:
public void MYfunction()
{
loPDFEditor is MyPDFEditorCoreAPI // creating an instance of the class in the dll

then I call a function from this class
loPDFEditor.MyExtractPage(gsPDFEditorKey,lsInputFIle,I-1,StringBuild(lsOutputfile,I))

In this function the initialisation is run and a api core call to the method i want to use.

At the end I use loPDFEditor.MyFinalize()
to finalize...

delete loPDFEditor // to delete the instance of the class
}


So everytime I use this method a new instance of the class is generated.

When I run this without the Myfinalize this works for successive attempts. Even with multiple executions of the init() function.

The problem starts when I use other dlls and activeX objects between two executions. Then suddenly I get problems:
- whithout the Finalize: the program hangs on the loPDFEditor.MyExtractPage line
- with the Finalize : i get the invalid parameter error.
User avatar
Lzcat - Tracker Supp
Site Admin
Posts: 677
Joined: Thu Jun 28, 2007 8:42 am

Re: Error on openDocument from File after multiple Insert pa

Post by Lzcat - Tracker Supp »

"Simply speaking the number of the Init() and Finalize() should be the same."
Can point me where this was stated? As far as I can see you where told that Init() must be called once.
Again, as said earlier - after call Finalize all calls to other functions are not allowed. Only first call Init is required, all subsequent calls will do nothing, this why your code working when you remove Finalize. Maybe toy need to create a global object which will hold instance object for you and call Finalize when it is destroyed (when application is closing).
What's the best, using the close() when you don't need the document object anymore or not?
It is very simple: function Close immediately free document source (free memory or close source file on a disk), and put document in non-operable mode, so even if someone hold document interface it will be not able to read or modify it. But please note that document object itself will be destroyed only when everyone who use it will release it. It is not problem if you do not call Close before releasing document object, only difference is that document source will be released during destroying document object (so if someone still hold document - source will remain in use until everyone release). Please note that even you have only one place where holding document object document may be not released immediately, but with some small delay due to architectural limitations. So best practice is to call Close at point when document object is no more needed to be sure that source is released (for example when you need to reuse it).
HTH.
Victor
Tracker Software
Project manager

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Error on openDocument from File after multiple Insert pa

Post by Tom Princen »

"Simply speaking the number of the Init() and Finalize() should be the same."
This qoute is from this topic from Sasha at Fri Apr 29, 2016 6:23 am

I tried using a global instance of my class. This works as long as I don't use other .Net dll's or other .Net controls in my program. Whenever another .Net control is used its simply hanging on OpenDocumentFromFile (without error).

I am only looking at a way to start with a clean sheet.

Now even when I create a new instance of my class or when I use a disposable class. For some reason, it wont work...
He always hangs on PDFXCoreAPI.IPXC_Inst.OpenDocumentFromFile

I thought Finalize was exactly for this?
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17819
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Error on openDocument from File after multiple Insert pa

Post by Tracker Supp-Stefan »

Hi Tom,

That quote from Sasha's words might got a bit "lost in translation. Having Init() and Finalize() once each is technicaly also making them called equal number of times. But as Victor said please ensure that you call them only once each, and things will run smoothly.

Regards,
Stefan
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Error on openDocument from File after multiple Insert pa

Post by Tom Princen »

no it does not run smoothly.

When use other .Net dll's or other .Net controls between 2 successive calls. It does not work anymore.

I really need a possibility to reinitialize the core api functionality.
User avatar
Lzcat - Tracker Supp
Site Admin
Posts: 677
Joined: Thu Jun 28, 2007 8:42 am

Re: Error on openDocument from File after multiple Insert pa

Post by Lzcat - Tracker Supp »

I really need a possibility to reinitialize the core api functionality.
Can you explain why? Can you give us a sample illustrating why this is essential and cannot have any workaround?
I'm afraid that it is impossible to do this at the moment, and this cannot be changed easy because of SDK design.
Victor
Tracker Software
Project manager

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Error on openDocument from File after multiple Insert pa

Post by Tom Princen »

Sorry but I told you already several times in previous posts...

this is what I do:
1. I use the split functionality from the core api. (I can do this en infinite number of times)
2. I use another .Net form control (IMSDKUIDocument.Controls.IMPdfView)
This is a control that shows recognized data on PDF files If you want I could send you the dlls.
3. The split function does not work any more?! It hangs on open OpenDocumentFromFile. Even WITHOUT reuse of INIT() or FINALIZE()!

Any other suggestions on how to solve this beside reinitializing the core API?


By the way I needed to convert my program to the core api because you don't continue developing the SDK. That was what you suggested when I mentioned an error opening PDF files with some small errors in it.
But the new API has only been trouble for me. Bad documentation, function that are not available,...
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3549
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: Error on openDocument from File after multiple Insert pa

Post by Ivan - Tracker Software »

Can you please provide us with minimal project where we can reproduce the problem?
As my colleagues said above IPXC_Inst should not re-initialized - means Init function should not be called after Finalize. If you are sure you cannot avoid that, it would be better to do not call Finalize at all.

If you will send us your sample project, we will try to change it a bit to remove extra calls to Init and Finalize.
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Error on openDocument from File after multiple Insert pa

Post by Tom Princen »

can I send the project by mail?
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17819
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Error on openDocument from File after multiple Insert pa

Post by Tracker Supp-Stefan »

Hi Tom,

Yes - please send the project to support@ - and I will forward it to Ivan.

Regards,
Stefan
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Error on openDocument from File after multiple Insert pa

Post by Tom Princen »

I created a Visual studio project. But there i don't have this issue.

My program is written in Windev. I don't you can do something with a windev project?
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17819
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Error on openDocument from File after multiple Insert pa

Post by Tracker Supp-Stefan »

Hi Tom,

If it works in a VS project, then the issue is most likely with the implementation in Windev. I can't comment if Ivan will be able to look at that project, but please do share it with us - and if he can, he will advise further on what could be causing this.

Regards,
Stefan
Tom Princen
User
Posts: 83
Joined: Wed Mar 25, 2015 10:15 am

Re: Error on openDocument from File after multiple Insert pa

Post by Tom Princen »

Sorry but a .net application has other ways of handling external dlls. For some reason in my development environment one dll interferes with another if you do not properly close them.

Is it it really that big change to develop a dispose function of some kind?
So that I can use the init function multiple times? It would be a huge help for me.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Error on openDocument from File after multiple Insert pa

Post by Sasha - Tracker Dev Team »

Hello Tom,

1) Please send a sample Windev project to polaringu@tracker-software.com
2) Have you tried doing this?
Ivan - Tracker Software wrote:As my colleagues said above IPXC_Inst should not re-initialized - means Init function should not be called after Finalize. If you are sure you cannot avoid that, it would be better to do not call Finalize at all.
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply