<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: iText tutorial: Merge &amp; Split PDF files using iText JAR</title>
	<atom:link href="http://viralpatel.net/blogs/2009/06/itext-tutorial-merge-split-pdf-files-using-itext-jar.html/feed" rel="self" type="application/rss+xml" />
	<link>http://viralpatel.net/blogs/2009/06/itext-tutorial-merge-split-pdf-files-using-itext-jar.html</link>
	<description>Tutorials, Java, J2EE, Struts, AJAX, JavaScript, CSS, Web 2.0, MySQL, Articles</description>
	<lastBuildDate>Fri, 12 Mar 2010 08:30:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Viral Patel</title>
		<link>http://viralpatel.net/blogs/2009/06/itext-tutorial-merge-split-pdf-files-using-itext-jar.html/comment-page-1#comment-11903</link>
		<dc:creator>Viral Patel</dc:creator>
		<pubDate>Fri, 08 Jan 2010 10:29:25 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=1406#comment-11903</guid>
		<description>Thanks Goblin for the code. I appreciate your effort of sharing this here.</description>
		<content:encoded><![CDATA[<p>Thanks Goblin for the code. I appreciate your effort of sharing this here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Goblin_Queen</title>
		<link>http://viralpatel.net/blogs/2009/06/itext-tutorial-merge-split-pdf-files-using-itext-jar.html/comment-page-1#comment-11902</link>
		<dc:creator>Goblin_Queen</dc:creator>
		<pubDate>Fri, 08 Jan 2010 09:33:47 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=1406#comment-11902</guid>
		<description>I would like to paste my code here to split a PDF based on bookmarks using iText. All my bookmarks are on level1. I based my code sample on the example on this page, and that&#039;s why I would like to place my code here. I couldn&#039;t really find any example of splitting a PDF based on bookmarks with iText, so I thought it could help other people if I put my code on this page:

&lt;pre class=&quot;brush: java;&quot;&gt;
public static void splitPDFByBookmarks(String pdf, String outputFolder){
        try
        {
            PdfReader reader = new PdfReader(pdf);
            //List of bookmarks: each bookmark is a map with values for title, page, etc
            List&lt;HashMap&gt; bookmarks = SimpleBookmark.getBookmark(reader);
            for(int i=0; i&lt;bookmarks.size(); i++){
                HashMap bm = bookmarks.get(i);
                HashMap nextBM = i==bookmarks.size()-1 ? null : bookmarks.get(i+1);
                //In my case I needed to split the title string
                String title = ((String)bm.get(&quot;Title&quot;)).split(&quot; &quot;)[2];
                
                log.debug(&quot;Titel: &quot; + title);
                String startPage = ((String)bm.get(&quot;Page&quot;)).split(&quot; &quot;)[0];
                String startPageNextBM = nextBM==null ? &quot;&quot; + (reader.getNumberOfPages() + 1) : ((String)nextBM.get(&quot;Page&quot;)).split(&quot; &quot;)[0];
                log.debug(&quot;Page: &quot; + startPage);
                log.debug(&quot;------------------&quot;);
                extractBookmarkToPDF(reader, Integer.valueOf(startPage), Integer.valueOf(startPageNextBM), title + &quot;.pdf&quot;,outputFolder);
            }
        }
        catch (IOException e)
        {
            log.error(e.getMessage());
        }
    }
    
    private static void extractBookmarkToPDF(PdfReader reader, int pageFrom, int pageTo, String outputName, String outputFolder){
        Document document = new Document();
        OutputStream os = null;
        
        try{
            os = new FileOutputStream(outputFolder + outputName);

            // Create a writer for the outputstream
            PdfWriter writer = PdfWriter.getInstance(document, os);
            document.open();
            PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data
            PdfImportedPage page;
    
            while(pageFrom &lt; pageTo) {
                document.newPage();
                page = writer.getImportedPage(reader, pageFrom);
                cb.addTemplate(page, 0, 0);
                pageFrom++;
            }
            
            os.flush();
            document.close();
            os.close();
        }catch(Exception ex){
            log.error(ex.getMessage());
        }finally {
            if (document.isOpen())
                document.close();
            try {
                if (os != null)
                    os.close();
            } catch (IOException ioe) {
                log.error(ioe.getMessage());
            }
        }
    }
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I would like to paste my code here to split a PDF based on bookmarks using iText. All my bookmarks are on level1. I based my code sample on the example on this page, and that&#8217;s why I would like to place my code here. I couldn&#8217;t really find any example of splitting a PDF based on bookmarks with iText, so I thought it could help other people if I put my code on this page:</p>
<pre class="brush: java;">
public static void splitPDFByBookmarks(String pdf, String outputFolder){
        try
        {
            PdfReader reader = new PdfReader(pdf);
            //List of bookmarks: each bookmark is a map with values for title, page, etc
            List&lt;HashMap&gt; bookmarks = SimpleBookmark.getBookmark(reader);
            for(int i=0; i&lt;bookmarks.size(); i++){
                HashMap bm = bookmarks.get(i);
                HashMap nextBM = i==bookmarks.size()-1 ? null : bookmarks.get(i+1);
                //In my case I needed to split the title string
                String title = ((String)bm.get("Title")).split(" ")[2];

                log.debug("Titel: " + title);
                String startPage = ((String)bm.get("Page")).split(" ")[0];
                String startPageNextBM = nextBM==null ? "" + (reader.getNumberOfPages() + 1) : ((String)nextBM.get("Page")).split(" ")[0];
                log.debug("Page: " + startPage);
                log.debug("------------------");
                extractBookmarkToPDF(reader, Integer.valueOf(startPage), Integer.valueOf(startPageNextBM), title + ".pdf",outputFolder);
            }
        }
        catch (IOException e)
        {
            log.error(e.getMessage());
        }
    }

    private static void extractBookmarkToPDF(PdfReader reader, int pageFrom, int pageTo, String outputName, String outputFolder){
        Document document = new Document();
        OutputStream os = null;

        try{
            os = new FileOutputStream(outputFolder + outputName);

            // Create a writer for the outputstream
            PdfWriter writer = PdfWriter.getInstance(document, os);
            document.open();
            PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data
            PdfImportedPage page;

            while(pageFrom &lt; pageTo) {
                document.newPage();
                page = writer.getImportedPage(reader, pageFrom);
                cb.addTemplate(page, 0, 0);
                pageFrom++;
            }

            os.flush();
            document.close();
            os.close();
        }catch(Exception ex){
            log.error(ex.getMessage());
        }finally {
            if (document.isOpen())
                document.close();
            try {
                if (os != null)
                    os.close();
            } catch (IOException ioe) {
                log.error(ioe.getMessage());
            }
        }
    }
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Viral Patel</title>
		<link>http://viralpatel.net/blogs/2009/06/itext-tutorial-merge-split-pdf-files-using-itext-jar.html/comment-page-1#comment-11838</link>
		<dc:creator>Viral Patel</dc:creator>
		<pubDate>Fri, 25 Dec 2009 19:54:01 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=1406#comment-11838</guid>
		<description>@Prem, This example only splits the PDF on basis of page numbers. I will definitely try to write code for splitting pdf on basis of size and update you.</description>
		<content:encoded><![CDATA[<p>@Prem, This example only splits the PDF on basis of page numbers. I will definitely try to write code for splitting pdf on basis of size and update you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: prem</title>
		<link>http://viralpatel.net/blogs/2009/06/itext-tutorial-merge-split-pdf-files-using-itext-jar.html/comment-page-1#comment-11825</link>
		<dc:creator>prem</dc:creator>
		<pubDate>Wed, 23 Dec 2009 05:39:38 +0000</pubDate>
		<guid isPermaLink="false">http://viralpatel.net/blogs/?p=1406#comment-11825</guid>
		<description>Hi , 
I want to split the pdf if my pdf exceeds the given size. 
For Example, 
If my pdf size is 12MB,I want to split the pdf in to 5MB parts. 
So i need to split the pdf in to 3, 5MB,5MB,2MB respectively. 
Pls let me know whether this is possible. 

Regards, 
Prem</description>
		<content:encoded><![CDATA[<p>Hi ,<br />
I want to split the pdf if my pdf exceeds the given size.<br />
For Example,<br />
If my pdf size is 12MB,I want to split the pdf in to 5MB parts.<br />
So i need to split the pdf in to 3, 5MB,5MB,2MB respectively.<br />
Pls let me know whether this is possible. </p>
<p>Regards,<br />
Prem</p>
]]></content:encoded>
	</item>
</channel>
</rss>
