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 + " on " + n + " sheets" ) ; 

		Integer num ;
		int foo ;
		ArrayList pageOrder = new ArrayList ( 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

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)

Ygor I have always suspected that

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