Attached you find an sample plugin that does this:
1. try to guess what pages are A4 and what A3
2. rotate A4 portrait to landscape and A3 landscape to portrait
3. insert pages where A4 and A3 pages follow each other on the same
sheet of paper (duplex)
you can do some things better, just try to improve it.
Best regards,
Carsten
abc xyz schrieb:
hi All,
i want to rotate the pages of existing pdf file mean landscape to
portrait and portrait to landscape. can any one help me
can we do it by using PdfStamper class or PdfCopy class
thnx.
regard,
*_Simaab_*
------------------------------------------------------------------------
Yahoo! Mail goes everywhere you do. Get it on your phone
<http://us.rd.yahoo.com/evt=31132/*http://mobile.yahoo.com/services?promote=mail>.
package com.lowagie.tools.plugins;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.*;
import java.io.*;
import java.util.*;
import com.lowagie.tools.arguments.PdfFilter;
import com.lowagie.tools.arguments.FileArgument;
import javax.swing.JInternalFrame;
import com.lowagie.tools.arguments.ToolArgument;
public class Normalize
extends AbstractTool {
static {
addVersion("$Id: Burst.java,v 1.8 2005/11/29 21:05:02 blowagie Exp $");
}
/**
* Constructs a Burst object.
*/
public Normalize() {
menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW;
arguments.add(new FileArgument(this, "srcfile",
"The file you want to normalize", false,
new PdfFilter()));
arguments.add(new FileArgument(this, "destfile", "The resulting PDF", true,
new PdfFilter()));
}
/**
* @see com.lowagie.tools.plugins.AbstractTool#createFrame()
*/
protected void createFrame() {
internalFrame = new JInternalFrame("Normalize", true, false, true);
internalFrame.setSize(300, 80);
internalFrame.setJMenuBar(getMenubar());
System.out.println("=== Normalize OPENED ===");
}
int pagecount;
float width;
float height;
PdfDictionary lastpage = null;
float tolerancex = 60;
float tolerancey = 60;
int pagecountinsertedpages;
int pagecountrotatedpages;
protected void iteratePages(PdfDictionary page, PdfReader pdfreader,
ArrayList pageInh,
int count_in_leaf, PdfWriter writer) throws
IOException {
float curwidth;
float curheight;
PdfArray kidsPR = (PdfArray) pdfreader.getPdfObject(page.get(PdfName.KIDS));
if (kidsPR == null) {
PdfArray arr = (PdfArray) page.get(PdfName.MEDIABOX);
ArrayList arl = arr.getArrayList();
curwidth = Float.parseFloat(arl.get(2).toString());
curheight = Float.parseFloat(arl.get(3).toString());
PdfNumber rotation = (PdfNumber) pdfreader.getPdfObject(page.get(PdfName.
ROTATE));
if (rotation == null) {
System.out.println("Rotation fehlt");
rotation = new PdfNumber(0);
}
Ausrichtung ausr = new Ausrichtung(rotation.floatValue(),
new Rectangle(curwidth, curheight));
switch (ausr.type) {
case Ausrichtung.A4Landscape:
case Ausrichtung.A3Portrait:
ausr.rotate();
page.put(PdfName.ROTATE, new PdfNumber(ausr.getRotation()));
System.out.println("Drehe Seite:" + (pagecount + 1) + " Zielformat: " +
ausr);
this.pagecountrotatedpages++;
break;
}
curwidth = ausr.getM5();
curheight = ausr.getM6();
if ( ( (pagecount + 1) % 2) == 0) {
if ( (Math.abs(curwidth - width) > tolerancex) ||
(Math.abs(curheight - height) > tolerancey)) {
Seitehinzufuegen(page, count_in_leaf, writer, arl);
this.pagecountinsertedpages++;
}
}
/**
* Bei ungeraden Seiten die Seitenabmessungen speichern
*/
if ( ( (pagecount + 1) % 2) == 1) {
width = curwidth;
height = curheight;
lastpage = page;
}
pageInh.add(pagecount, page);
pagecount++;
}
else {
page.put(PdfName.TYPE, PdfName.PAGES);
ArrayList kids = kidsPR.getArrayList();
for (int k = 0; k < kids.size(); ++k) {
PdfDictionary kid = (PdfDictionary) PdfReader.getPdfObject( (
PRIndirectReference) kids.get(
k));
iteratePages(kid, pdfreader, pageInh, k, writer);
}
}
}
private void Seitehinzufuegen(PdfDictionary page, int count_in_leaf,
PdfWriter writer,
ArrayList arl) throws IOException {
System.out.print("aenderung!");
PdfDictionary parent = (PdfDictionary) PdfReader.getPdfObject(page.get(
PdfName.PARENT));
PdfArray kids = (PdfArray) PdfReader.getPdfObject(parent.get(PdfName.KIDS));
PdfIndirectReference ref = writer.getPdfIndirectReference();
kids.getArrayList().add(count_in_leaf, ref);
PdfDictionary newPage = new PdfDictionary(PdfName.PAGE);
newPage.merge(lastpage);
newPage.remove(PdfName.CONTENTS);
newPage.remove(PdfName.ANNOTS);
newPage.put(PdfName.RESOURCES, new PdfDictionary());
writer.addToBody(newPage, ref);
PdfNumber count = null;
while (parent != null) {
count = (PdfNumber) PdfReader.getPdfObject(parent.get(PdfName.COUNT));
// System.out.println("Seitenzahl:" + count.intValue());
parent.put(PdfName.COUNT, new PdfNumber(count.intValue() + 1));
parent = (PdfDictionary) PdfReader.getPdfObject(parent.get(PdfName.PARENT));
}
System.out.println("Seite:" + (pagecount + 1) + " nr in leaf:" +
count_in_leaf + " arl x:" +
arl.get(0) + " y:" + arl.get(1) + " width:" + arl.get(2) +
" height:" + arl.get(3));
}
/**
* @see com.lowagie.tools.plugins.AbstractTool#execute()
*/
public void execute() {
try {
if (getValue("srcfile") == null) {
throw new InstantiationException("You need to choose a sourcefile");
}
File src = (File) getValue("srcfile");
if (getValue("destfile") == null) {
throw new InstantiationException(
"You need to choose a destination file");
}
File dest = (File) getValue("destfile");
pagecountinsertedpages = 0;
pagecountrotatedpages = 0;
pagecount = 0;
PdfReader reader = new PdfReader(src.getAbsolutePath());
PdfStamper stp = new PdfStamper(reader, new FileOutputStream(dest));
PdfWriter writer = stp.getWriter();
ArrayList pageInh = new ArrayList();
PdfDictionary catalog = reader.getCatalog();
PdfDictionary rootPages = (PdfDictionary) PdfReader.getPdfObject(catalog.
get(
PdfName.PAGES));
iteratePages(rootPages, reader, pageInh, 0, writer);
if ( ( (pagecount) % 2) == 1) {
appendemptypageatend(reader, writer);
this.pagecountinsertedpages++;
}
stp.close();
System.out.println("In " + dest.getAbsolutePath() + " Insgesamt= " +
pagecount +
" Eingefügte Seiten=" + this.getPagecountinsertedpages() +
" Gedrehte Seiten=" +
this.getPagecountrotatedpages());
}
catch (Exception e) {
e.printStackTrace(System.out);
}
}
private void appendemptypageatend(PdfReader reader, PdfWriter writer) throws
IOException {
System.out.println("Letzte Seite ungerade. Fuege Seite an!");
PdfDictionary page = reader.getPageN(reader.getNumberOfPages());
PdfDictionary parent = (PdfDictionary) PdfReader.getPdfObject(page.get(
PdfName.PARENT));
PdfArray kids = (PdfArray) PdfReader.getPdfObject(parent.get(PdfName.KIDS));
PdfIndirectReference ref = writer.getPdfIndirectReference();
kids.add(ref);
PdfDictionary newPage = new PdfDictionary(PdfName.PAGE);
newPage.merge(lastpage);
newPage.remove(PdfName.CONTENTS);
newPage.remove(PdfName.ANNOTS);
newPage.put(PdfName.RESOURCES, new PdfDictionary());
writer.addToBody(newPage, ref);
PdfNumber count = null;
while (parent != null) {
count = (PdfNumber) PdfReader.getPdfObject(parent.get(PdfName.COUNT));
parent.put(PdfName.COUNT, new PdfNumber(count.intValue() + 1));
parent = (PdfDictionary) PdfReader.getPdfObject(parent.get(PdfName.PARENT));
}
}
public int getPagecountinsertedpages() {
return pagecountinsertedpages;
}
public int getPagecountrotatedpages() {
return pagecountrotatedpages;
}
/**
* @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument)
*/
public void valueHasChanged(ToolArgument arg) {
if (internalFrame == null) {
// if the internal frame is null, the tool was called from the commandline
return;
}
// represent the changes of the argument in the internal frame
}
/**
* Normalize PDF file.
* @param args
*/
public static void main(String[] args) {
Normalize tool = new Normalize();
if (args.length < 2) {
System.err.println(tool.getUsage());
}
tool.setArguments(args);
tool.execute();
}
/**
* @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF()
*/
protected File getDestPathPDF() throws InstantiationException {
throw new InstantiationException("There is more than one destfile.");
}
public class Ausrichtung {
static final float tolerance = 60;
static final int UNKNOWN = 0;
static final int A4Portrait = 1;
static final int A4Landscape = 2;
static final int A3Portrait = 3;
static final int A3Landscape = 4;
float rotation;
Rectangle rect;
float m5;
float m6;
int type;
public Ausrichtung() {
this(0, new Rectangle(1, 1));
}
public Ausrichtung(float rotation, Rectangle unrotatedoriginalrect) {
this.rotation = rotation;
if ( (rotation == 90) || (rotation == 270)) {
rect = unrotatedoriginalrect.rotate();
}
else {
rect = unrotatedoriginalrect;
}
m5 = rect.width();
m6 = rect.height();
klassifiziere();
}
private void klassifiziere() {
if (Math.abs(rect.width() - 595) < tolerance &&
Math.abs(rect.height() - 842) < tolerance) {
this.type = this.A4Portrait;
}
else if (Math.abs(rect.width() - 842) < tolerance &&
Math.abs(rect.height() - 595) < tolerance) {
this.type = this.A4Landscape;
}
else if (Math.abs(rect.width() - 1190) < tolerance &&
Math.abs(rect.height() - 842) < tolerance) {
this.type = this.A3Landscape;
}
else if (Math.abs(rect.width() - 842) < tolerance &&
Math.abs(rect.height() - 1190) < tolerance) {
this.type = this.A3Portrait;
}
else {
type = this.UNKNOWN;
}
}
public float getM5() {
return m5;
}
public float getM6() {
return m6;
}
public String toString() {
String back;
switch (type) {
case UNKNOWN:
back = rect.width() + "*" + rect.height();
break;
case A3Landscape:
back = "A3 Landscape";
break;
case A3Portrait:
back = "A3 Portrait";
break;
case A4Landscape:
back = "A4 Landscape";
break;
case A4Portrait:
back = "A4 Portrait";
break;
default:
back = "";
}
return back;
}
public void rotate() {
rect = rect.rotate();
m5 = rect.width();
m6 = rect.height();
rotation += 90;
rotation = rotation % 360;
klassifiziere();
}
public float getRotation() {
return rotation;
}
}
}