Interview Questions

Java Create PDF Document Using iText

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE IS DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package in.techdive;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

/**
 * This class contains methods used to create a simple PDF document using iText API.
 */

public class PDFDocWriter
{

        /**
         * The program execution starts from this main method
         */

        public static void main(String[] args)
        {

                PDFDocWriter pdf = new PDFDocWriter();
                String fileName = "H:\\TechDive.pdf";
                String fileContent = "Welcome to TechDive!"
                + "Tech Dive heralds the arrival of a community of Developers who share, collaborate and exchange ideas, concepts, technical know-how. This forum lets you take a deep dive in technical topics that are hot and happening as well as on legacy systems. "
                + "The idea of the forum is to ensure collaboration amongst developers through exchange of ideas/know-how/concepts so their technical skills are enhanced."
                + "We plan to bring in experienced professionals on board so content/blog written is authentic and precise."
                + "Come, join us and be a part of new way of collaboration! \n\n http:\\\\www.techdive.in";
                pdf.wrtieToPDF(fileName, fileContent);
        }

        /**
         * The method creates a PDF document with the given fileName and file content
         */

        public void wrtieToPDF(String fileName, String fileContent)
        {
                try
                {
                        Document objDocument = null;
                        ByteArrayOutputStream objBAOStream = null;
                        PdfWriter objPdfWriter = null;

                        objDocument = new Document(PageSize.A4.rotate());
                        objBAOStream = new ByteArrayOutputStream();
                        objPdfWriter = PdfWriter.getInstance(objDocument, objBAOStream);

                        objDocument.open();
                        objDocument.add(new Paragraph(fileContent));

                        objDocument.close();

                        FileOutputStream out1 = new FileOutputStream(fileName);
                        objBAOStream.writeTo(out1);
                        out1.close();
                        objPdfWriter.close();
                }
                catch (Exception e)
                {
                        System.out.println(e);
                }
        }
}