Hi,
 
I want to do following : importing an existing PDF file (scaled so I can add a 
title) into a new one and rotate it if necessary.  My code works fine, however 
I encounter a problem with one particular PDF file (see attachment).
 
The resulting file is even different when I use :
 
document.setPageSize(new 
Rectangle(reader.getPageSizeWithRotation(i).getWidth(), 
reader.getPageSizeWithRotation(i).getHeight()));
    >>> result is a white page with the text 'TEST' (due to method addSomeText

document.setPageSize(reader.getPageSizeWithRotation(i));
    >>> result is a page with half of the imported pdf (and no text).
 
Any ideas?
 
Thx
Frederik
 
 
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.awt.geom.AffineTransform;
import java.io.*;
import java.util.*;
 
public class VdwPdfTest {
 
    private PdfWriter writer;
    private PdfReader reader;
    private Document document;
    private java.io.FileInputStream fis = null;
    private java.io.FileOutputStream fos = null;
    private PdfContentByte cb;
 

    public VdwPdfTest() {
    }
 

    private void init(File source, File destination) throws 
FileNotFoundException, IOException, DocumentException {
        fis = new java.io.FileInputStream(source);
        reader = new PdfReader(fis);
        fos = new java.io.FileOutputStream(destination);
        document = new Document();
        writer = PdfWriter.getInstance(document, fos);
        document.open();
        cb = writer.getDirectContent();
    }
 

    private void importPage(int pagenumber) {
        float factor = 1 - 
70/reader.getPageSizeWithRotation(pagenumber).getHeight();
        float x = (reader.getPageSizeWithRotation(pagenumber).getWidth() - 
reader.getPageSizeWithRotation(pagenumber).getWidth() * factor) / 2;
        Rectangle psize = reader.getPageSizeWithRotation(pagenumber);
        PdfImportedPage page = writer.getImportedPage(reader, pagenumber);
        System.out.println(psize.getWidth() + " - reader - " + 
psize.getHeight());
        System.out.println(page.getWidth() + " - page - " + page.getHeight());
        if (page.getWidth() != psize.getWidth()) {
            System.out.println("ROTATE");
            cb.transform(AffineTransform.getTranslateInstance( x, 
psize.getHeight() - 42));
            cb.transform(AffineTransform.getRotateInstance(-Math.PI / 2));
        } else
            cb.transform(AffineTransform.getTranslateInstance( x, 28));
        cb.transform(AffineTransform.getScaleInstance(factor, factor)) ;
        cb.addTemplate(page, 0,0);
    }
 
    private void close() throws Exception {
        document.close();
        writer.close();
        fos.close();
        fis.close();
    }
 
    private void addSomeText() {
        cb.beginText();
        
cb.setFontAndSize(FontFactory.getFont(FontFactory.HELVETICA).getCalculatedBaseFont(true),
 14);
        cb.showTextAligned(Element.ALIGN_LEFT, "TEST", 10, 10, 0);
        cb.endText();
    }
 
    public void create(File source, File destination, String docnr) throws 
Exception {
        init(source, destination);
        for (int i=1, n=reader.getNumberOfPages(); i<=n; i++) {
            //document.setPageSize(new 
Rectangle(reader.getPageSizeWithRotation(i).getWidth(), 
reader.getPageSizeWithRotation(i).getHeight()));
            document.setPageSize(reader.getPageSizeWithRotation(i));
            document.newPage();
            importPage(i);
            addSomeText();
        }
        close();
    }
 
    public static void main(String[] args) throws Exception {
            String docnr = "60-600983";
            File source = new File("c:\\temp\\" + docnr + ".pdf");
            File dest = new File(source.getParent() + "\\" + docnr + 
"_new.pdf");
            (new VdwPdfTest()).create(source, dest, docnr);
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + 
dest.getAbsolutePath());
    }
}

Attachment: 60-600983.pdf
Description: 60-600983.pdf

-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to