Is there a way to retrieve the origin of a mediabox?

TIA,
Frederik


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of Paulo
Soares
Sent: woensdag 12 december 2007 12:20
To: Post all your questions about iText here
Subject: Re: [iText-questions] importing an existing pdf file


The transformations you are using assume that the pdf's mediabox has a
(0,0) origin and that's not the case with your pdf.

Paulo 

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On 
> Behalf Of Decoster Frederik
> Sent: Wednesday, December 12, 2007 8:16 AM
> To: [email protected]
> Subject: [iText-questions] importing an existing pdf file
> 
> 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).g
> etCalculatedBaseFont(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());
>     }
> }
> 
> 


Aviso Legal:

Esta mensagem é destinada exclusivamente ao destinatário. Pode conter 
informação confidencial ou legalmente protegida. A incorrecta transmissão desta 
mensagem não significa a perca de confidencialidade. Se esta mensagem for 
recebida por engano, por favor envie-a de volta para o remetente e apague-a do 
seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de 
usar, revelar ou distribuir qualquer parte desta mensagem. 



Disclaimer:

This message is destined exclusively to the intended receiver. It may contain 
confidential or legally protected information. The incorrect transmission of 
this message does not mean the loss of its confidentiality. If this message is 
received by mistake, please send it back to the sender and delete it from your 
system immediately. It is forbidden to any person who is not the intended 
receiver to use, distribute or copy any part of this message.




______________________________________________________________________
This email has been scanned by the Email Security System.
______________________________________________________________________

-------------------------------------------------------------------------
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://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
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