Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Insert pages pdf

                         

In this program we are going to insert a new blank pages in pdf  file  through java program.

In this example we need iText.jar file, without this jar file we never compile our application . The package we need to import is java.io.FileOutputStream, java.io.IOException, com.lowagie.text.Document, com.lowagie.text.DocumentException, com.lowagie.text.Paragraph, com.lowagie.text.Rectangle,com.lowagie.text.pdf.PdfWriter
..

The java.io.FileOutputStream and java.io.IOException classes are used to create file and throw the exception, if there is any exception occurs while creating a file. The com.lowagie.text.Document class is used to create a document object,  com.lowagie.text.DocumentException is used to throw exception if there is any exception. com.lowagie.text.Paragraph classes is used to make paragraph , com.lowagie.text.Rectangle classes is used to create object of Rectangle class which will be used for setting the left, right, top, bottom margin, com.lowagie.text.pdf.PdfWriter class is used to write the document on a pdf file,.

To make the program for changing the pdf version firstly we will make a class NewpageInsertingPDF
. Remember the name of the file should be such that, if any other person sees the example then just by seeing the name of the program he can understand what the program is going to do without seeing the code. 

Inside this class declare the main method which must throws Exception. After that follow the simple steps find the version:

  1. Create Rectangle object and set length and width of the page.
  2. Create Document object and inside the constructor of Document pass pageSize and four integer which set the left,right,top and bottom margin. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.
  3. Create object of PrintWiter to add the value to document for this we use getInstance() method in which we pass two arguments document and FileOutputStream("NewPagePDF.pdf").
  4. Open the document.
  5. Create a Paragraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously created Document. Inside the constructor of Paragraph we have passed a String .Create paragraph and add it into document .
  6. Add the paragraph in the document.
  7. Use setPageEmpty(boolean pageEmpty) to make sure that page is added. If setPageEmpty(false) then invoking newPage() after blank page will add a newPage().
  8. close the document. The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
public class NewpageInsetingPDF {
     public static void main(String[] args)throws Exception {
           System.out.println("Example of a PDF File  to blank 
pages and new page"
);       
        Rectangle pageSize = new Rectangle(288720);
        Document document = new Document(pageSize, 36187272);
        PdfWriter writer=PdfWriter.getInstance(document,
 
new FileOutputStream("NewPagePDF.pdf"));
        document.open();
        document.add(new Paragraph("First Page-->>Rose India"));
        document.add(new Paragraph("Next Two blank Pages"));
        document.newPage();
        writer.setPageEmpty(false);
        document.newPage();
        writer.setPageEmpty(false);
        document.newPage();
        document.add(new Paragraph("Last Page with some
 data-->>RoseIndai.net"
));      
        document.close();
    }
}

The output of the program is given below:

 


Download this example.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
remove rectangle awt
Time
pagination taglib
static
Java Pass Value
array element
Date Examples java
techiques to use subst
ttcn
selectonelistbox examp
techiques to use subst
string arrays jsp pro
Convert IST time to no
java.nio
jsp to microsoft sql
optgroup
where we place ...-in
collection classes
a sample example forh
rmi
parseInt
spring application
Sample paging program
Flex TreeGrid
Tiles definition richf
download package com.s
button launches dialog
sleep
getRequestDispatcher s
how to override...ng i
ibatis fundamental
Hibernate Mapping
jQuery To Slide Effect
Nested WHILE LOOPS
pos
source code for change
)How
textfield password
Delete database
java random numbers
checkbox struts 1
FILEUPLOADEXAMPLEWITHD
day number given date
update all columns of
create xml using java
J2EE interview questio
PHP User Authenticatio
jdbc connections
executequery
Combattons la programm
remove rectangle
string variable
Merge Sort In Java
Flash Math Physics Fla
Photoshop Effects Crea
HQL
Photoshop Basics Kodak
lazy load in hibernate
increase size of strut
logout code in jsp
calculator code usind
java strings
what is static inner c
using set in java
EncoderTest
password match jsf
Copy One Database Tabl
checkbox button enable
simple calendar
ajax
embedded java script i
how to use tab control
HSSF clone cell
Arraylist text file
What are the defined J
jsp applet netbeans
linked list
SwingWorker
launch dialog with but
charts using POI
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.