Writing a java program for generating .pdf file with the data of MS-Excel .

Hi all,
My object is write a java program so tht...it'll generate the .pdf file after retriving the data from MS-Excel file.
I used POI HSSF to read the data from MS-Excel and used iText to generate .pdf file:
My Program is:
* Created on Apr 13, 2005
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
package forums;
import java.io.*;
import java.awt.Color;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import com.lowagie.text.Font.*;
import com.lowagie.text.pdf.MultiColumnText;
import com.lowagie.text.Phrase.*;
import net.sf.hibernate.mapping.Array;
import org.apache.poi.hssf.*;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hssf.usermodel.*;
import com.lowagie.text.Phrase.*;
import java.util.Iterator;
* Generates a simple 'Hello World' PDF file.
* @author blowagie
public class pdfgenerator {
     * Generates a PDF file with the text 'Hello World'
     * @param args no arguments needed here
     public static void main(String[] args) {
          System.out.println("Hello World");
          Rectangle pageSize = new Rectangle(916, 1592);
                    pageSize.setBackgroundColor(new java.awt.Color(0xFF, 0xFF, 0xDE));
          // step 1: creation of a document-object
          //Document document = new Document(pageSize);
          Document document = new Document(pageSize, 132, 164, 108, 108);
          try {
               // step 2:
               // we create a writer that listens to the document
               // and directs a PDF-stream to a file
               PdfWriter writer =PdfWriter.getInstance(document,new FileOutputStream("c:\\weeklystatus.pdf"));
               writer.setEncryption(PdfWriter.STRENGTH128BITS, "Hello", "World", PdfWriter.AllowCopy | PdfWriter.AllowPrinting);
//               step 3: we open the document
                         document.open();
               Paragraph paragraph = new Paragraph("",new Font(Font.TIMES_ROMAN, 13, Font.BOLDITALIC, new Color(0, 0, 255)));
               POIFSFileSystem pofilesystem=new POIFSFileSystem(new FileInputStream("D:\\ESM\\plans\\weekly report(31-01..04-02).xls"));
               HSSFWorkbook hbook=new HSSFWorkbook(pofilesystem);
               HSSFSheet hsheet=hbook.getSheetAt(0);//.createSheet();
               Iterator rows = hsheet.rowIterator();
                              while( rows.hasNext() ) {
                                   Phrase phrase=new Phrase();
                                   HSSFRow row = (HSSFRow) rows.next();
                                   //System.out.println( "Row #" + row.getRowNum());
                                   // Iterate over each cell in the row and print out the cell's content
                                   Iterator cells = row.cellIterator();
                                   while( cells.hasNext() ) {
                                        HSSFCell cell = (HSSFCell) cells.next();
                                        //System.out.println( "Cell #" + cell.getCellNum() );
                                        switch ( cell.getCellType() ) {
                                             case HSSFCell.CELL_TYPE_STRING:
                                             String stringcell=cell.getStringCellValue ()+" ";
                                             writer.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);
                                             phrase.add(stringcell);
                                        // document.add(new Phrase(string));
                                                  System.out.print( cell.getStringCellValue () );
                                                  break;
                                             case HSSFCell.CELL_TYPE_FORMULA:
                                                       String stringdate=cell.getCellFormula()+" ";
                                                       writer.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);
                                                       phrase.add(stringdate);
                                             System.out.print( cell.getCellFormula() );
                                                       break;
                                             case HSSFCell.CELL_TYPE_NUMERIC:
                                             String string=String.valueOf(cell.getNumericCellValue())+" ";
                                                  writer.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);
                                                  phrase.add(string);
                                                  System.out.print( cell.getNumericCellValue() );
                                                  break;
                                             default:
                                                  //System.out.println( "unsuported sell type" );
                                                  break;
                                   document.add(new Paragraph(phrase));
                                   document.add(new Paragraph("\n \n \n"));
               // step 4: we add a paragraph to the document
          } catch (DocumentException de) {
               System.err.println(de.getMessage());
          } catch (IOException ioe) {
               System.err.println(ioe.getMessage());
          // step 5: we close the document
          document.close();
My Input from MS-Excel file is:
     Planning and Tracking Template for Interns                                                                 
     Name of the Intern     N.Kesavulu Reddy                                                            
     Project Name     Enterprise Sales and Marketing                                                            
     Description     Estimated Effort in Hrs     Planned/Replanned          Actual          Actual Effort in Hrs     Complexity     Priority     LOC written new & modified     % work completion     Status     Rework     Remarks
S.No               Start Date     End Date     Start Date     End Date                                        
1     setup the configuration          31/01/2005     1/2/2005     31/01/2005     1/2/2005                                        
2     Deploying an application through Tapestry, Spring, Hibernate          2/2/2005     2/2/2005     2/2/2005     2/2/2005                                        
3     Gone through Componentization and Cxprice application          3/2/2005     3/2/2005     3/2/2005     3/2/2005                                        
4     Attend the sessions(tapestry,spring, hibernate), QBA          4/2/2005     4/2/2005     4/2/2005     4/2/2005                                        
     The o/p I'm gettint in .pdf file is:
Planning and Tracking Template for Interns
N.Kesavulu Reddy Name of the Intern
Enterprise Sales and Marketing Project Name
Remarks Rework Status % work completion LOC written new & modified Priority
Complexity Actual Effort in Hrs Actual Planned/Replanned Estimated Effort in Hrs Description
End Date Start Date End Date Start Date S.No
38354.0 31/01/2005 38354.0 31/01/2005 setup the configuration 1.0
38385.0 38385.0 38385.0 38385.0 Deploying an application through Tapestry, Spring, Hibernate
2.0
38413.0 38413.0 38413.0 38413.0 Gone through Componentization and Cxprice application
3.0
38444.0 38444.0 38444.0 38444.0 Attend the sessions(tapestry,spring, hibernate), QBA 4.0
                                   The issues i'm facing are:
When it is reading a row from MS-Excel it is writing to the .pdf file from last cell to first cell.( 2 cell in 1 place, 1 cell in 2 place like if the row has two cells with data as : Name of the Intern: Kesavulu Reddy then it is writing to the .pdf file as Kesavulu Reddy Name of Intern)
and the second issue is:
It is not recognizing the date format..it is recognizing the date in first row only......
Plz Tell me wht is the solution for this...
Regards
[email protected]

Don't double post your question:
http://forum.java.sun.com/thread.jspa?threadID=617605&messageID=3450899#3450899
/Kaj

Similar Messages

  • Generating .csv file with the datas from the database

    Hi Java experts,
    Ihave a current assignment to generate a .CSV file using java getting values from database. The output CSV file may have more than one row and each row should have a end of line delimiter. Any help in giving a template for generating a CSV file accessing from database is greatly appreciated.

    Ihave a current assignment to generate a .CSV file
    using java getting values from database.well, I hope you learn something doing it yourself...
    The output
    CSV file may have more than one row and each row
    should have a end of line delimiter. Any help in
    giving a template for generating a CSV file accessing
    from database is greatly appreciated.How about looking for a JDBC and a file/print stream tutorial?

  • How do I make Adobe Reader NOT my default program for reading pdf files?

    I installed Adobe Reader and made it my default program for reading pdf files.  I'd like to undo that and go back to "preview" as my default.

    Sure you are following instructions:

  • I have a pdf file with the added sounds, so I can not run the sound in adobe reader XI on my tablet samsung galaxi pro (android)

    I have a pdf file with the added sounds, so I can not run the sound in adobe reader XI on my tablet samsung galaxi pro (android)

    Thanks for writing to us. Unfortunately, such advanced javascript support is currently not provided by Adobe Reader for Android.
    Thanks,
    Adobe Reader Team

  • How do I create an interactive PDF file with variable data

    We would like to basically do a 'mail merge' of our list of customers with an interactive PDF file (including videos, menus, etc - not just form fill out and web links) to create a single PDF file that contains multiple mail pieces ... one for each customer ... with each mail piece being customized for that customer.  Customizations would include different greetings (Dear Bob, Dear Dana, etc), as well as different charts based on data unique to the customer, different photographs, etc.
    I've seen that InDesign and Acrobat Professional can be used to create an interactive PDF (such as from http://tv.adobe.com/watch/ask-the-adobe-ones/14-calling-rufus-about-interactive-pdf-making).  However I don't understand how I can insert data from a database, csv file, excel file etc into the PDF file so that each page, or each set of pages, within the PDF can be customized.
    Can anyone point me to a tool to use for this?
    Thanks,
    Bob Kendall

    For that kind of volume and unattended operation, you want InDesign Server – which is the server/high volume edition of INDD.
    From: Adobe Forums <[email protected]<mailto:[email protected]>>
    Reply-To: "[email protected]<mailto:[email protected]>" <[email protected]<mailto:[email protected]>>
    Date: Thu, 3 Nov 2011 06:58:07 -0700
    To: Leonard Rosenthol <[email protected]<mailto:[email protected]>>
    Subject: How do I create an interactive PDF file with variable data
    Re: How do I create an interactive PDF file with variable data
    created by Ti26E31DSxxx<http://forums.adobe.com/people/Ti26E31DSxxx> in PDF Language and Specifications - View the full discussion<http://forums.adobe.com/message/4005459#4005459

  • Automating the process of comparing two PDF file with the help of QTP(Automation Testing Tool)

    Can anybody help me with comparing the two pdf files with the help of QTP.I have Adobe Acrobat installed on my system and i have access to the API.
    Thanks,
    Varun Saini

    I want to find out more about QTP and API. Maybe that is what I want to compare two mechanical drawings for differences between them. (see “More than one pdf file in one window”. Is that what you are looking to do? 9Not necessarily mechanical drawings but some other pdf).

  • How to make the active save-to-file with the data from GPIB ?

    Hello,,everybody !!
    I want to make the active save-to-file with the data from GPIB since starting the measurement. To save-to-file at the end of measurment is somehow risky for losing the data because my measurment have to take for long time (eg. 24-48 hours).
    Thank you in advance for anybody's help !!

    Thanks Dennis,
    I have already append, I got it but still have one small problem that between each line it has the blank line. Example as below ,
    16:40:33 54.24
    16:40:34 54.23
    16:40:35 54.24
    I want to get rid of the blank line in between.. Do you have the idea about it ?

  • How to Generate PDF file on the server

    Hello Everybody !
    I Need to generate a PDF File on the server, and I'm usin the command :
    "OPEN DATASET p_path FOR OUTPUT IN BINARY MODE message l_verro .
           LOOP AT t_lines.
             TRANSFER t_lines-tdline TO p_path.
           ENDLOOP.
           CLOSE DATASET p_path.  "
    The File was generated, but is corrupt . Don't Open the file, It's wrong.
    When I use the function GUI_DOWNLOAD the file is generated  right. But in the server not.
    Can someone help me please ??
    Best Regards,
    Wagner Duarte

    Hi.
    did you check this post?
    Download PDF File to SAP Application Server
    It doest the same as you are asking.
          " Write PDF Binary Data to App Server
        OPEN DATASET lv_filename FOR OUTPUT IN BINARY MODE.
        IF sy-subrc = 0.
          LOOP AT lines INTO ls_data.
            TRANSFER ls_data TO lv_filename.
          ENDLOOP.
        ENDIF.
        CLOSE DATASET lv_filename.
    Regards
    Miguel

  • Looking for help for sharing pdf files with ipad from mac.  Pages only recognizes text.  What app should I use for file sharing pdf documents?

    Looking for advice on how to share pdf files with iPad from Mac.   Pages seems to only recognize text.   What app is recommended for pdf documents?

    Welcome to Apple Support Communities!
    If I understand your question, you want to read pdfs on your iPad, not create or edit.
    Drag pdf files to the iPad icon in Finder while connected to your computer via the USB cable.
    They will show up in the iBooks application
    (If you don't already have iBooks, it's a free download from the App Store.)
    Once installed, there are two buttons at the top left of the Bookshelf.
    One is Books, the other is Collections.
    Tap Collections, and you'll see Books and PDFs.
    Tap PDFs and you're there.

  • How to save dynamically generated PDF files with proper file name?

    Hi,
    I need to save the pdf file with default name which generated by
    dinamically.
    this is the code i am trying to save by default
    protected void processRequest (HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    OutputStream bos = response.getOutputStream ();
    // this returns pdf byte array
    byte  ios[]  = callReport ( reportId_param , fullyQualifiedReportClassName , paramHashMap ) ;
    response.setContentType ("application/pdf");
    // reportId_param and reportName_param supplied from some where
    String docName =  reportId_param + "_" + reportName_param ;
    if( fromDate_req != null && toDate_req != null )
                    String dateStr = fromDate_req + "_to_" + toDate_req ;
                    dateStr = dateStr.replaceAll ( "/" , "_" ) ;
                    docName = docName + "_" + dateStr ;
    // here docName would be like "RP_COM_COLL_01_Message_Details_03_09_2008_to_03_09_2008"
    response.setHeader ("Content-Disposition","inline; filename=" + docName ) ;
    bos.write (ios);
    bos.flush ();
    }the above code produces pdf in browser when calling this URL
    http://localhost:8080/DAD01TN0801027/RWReportServlet
    In browser we can save this PDF by using two ways
    1. File -> Save As
    2. By using Save Icon within the PDF toolBar
    both the actions uses the file name "RWReportServlet" as default instead of using docName
    Can any one help me please
    Regards
    Sakthivel.

    That said, your code is not very efficient. You're storing the complete PDF file in memory. What if the PDF file is fairly large? And what if multiple users download multiple large PDF files simultaneously? Your server would run out of memory. Rather use InputStream instead of a raw byte[]. And preferably buffer it.
    You should also be setting the content-length header by the way.

  • Can't open PDF-Files with the 64Bit Photoshop-Version but with 32Bit Version

    Hello,
    our company use Photoshop in combination with PDF-Files. Normally we can open every of our PDFs with Photoshop.
    But now we have some PDFs that we can't open with the 64Bit version. If we try it the application crashes.
    But we are able to open the same files with the 32Bit version of Photoshop. We assume a bug in the 64Bit Version.
    Please contact me to get the a PDF-Sample-File because i am not allowed to append the file to a public space.
    Regards
    Carsten Evers
    PS: email: [email protected]

    Hi Carsten,
    I'll move this to the PS forums on your behalf for further assistance. 
    Cheers!
    -Sidney

  • Is there a way to "stamp" PDF files with the purchaser's email address?

    I would like to sell PDF reports and/or eBooks via BC, but I would like to be able to stamp each such PDF/eBook with the purchaser's email address.
    There are certainly stand-alone PDF stamper programs and some providers like gumroad.com already offer that feature.
    Anyone actually done it with BC, please?
    Thanks in advance
    Andy

    Thanks for the rapid response, Liam -- even if it's not the response I was hoping for.

  • Upload new pdf file with the same name

    We are trying to upload a pdf file with a specific name (i.e
    file.pdf) that has links to it on many pages of a website. We need
    the pdf file to keep the same name but when we try to upload it,
    Contribute is being 'smart' and changing the name so as to not
    overwrite the file on the server. But we WANT to overwrite the file
    on the server. Any ideas on how we can achieve this in Contribute?
    Thanks so much (in advance) for your help!

    Actually, we have the same problem and I believe the answer
    lies in the abilities that your role has assigned to it. The
    solution that ThinkInk gave works *if you are allowed to delete
    files*. If your role doesn't have that permission, then it will
    just upload another copy of the file and rename it.
    It's rather irritating that there isn't more control over
    this...our server is littered with *thousands and thousands* of
    duplicate files that originate from this situation.
    If your administrator wants you to be able to delete files as
    well, then you could get him/her to give you that permission, and I
    believe this will allow you to overwrite the file by using the
    "Publish file from my computer" option described above.

  • I want to prevent users from saving a PDF form with the data filled in

    I am using Adobe Acrobat X Pro to create a fillable PDF form.  When I open my form in Acrobat Reader XI, it says right at the top "You can save data typed into this form."  I have NOT saved it as Reader Extended PDF. This is a form that will be emailed to recipients or available to download from our website as a PDF so they can fill it out on their office computer, print with the data filled, and then fax or snail mail to us. (No online or email submissions.)  We do not want them to accidentally save confidential information on the form that will be visible to the next user when they close it, or to Save As and keep the filled form.
    I know there are still ways to do it if someone really wants to, like printing it to PDF or using a PDF software other than Reader, but these are not folks who would have time or intention to do such a thing.  99% of them will be using some version of Reader, probably a much older one than mine.  Thank you for any advice!

    Thank you Gilad and George for trying. I just discovered both of you have replied to this issue here - http://forums.adobe.com/message/5881211#5881211 - which is my exact same situation; I am also a regional health care agency trying to provide forms for health care providers! (It's a small world.) I just didn't find this thread the first time I searched for answers.  I will investigate the JavaScript reset Form statement. Thanks!

  • Daily backup file with the date in the filename

    Hello,
    I like to backup the same file every day of the week. And add the date in the copied filename (for example "meta09-O4-04.rtf") to automate and schedule I can use cronnix. But adding the date in the filename doesn't work for me.
    As you can see in the script the backup file must be copied in a directory (for example "vis")that is in the same directory as the main file.
    Can some one help me on this?
    backupFileToFolder("Macintosh HD:Users:gast:Documents:meta.rtf", "Macintosh HD:Users:gast:Documents:vis:")
    on backupFileToFolder(startFile, targetFolder)
    set startFile to startFile as alias
    set targetFolder to targetFolder as alias
    tell application "Finder"
    duplicate ("Macintosh HD:Users:gast:Documents:meta.rtf") to ("Macintosh HD:Users:gast:Documents:vis:")
    end tell
    end backupFileToFolder

    But adding the date in the filename doesn't work for me.
    Might help if you showed how you were trying to add the date.
    That said, it shouldn't be hard to do. There are several ways of getting the date in the format you want, either via AppleScript or a simple shell command:
    AppleScript:
    set curDate to (get current date)
    set dateStr to getYear(curDate) & "-" & getDay(curDate) & "-" & getMonth(curDate)
    on getMonth(theDate)
    set cM to month of theDate as integer
    return text -2 through -1 of ("0" & cM)
    end getMonth
    on getDay(theDate)
    set cD to day of theDate as text
    return text -2 through -1 of ("0" & cD)
    end getDay
    on getYear(theDate)
    set cY to year of theDate as text
    return text -2 through -1 of cY
    end getYear
    Note this is a little more verbose than it needs to be, but it has the flexibility of formatting each date element (year, month, day) as you like.
    If you want a shell version:
    set dateStr to do shell script "date +%y-%d-%m"
    Now you have the dateStr with the date you want, so it's easy to rename the file.
    Just replace your duplicate command with the following:
    set backupFile to duplicate startFile to folder targetFolder
    set n to name of backupFile -- get the current file name
    set nE to name extension of backupFile -- get its filename extension
    set baseName to text 1 through (-1 + (length of nE)) of n -- work out the name without the extension
    set newName to baseName & dateStr & "." & nE -- work out the new name using the dateStr
    set name of backupFile to newName -- and rename the file

Maybe you are looking for