Delete last page at runtime | Community
Skip to main content
Level 2
July 4, 2018
Solved

Delete last page at runtime

  • July 4, 2018
  • 19 replies
  • 9485 views

We've got an invoice adobe form in SAP where the last page should always be printed on a different kind of paper.

That requires us to use a different intput and output tray for the last page.

Out current workaround idea:

Print the document twice: First time without the last page, second time only the last page. This way we have 2 separate print jobs and can control the intput/output tray as we may. But we can't manage to always delete the last page.

The pages are filled using repeating subforms. We don't know how many times the subforms are repeated at design time, and how large they'll be.

Any ideas on how to solve this?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by _Bruce_Robertson

Hi,

You could move the script to the layout:ready event.  You will need to force the relayout to get the presence update to take effect.  The problem then is to stop this script from firing again as you will probably end up with all the pages hidden eventually as the relayout will trigger the layout:ready event.

Try this code in the layout:ready event of Formular1.

if (!PagesHidden.value) {
      var pageCount = xfa.layout.pageCount();
      var lastVisible = null;
      for (var i = content._repeating_subform.count - 1; i >=0; i--) {
            if (xfa.layout.page(content.repeating_subform.all.item(i)) == pageCount) {
                 content.repeating_subform.all.item(i).presence = "hidden";
            } else {
                if (lastVisible == null) {
                      lastVisible = content.repeating_subform.all.item(i);
                }
            }
      }
      if (xfa.layout.pageSpan(lastVisible) > 1) {
          lastVisible.presence = "hidden";
      }
      PagesHidden.value = "1";
      xfa.layout.relayout();
}

PagesHidden is a form variable with an initial value of zero.  This gets set to "1" so the script only executes once.

19 replies

_Bruce_Robertson
Level 10
July 4, 2018

Hi,

There are some print options in Designer, if your different kinds of paper are different size then they might be all you need.

Regards

Bruce

Level 2
July 4, 2018

Hi Bruce,

thanks for your explanation.

To use that option, our last page would need a different page size, correct?

How can I tell the last page to be on a different master page, or a different page size?

Best regards

Joschka

_Bruce_Robertson
Level 10
July 4, 2018

That is difficult to say without seeing the structure of your form. But, I would hope you could have a second master page that the last page was linked with.  Can you structure your form that way?

Level 2
July 4, 2018

We can create a second master page, but we would need to tell the content when to switch to the second master page.

Here's the structure:

Repeeating_subform will eventually extend to the last page.

How do I tell repeating_subform to use my second masterpage "last_page"?

_Bruce_Robertson
Level 10
July 5, 2018

Hi,

I had thought the last page was different, that is an alternative to content in your sample.  So that print options approach wont work.

This script will hide all repeating_subform objects that are on the last page.

var pageCount = xfa.layout.pageCount(); 

for (var i = _repeating_subform.count - 1; i >=0; i--) {

      if (xfa.layout.page(repeating_subform.all.item(i)) == pageCount) {

           repeating_subform.all.item(i).presence = "hidden";

      }

}

Level 2
July 5, 2018

Hm... that sounds like the right direction.

If I do that now, the last page does not contain any items, but it's still printed.

Any idea on how to avoid the blank page?

Can I access the page and set the presence to hidden? If so, how?

_Bruce_Robertson
Level 10
July 5, 2018

I guess your repeating_subform and my_text allows page breaks within content?  If so then we will need to get rid of one more.

So try this;

var pageCount = xfa.layout.pageCount();   
var lastVisible = null;
for (var i = _repeating_subform.count - 1; i >=0; i--) { 
      if (xfa.layout.page(repeating_subform.all.item(i)) == pageCount) { 
           repeating_subform.all.item(i).presence = "hidden"; 
      } else {
           if (lastVisible == null) {
                lastVisible = repeating_subform.all.item(i);
           }
      }

if (xfa.layout.pageSpan(lastVisible) > 1) {
     lastVisible.presence = "hidden"; 
}

Level 2
July 5, 2018

Thanks again for your input.

Same problem. I've tried to hide all items from the second-last page (... == pageCount - 1).

With 4 pages of subforms, pages 3 and 4 are now empty, but are still printed.

Is the event ready:layout maybe too late?

The page count is only available at this event though, right? At least the script doesn't work in ready:form.

_Bruce_Robertson
Level 10
July 5, 2018

Hi, 

There's lots I'm not sure about with what you are trying o do.  I tried that code in the click event of a button.  But maybe the DocReady would work?

Brue

Level 2
July 5, 2018

Hi Brue,

what I'm trying to do is to split the document into last page and everything but last page.

Since I can't use external tools for this (like PDF Toolkit / PDF Toolbox), I have to find a script to do just that.

I hope it's clearer now.

A button will not work for us, since the form is not interactive.

So I really just need a command to delete the last page from the resulting PDF, or every page before that.