Expand my Community achievements bar.

SOLVED

The xfa.host.print command in form1::prePrint causes the print dialog to loop

Avatar

Level 1

I've tried for multiple hours over two days now to get the xfa.host.print command to function properly in the prePrint event. When I use the following javascript command in a button, I get the proper results but using it in prePrint causes the print dialog to continuously loop back. I cannot use the Cancel button, or print without the dialog popping back up.

The only way to stop it is to use the Task Manager on Windows or "Force Quit" the application on a Mac. This occurs in both Adobe Reader or Acrobat Pro.

Using this under a button works perfectly but not in the prePrint section of either the button or the form itself. It's important that I capture the user trying to use the Reader or Acrobat default print button in order to prevent printing the first page.

This is the command:

xfa.host.print(1, "1", (xfa.host.numPages -1).toString(), 0, 0, 0, 0, 0);

I've also tried it using specific page numbers:

xfa.host.print(1, "1", "3", 0, 0, 0, 0, 0);

1 Accepted Solution

Avatar

Correct answer by
Level 10

To allow the printout only via a specific button, you'll have to a reference like a variable.

Open the form properties and add a new variable "printFlag" with a default value 0.

And the following script to the prePrint event of the form:

if (printFlag.value == "0") {

xfa.event.cancelAction = true;

}

Add a print button with these scripts in its click event and …

// Change print flag before executing print method!

printFlag.value = "1";

xfa.host.print(1, "0", (xfa.host.numPages -1).toString(), 0, 0, 0, 0, 0);

… the post print event.

// Reset the print flag after printing

printFlag.value = "0";

Hope this helps.

View solution in original post

4 Replies

Avatar

Level 10

This is just a bad idea! The prePrint event fires when a print command has been executed either via script or user action. Executing another print command from this event will of course create a loop.

Use the click event instead.

Avatar

Level 1

When using a button, I do use the Click event, but I cannot force everyone to use the button so I need to use the prePrint event of the form to capture the times when users will select the application's print button.

Unless there's another way to capture and control the user selecting the application print button, what option do we have?

Avatar

Correct answer by
Level 10

To allow the printout only via a specific button, you'll have to a reference like a variable.

Open the form properties and add a new variable "printFlag" with a default value 0.

And the following script to the prePrint event of the form:

if (printFlag.value == "0") {

xfa.event.cancelAction = true;

}

Add a print button with these scripts in its click event and …

// Change print flag before executing print method!

printFlag.value = "1";

xfa.host.print(1, "0", (xfa.host.numPages -1).toString(), 0, 0, 0, 0, 0);

… the post print event.

// Reset the print flag after printing

printFlag.value = "0";

Hope this helps.

Avatar

Level 1

I saw your post similar to this to someone else and already implemented it, I was hoping for something else. It's too bad that this can't be done by hooking the Print key properly but it is working using this method although I added a message box to alert the user to find the print button on the form.

Thank you...