How to Arrange Pages for 2-Up Duplex Printing

I worked this out and made a Java program out of it.
You can use the output for the command line for Multivalent-Impose command, -page option

Command Syntax is : java Page <pages to print> <pages to pad>

So, like for my example 1 of 10 pages, I'd enter "java Page 10 1" because I want my first page as a left-hand page. The output is:

For 10 pages on 3 sheets
x,x,1,10,9,2,3,8,7,4,5,6

The two "x"'s tell me that if I use the document as is, I will have two blank pages in the final booklet for a total of 12 pages. If I say "java Page 12 0", I get:

For 12 pages on 3 sheets
12,1,2,11,10,3,4,9,8,5,6,7

By comparing the sequence of numbers, I can determine I would need to add a page to the beginning and end of my 10 pages to get 12 pages in the order I want.

Here's the source. Holler if you have questions.

import java.io.* ;
import java.util.* ;
import java.lang.Integer ;

public class Page
{
	public static void main ( String args[ ] )
	{
		int pages = Integer.parseInt ( args[0] ) ;
		int pad = Integer.parseInt ( args[1] ) ;
		
		int n = ( pages + pad ) / 4 ;
		
		if ( ( pages + pad ) % 4 != 0 ) n++ ;

		System.out.println ( "For " + pages + " pages on " + n + " sheets" ) ; 

		Integer num ;
		int foo ;
		ArrayList<Integer> pageOrder = new ArrayList<Integer> ( pages + pad ) ;

		for ( int m = 0 ; m < n ; m++ )
		{	pageOrder.add ( new Integer ( ( 4 * n ) - ( 2 * m ) ) ) ;
			pageOrder.add ( new Integer ( ( 2 * m ) + 1 ) ) ;
			pageOrder.add ( new Integer ( ( 2 * m ) + 2 ) ) ;
			pageOrder.add ( new Integer ( ( 4 * n ) - ( 2 * m ) - 1 ) ) ;
		}

		for ( ListIterator it = pageOrder.listIterator() ; it.hasNext() ; )
		{	num = (Integer)it.next() ;
			foo = num.intValue() - pad ;

			if ( foo <= 0 || foo > pages ) 
			{	System.out.print ( "x" ) ;
			}
			else
			{	System.out.print ( foo ) ;
			}

			if ( it.hasNext() )
			{	System.out.print ( "," ) ;
			}
		}
		System.out.println () ;
	}
}

[update] fixed a typo
[update] Got rid of the warning !

Syndicate content

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Clarification

Not being at all conversant in java, do I save this as a .js, .java, .jar, or something different? Anything required, like -classpath flag to java at execution to make it work?

Running Windows XP, java 1.6.0_02.

Jason
-- Coffee and Books, the pleasures of life

How to...

Save it as "Page.java"

Then

javac -d . Page.java

and then you should be able to

java Page 12 0

The "-d ." option puts the Page.class file in the current directory which SHOULD be in your classpath
-----------------------------------
"I think the surest sign that there is intelligent life out there in the universe is that none of it has tried to contact us." (Calvin and Hobbes/Bill Waterson)

I am not conversant with

I am not conversant with java and my programming skills have faded about 10 years ago! This might be a dumb question but can you tell me how to get javac recognized as a command to execute the command "javac -d . Page.java"? I have the latest java 1.6. Thanks.

Do you have the SDK or just the JVM ?

The Java Virtual Machine (JVM) is what you need to run Java programs

The Software Development Kit (SDK) is what you need to create Java programs

They are separate installs

-----------------------------------
"I think the surest sign that there is intelligent life out there in the universe is that none of it has tried to contact us." (Calvin and Hobbes/Bill Waterson)

Hi, I've never used the Java

Hi,

I've never used the Java compiler in my life, so I'm a bit confused. How do I solve this problem?

C:\Program Files\Java\jdk1.7.0\bin>javac -d . Page.java
Page.java:3: cannot find symbol
symbol : class Integer
location: package java.util
import java.util.Integer ;
^
Note: Page.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

OOOOOOPS ! typo

java.util.Integer should be java.lang.Integer

and don't worry about the "unsafe operations"

it's complaining about the "pageOrder.add ( new Integer...) lines. In "bullet-proof" code, you should check that the "new Integer" part did not fail before adding it to the list.

I find this typo weird. I swear I tested this code. Unless Integer moved from util to lang ??!
-----------------------------------
"I think the surest sign that there is intelligent life out there in the universe is that none of it has tried to contact us." (Calvin and Hobbes/Bill Waterson)

No more warnings !

Changed

ArrayList pageOrder = new ArrayList ( pages + pad ) ;

to

ArrayList<Integer> pageOrder = new ArrayList<Integer> ( pages + pad ) ;

and the compile warnings go away
-----------------------------------
"I think the surest sign that there is intelligent life out there in the universe is that none of it has tried to contact us." (Calvin and Hobbes/Bill Waterson) ***

compiler objecting again

Ygor:

When I tried to compile I got this error:

page.java:5: class Page is public, should be declared in a file named Page.java
public class Page
^
1 error

Which I found odd, considering line 5 of the script reads
public class Page

Not a huge issue. I'm putting together weekly calendars so I can run it in batches of 10/12 pages (or figure out myself), but now it's under my skin and it's got my curiosity aroused.

Troubleshooting

Sent you a message directly
-----------------------------------
"I think the surest sign that there is intelligent life out there in the universe is that none of it has tried to contact us." (Calvin and Hobbes/Bill Waterson) ***

ah shoot. sorry to bother you

I just figured it out myself. After looking at the instructions for the nth time, I saw that you specified "save as Page.java" not "page.java" which makes a difference to real languages :-)

still works just fine with jdk1.6.0_31 nt

On the next go-round, I'll test it on 1.7. I have both installed.
-
You can do anything, but you can't do everything.

No problem

Glad you got it working
-----------------------------------
"I think the surest sign that there is intelligent life out there in the universe is that none of it has tried to contact us." (Calvin and Hobbes/Bill Waterson) ***

Ygor I have always suspected that

you are a smart person!!!!! More power to you!!!!

Printing, editing and other instructions...

for non-Luddite dummies?

Okay, I'll admit it, my computer is a tool. One that I know how to use, but only when the genius computing stuff is done by others.

Could there be a place where one could find out what to do, exactly, in excruciatingly simple step by step instructions.

As in, open "this". Put this into "that" place, write the following (using however many angels you can extract from the head of that pin over there), and then do "this next thing", and so on. In, if I may make it even more excruciatingly clear, those excruciatingly laborious step by bloody step-wise directions. Leave nothing out, particularly for one such as I know I am...smart at some stuff, but not a computer wise being. In fact, a computing idiot, if truth be known.

Or, I could just say it simply. I have no idea whatsoever how to print this out, to say nothing of how to edit it into a format that will be printable so that I can use this in my Filofax Personal. Please help me.

Please...

If it were possible to beg on bended knee, I'd do it. In fact, consider it done-metaphorically speaking that is. Thanks so much for the hard work you've done, and the great calendar.

I do have WordPerfect X4, which has a PDF program where I can edit, I think, and print out PDF files. However, I can't even get this to open into WP X4, so if you could tell me how to save, move or otherwise, I'd appreciate that too.

Your humble cohort in crime...err...calendaring...

Elizabeth

What joy!!!---my heart really is a violin.

Did you see...

...this post: Dynamic Templates and Multivalent: Example 1 - January 2008 classic booklet ??

I am not sure how much detail I can provide. Sadly, the easy-to-use stuff is not free.

I did find these: for Mac: PDFLab -- from the same guy that did CocoaBooklet
and for Windows: PDFTK Builder

These two utilities will let you combine and split and rearrange pages of a PDF document. Using one of these utilities will save you from doing all but the last step of the Multivalent example

That last step is called IMPOSITION. Imposition is the process of arranging pages on multi-page sized sheets for printing so that the large sheets can be folded, and possibly cut, and have the pages in the correct order.

Multivalent is the only freebie I have found that will impose PDF.
[Edit] Just for the heck of it, I tried BookletCreator-dot-Com. It does work, but I thought it a bit slow.
-----------------------------------
"I think the surest sign that there is intelligent life out there in the universe is that none of it has tried to contact us." (Calvin and Hobbes/Bill Waterson)

I saw, but that's where I got lost...

I did see that post. But, and promise you won't laugh too loudly, what exactly does one do with those strings of language?

Where would you use them? What step are you at when they might be needed, and in what program window?

Like I said above, not a Luddite, just iggurnaut, so to speak.

Thanks again.

What joy!!!---my heart really is a violin.