OpenDocumentFromFile with Password, etc.

PDF-XChange Editor SDK for Developers

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

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.
jeffp
User
Posts: 923
Joined: Wed Sep 30, 2009 6:53 pm

OpenDocumentFromFile with Password, etc.

Post by jeffp »

I have two questions relating to OpenDocumentFromFile.

[1] I'm trying to implement the pAuthCallback parameter so I can open password protected PDFs, but I'm not able to compile the code below, which is my attempt to implement the callback. Can you help me out with the proper syntax in Delphi.

[2] How do Iget OpenDocumentFromFile to open non-PDF files like image files and office files and automatically convert them into PDFs? Is that possible with this call from the Core Layer?

Code: Select all

function TPDFCoreLayer.OpenDocument(AFileName: String; APasswords: String = ''): Boolean;
  procedure AuthCallback(const pDoc: IPXC_Document; nFlags: ULONG_T);
  var
    AAtom, AStandard: Cardinal;
    AStatus: PXC_PermStatus;
  begin
    AAtom := pDoc.GetSecurityHandlerType(False);
    if (AAtom > 0) then
    begin
      AStandard := INST_PXS.StrToAtom('Standard');
      if (AStandard = AAtom) then
      begin
        AStatus := pDoc.AuthorizeWithPassword(PChar(FPassword));
        if (AStatus = Perm_ReqGranted) then //?? How do I handle this?
      end;
    end;
  end;
begin
  Result := False;
  if FileExists(AFileName) then
  begin
    FDoc := INST_PXC.OpenDocumentFromFile(PChar(AFileName), AuthCallback, nil, 0, 0);
    Result := True;
  end;
end;
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: OpenDocumentFromFile with Password, etc.

Post by Sasha - PDF-XChange »

Hello Jeff,

1) We will try to update our Delphi sample with the appropriate code.

2) Sadly, there is no such possibility to do the different format opening from the Core SDK - all of the converters are Editor SDK only.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
jeffp
User
Posts: 923
Joined: Wed Sep 30, 2009 6:53 pm

Re: OpenDocumentFromFile with Password, etc.

Post by jeffp »

Ok. Let me know when the open with password code is placed in the Delphi sample.

On the second point, is there anyway to open and convert a document using IPXV_Inst instead of IPXV_Control?

--Jeff
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: OpenDocumentFromFile with Password, etc.

Post by Sasha - PDF-XChange »

Hello Jeff,

Well, you can use the https://sdkhelp.pdf-xchange.com/vie ... _MainFrame methods directly. The similar Control's methods are redirected to these methods in their inner code.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: OpenDocumentFromFile with Password, etc.

Post by Sasha - PDF-XChange »

Hello Jeff,

We've updated the Delphi sample with the code you needed:
https://github.com/tracker-software/PDF ... #L226-L235
https://github.com/tracker-software/PDF ... llback.pas

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
jeffp
User
Posts: 923
Joined: Wed Sep 30, 2009 6:53 pm

Re: OpenDocumentFromFile with Password, etc.

Post by jeffp »

Thanks for the AuthCallBack code. It works perfectly. I can now open my password protected PDF files.

My next step is to open non-PDF files have take advantage of your converters. For example, I have a bunch of .docx files I want to convert to PDF. I understand that I have to use PXV_Inst for this so I have constructed a modified open procedure below to do this.

It works just fine, but it displays a progress dialog that tells me it is converting the docx to PDF. I would like suppress this dialog and perhaps take advantage of some additional ICab Parameters, but I'm not sure how to use the ICab and what setting will suppress the progress converter dialog.

Code: Select all

function TMyPDF.OpenDocumentEx(AFileName: String): Boolean;
var
  ARect: tagRect;
  AParams: ICab;
begin
  if not Assigned(FFrame) then
  begin
    INST_PXV.CreateNewMainFrm(Application.MainFormHandle, ARect, 0, FFrame);
  end;
  FDocV := FFrame.OpenDocFromPath(PChar(AFileName), AParams);
  FDoc := FDocV.CoreDoc;
end;
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: OpenDocumentFromFile with Password, etc.

Post by Sasha - PDF-XChange »

Hello Jeff,

To hide progress this should be used:
https://gist.github.com/Polaringu/98b1c ... e26e07f3ad

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
jeffp
User
Posts: 923
Joined: Wed Sep 30, 2009 6:53 pm

Re: OpenDocumentFromFile with Password, etc.

Post by jeffp »

Perfect. That's what I need.

But where in the Help is listed all the options like

openParamsRoot.SetBool("NoProgress", true);

There seem to be a lot of these options to chose from but I can't seem to find a comprehensive list of them and I hate to keep asking all the time.

...

Also, just implemented your code in mine below. When I first ran it I got an error. Then I added the SetExceptionMask call (that you gave me in a SaveDocument context) just before the Open call it took care of the exception. Is this right? And where else am I going to have to add SetExceptionMask to get rid of exceptions?

Again, in the code below I'm opening a simple .docx file.

Code: Select all

function TMyPDF.OpenDocumentEx(AFileName: String): Boolean;
var
  ARect: tagRect;
  AParams: ICab;
  AOpts: ICabNode;
begin
  if not Assigned(FFrame) then
  begin
    INST_PXV.CreateNewMainFrm(Application.MainFormHandle, ARect, 0, FFrame);
  end;
  AParams := INST_PXV.CreateOpenDocParams;
  AOpts := AParams.Root;
  AOpts.SetBool('NoProgress', True);
  SetExceptionMask(exAllArithmeticExceptions);
  FDocV := FFrame.OpenDocFromPath(PChar(AFileName), AParams);
  FDoc := FDocV.CoreDoc;
end;

...

On further analysis, I looks like I can add

SetExceptionMask(exAllArithmeticExceptions);

In the TMyPDF.Create method of my custom object and it seems to handle those exceptions I've been reporting.

It works but I'm not sure I understand what is going on here.

Or should I just add SetExceptionMask(exAllArithmeticExceptions); in the Initialization section of my .pas file?


--Jeff
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: OpenDocumentFromFile with Password, etc.

Post by Sasha - PDF-XChange »

Hello Jeff,

Well, since it's one of the most used methods - it took me two hours to write this, but here you go :wink:
https://sdkhelp.pdf-xchange.com/vie ... Parameters

As for the Delphi questions - I will ask my colleague tomorrow at work.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
jeffp
User
Posts: 923
Joined: Wed Sep 30, 2009 6:53 pm

Re: OpenDocumentFromFile with Password, etc.

Post by jeffp »

Nice. Thanks.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19919
Joined: Mon Jan 12, 2009 8:07 am

Re: OpenDocumentFromFile with Password, etc.

Post by Stefan - PDF-XChange »

:D
jeffp
User
Posts: 923
Joined: Wed Sep 30, 2009 6:53 pm

Re: OpenDocumentFromFile with Password, etc.

Post by jeffp »

Take a look at this page

https://sdkhelp.pdf-xchange.com/vie ... es_Options

and the click on the link for the FormatParams.

The link is either broken or you have not written the help for this page. If the latter, is there anyway I can get you to fill out all the options and values for FormatParams. It seems to be the only page missing for the exportToImages feature. You do provide a good example on this one, but if you want to use some values other than those in the sample code, you don't know what they are.

Thanks.

--Jeff
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: OpenDocumentFromFile with Password, etc.

Post by Sasha - PDF-XChange »

Hello Jeff,

We will try to fill the required information asap.

As for the Delphi problem - I've written to the developer that can answer that question. He will reply here on that matter.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
User avatar
Roman - Tracker Supp
Site Admin
Posts: 337
Joined: Sun Nov 21, 2004 3:19 pm

Re: OpenDocumentFromFile with Password, etc.

Post by Roman - Tracker Supp »

Hello Jeff,

As of FPU exceptions.
Thechically the problem is because Delphi code leaves FPU control and status registers in a state that causes further FPU instructions in the editor code to generate exceptions even in valid conditions.

SetExceptionMask(exAllArithmeticExceptions) is a possible solution to this problem (with some overhead however - just clearing FPU status word (SW) would be sufficient.

I am not sure if calling SetExceptionMask once during your code initialization will be enough.
Some Delphi code could require FPU exceptions to be unmasked to work correctly. So it is possible that the exceptions mask will be reset in the meantime.

I think the most reliable solution would be to enclose PDF-X SDK calls with the following code:

Code: Select all

TArithmeticExceptionMask prevMask = SetExceptionMask(exAllArithmeticExceptions);
(*
PDF-X API calls
*)
SetExceptionMask(prevMask);
See also here.
jeffp
User
Posts: 923
Joined: Wed Sep 30, 2009 6:53 pm

Re: OpenDocumentFromFile with Password, etc.

Post by jeffp »

Thanks for the additional info. I'll keep my eye on this.

--Jeff
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: OpenDocumentFromFile with Password, etc.

Post by Sasha - PDF-XChange »

Glad that helped Jeff.
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ