Here's a complete example. Note that if you wanted to keep the crop box in
the page you'd have to use PdfWriter.setCropBox(). This example makes the
page size the size of the crop box.
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class import_pages {
public static void main(String[] args) {
Document document = null;
try {
PdfReader reader = new
PdfReader("e:\\fcdr\\downloads\\cmyk.pdf");
Rectangle psize = reader.getCropBox(1);
Rectangle media = new Rectangle(psize.width(), psize.height());
document = new Document(media, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream("c:\\crops2.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page1 = writer.getImportedPage(reader, 1);
page1.setBoundingBox(psize);
cb.addTemplate(page1, 1, 0, 0, 1, -psize.left(),
-psize.bottom());
document.close();
System.out.println("Finished.");
}
catch (Exception de) {
de.printStackTrace();
}
try {
document.close();
}
catch (Exception de) {
de.printStackTrace();
}
}
}
Best Regards,
Paulo Soares
> -----Original Message-----
> From: Waymon Ho [SMTP:[EMAIL PROTECTED]]
> Sent: Wednesday, July 03, 2002 18:12
> To: [EMAIL PROTECTED]
> Subject: help on copy document
>
> Thanks Paulo for all your help, but I still can't get the page to have
> same
> margin as the old document. You suggest to use setBoundingBox, so this is
>
> what I try,
> // get the page from old document
> PdfImportedPage page1 = writer.getImportedPage(reader, i);
> //set the size of the page
> page1.setBoundingBox(reader.getCropBox(i))
> //add first template
> cb.addTemplate(page1, 1, 0, 0, 1, 0, 0);
> //add second template
> cb.addTemplate(template,1,.3f,-.3f,1,0,0);
>
> So I wonder if my logic was correct, did I do that right? Or I should put
>
> setBoundingBox somewhere else?
>
> thanks for all your help.
>
> Waymon
>
> >From: Paulo Soares <[EMAIL PROTECTED]>
> >To: "'Waymon Ho'" <[EMAIL PROTECTED]>,
> >[EMAIL PROTECTED]
> >Subject: RE: [iText-questions] help on copy document
> >Date: Tue, 2 Jul 2002 19:46:13 +0100
> >
> >
> >
> > > -----Original Message-----
> > > From: Waymon Ho [SMTP:[EMAIL PROTECTED]]
> > > Sent: Tuesday, July 02, 2002 19:19
> > > To: [EMAIL PROTECTED]
> > > Subject: [iText-questions] help on copy document
> > >
> > > Hi all,
> > >
> > > I have questions on how to copy the pdf file. Below is my code on how
> I
> > > copy the exist pdf, read and write it to the new pdf file. I
> basically
> > > fellow and modify from example ch0112. But after running the program,
> I
> > > observe there are couple problem with it.
> > >
> > > 1. The original pdf file was contain 5077 kb, and the new pdf file
> only
> > > have
> > > 2094 kb. So, I wonder why the new file is much smaller than the
> >original
> > > one. Is there anything missing? I know both document have to same
> >number
> > >
> > > of pages. I know the bookmarks didn��t get copy over. Is that the
> > > reason?
> > >
> > Only the content is copied and nothing is missing. The difference
> >may also be due to the pdf file being an updated file and, as such,
> >containing object that are used no more. If you open the file in Acrobat
> >(full) and do a "Save As" does it reduce the size?
> >
> > > 2. I test the program with different pdf file, and I notice on some
> pdf
> > > file, all the margin is the same, however some pdf file, the program
> >will
> > > change the margin to be double than the original one (all 4 side).
> So,
> >I
> > > wonder what happen with it.
> > >
> > That's the crop box effect. I posted today two ways of getting rid
> >of the extra margin.
> > > <<RE: [iText-questions] Cropping an Imported PDF page>>
> > >
> > > Below is my code, please let me know if I did anything wrong.
> > >
> > There's nothing wrong with your code.
> >
> > Best Regards,
> > Paulo Soares
> >
> > > Thank you so much for you guys help.
> > >
> > > Waymon
> > >
> > > PdfReader reader = new PdfReader("M:\\programming.pdf");
> > > // we retrieve the total number of pages
> > > int n = reader.getNumberOfPages();
> > > // we retrieve the size of the first page
> > > Rectangle psize = reader.getPageSize(1);
> > > float width = psize.width();
> > > float height = psize.height();
> > >
> > > // step 1: creation of a document-object
> > > Document document = new Document(psize, 0, 0, 0, 0);
> > > // step 2: we create a writer that listens to the document
> > > PdfWriter writer = PdfWriter.getInstance(document, new
> > > FileOutputStream("M:\\trytry.pdf"));
> > >
> > > // step 3: we open the document
> > >
> > > document.open();
> > > // step 4: we add content
> > > PdfContentByte cb = writer.getDirectContent();
> > > PdfTemplate template = cb.createTemplate(width, height);
> > > while (i < n) {
> > > document.newPage();
> > > p++;
> > > i++;
> > > PdfImportedPage page1 = writer.getImportedPage(reader,
>
> >i);
> > > //add the existing template
> > > cb.addTemplate(page1, 1, 0, 0, 1, 0, 0);
> > > System.err.println("processed page " + i);
> > >
> > > cb.beginText();
> > >
> > > cb.endText();
> > > }
> > > // step 5: we close the document
> > > document.close();
> > > System.out.println("Done");
> > > }
> > > catch (Exception de) {
> > > de.printStackTrace();
> > > }
> > >
> > >
> > >
> > >
> > > _________________________________________________________________
> > > MSN Photos is the easiest way to share and print your photos:
> > > http://photos.msn.com/support/worldwide.aspx
> > >
> > >
> > >
> > > -------------------------------------------------------
> > > This sf.net email is sponsored by:ThinkGeek
> > > Welcome to geek heaven.
> > > http://thinkgeek.com/sf
> > > _______________________________________________
> > > iText-questions mailing list
> > > [EMAIL PROTECTED]
> > > https://lists.sourceforge.net/lists/listinfo/itext-questions
> ><< message3.txt >>
>
>
>
>
> _________________________________________________________________
> MSN Photos is the easiest way to share and print your photos:
> http://photos.msn.com/support/worldwide.aspx
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
No, I will not fix your computer.
http://thinkgeek.com/sf
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions