I had a similar problem that I solved in a slightly different way. I have pdf
files that are many files combined into one that I
needed to split. You will need to know enough to be able to get page objects
for the pages you want. You can do this using page
counts (if all of the documents have the same number of pages), by bookmarks
(if the document was set up with bookmarks delineating
the original documents) or by parsing the stream data (which is very hard to
do).
Once you have a way to know which pages you need, you can use this:
private int getPageNum(PdfDictionary page) throws IllegalArgumentException
{
int pageNum = -1;
for(int i = 1; i <= this.pageCount; i++)
{
if(page.equals(this.reader.getPageN(i)))
{
pageNum = i;
break;
}
}
if(pageNum < 0)
{
this.printDictionaryEntries(page);
throw new IllegalArgumentException("Page does not exist.");
}
return pageNum;
}
To determine which page you have (it isn't elegant, but it works) so that you
can do this:
Document document = new Document(this.reader.getPageSizeWithRotation(1));
FileOutputStream fos = new FileOutputStream(fileName);
PdfCopy writer = new PdfCopy(this.document, fos);
document.open();
PdfArray a = null;
a = (PdfArray)this.reader.getPdfObject(next.get(PdfName.DEST));
pageDic =
(PdfDictionary)this.reader.getPdfObject((PdfObject)a.getArrayList().get(0));
addPage(getPageNum(pageDic));
document.close();
To add the page to your new pdf file. The above does a single page so you would
have to alter it to accommodate whatever type of
multiple page choice algorithm you use. The biggest obstacle I ran into was
finding a page number for PdfCopy to use, since the
pages don't know their own page numbers.
Of course all this assumes that you don't need to split a page. If you do, then
I think you need to go back and get the project
scope changed.
Edward W. Rouse
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On
> Behalf Of 1T3XT info
> Sent: Wednesday, March 12, 2008 5:28 AM
> To: Post all your questions about iText here
> Subject: Re: [iText-questions] Split a PDF file according to
> offset andlength
>
>
> osmoz7 wrote:
> > I looked the examples in the tutorials and the book, but as I said,
> > I don't want to cut the PDF according to pages but
> according to offsets
> > and lengths.
>
> Byte offsets and byte array lengths?
> Or Y coordinates on a page?
> Your code sample suggests the former, which is madness.
>
> If you want pieces of a page based on offset, you will
> have to import COMPLETE PAGES, because PDF is a PDL and
> therefore PAGE BASED. (PDL = Page Description Language.)
> If you have the offsets, you can clip the pages.
> How to do that? That's described in the book.
>
> > One thing I forgot to mention in my first message is that
> the PDF file that
> > I want to split is already a concatenation of several PDF
> files. So I just
> > want to get the pieces back.
>
> That's not a good idea. You should have kept the original pieces.
> Read the conclusion of section 3.2.1 on page 80:
> "creating PDF is a one-way process."
>
> > I don't think I need to know about table, resource
> dictionary, etc. to do
> > that.
>
> You need a sound knowledge about PDF if you want to manipulate
> a PDF file the way you did in your code sample. Chapter 18 of
> the book warns you: "A recurring remark about PdfWriter, PdfCopy,
> and PdfStamper, is that the API isn't intuitive. Why can't you just
> take reader objects, select pages, and then concatenate all of them
> using a writer? Or even better: Why can't you take the content stream
> of a page, look up some words, and replace them or insert
> extra content
> at that specific position?" Ignoring the answer to these questions
> (offered in the book that you say you've looked at) is not very wise.
>
> > As I mentioned at the beginning, you don't answer my question.
>
> Some questions don't have an answer. See also:
> http://itext.ugent.be/library/question.php?id=42
>
> > Hope someone else could help me.
>
> I don't think that will happen. Your current question is
> unanswerable. It makes me think of the Fredric Brown story
> "The Weapon": you don't give a real gun to a child as a play toy.
>
> Similarly you don't give people who refuse to learn (saying "I don't
> think I need to know about table, resource dictionary, etc.") an
> assignment like this one. By the way: I didn't talk about
> "a table", I was talking about "the Cross-Reference table".
> There's a huge difference.
>
> Bottom line: you won't understand any answer if you're not willing
> to learn. If you want to cut pieces of pages for reuse, you have
> all the answers in the book.
> --
> This answer is provided by 1T3XT BVBA
>
> --------------------------------------------------------------
> -----------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> 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/
>
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
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/