Regarding Button additon problem in java

Regarding Button additon problem in java

Forum member · Aug 25, 2008
When i generate a pdf with some labels and pass to the browser through stream ,then that pdf is generated in adir and the i displayed in the browser properly. When i add 2 buttons say print and add into the same document and try to pass as stream to the browser , then it shows the pdf but with a pop up error -- An IO error occured. The browser shows a blank page. Can I get a solution for this? the code for generator clas ::::: private void setTemplate() { GregorianCalendar gc = new GregorianCalendar(); Date date = gc.getTime(); SimpleDateFormat sd1 = new SimpleDateFormat("dd MMM yyyy, H:mm:ss E"); Button button = null; Button button1 = null; JavaScriptAction action = null; UrlAction urlAction = new UrlAction("www.google.com"); // Create Button button = new Button( " PRINT ", 530, 10, 80 ,20 ); button.setAction(new JavaScriptAction( "app.alert('Hello');" )); button.setBackgroundColor( RgbColor.getDarkGray()); button.setBorderColor( RgbColor.getDarkGray() ); button.setBorderStyle( BorderStyle.SOLID ); button.setLabel( "PRINT" ); button.setTextColor( RgbColor.getWhite() ); button.setToolTip( "Print Entitlement Report" ); // Add the Button to the page template.getElements().add( button ); // Create Button // action = new JavaScriptAction( "SetupK(\""+ "\");PlaceCalendar(this.getField(\""+ "\"), \""+"\");" ); button1 = new Button( " BACK ", 620 , 10 , 80, 20 ); button1.setAction( urlAction ); button1.setBackgroundColor( RgbColor.getDarkGray()); button1.setBorderColor( RgbColor.getDarkGray() ); button1.setBorderStyle( BorderStyle.SOLID ); button1.setLabel( "BACK" ); button1.setTextColor( RgbColor.getWhite() ); button1.setToolTip( "Back" ); // Add the Button to the page template.getElements().add( button1 ); // Adds elements to the header template template.getElements().add(new Label("Entitlement Report",100,10, 500, 18,Font.getHelveticaBold(), 18, TextAlign.CENTER)); template.getElements().add(new Label("Users List", 100, 30, 500, 30,Font.getHelvetica(), 12, TextAlign.CENTER)); //template.getElements().add(new Label(sd1.format(date), 0, 0, 504, 12,Font.getHelveticaBold(), 12)); //template.getElements().add(new Label("Finiti User List", 0, 0,504, 12, Font.getHelveticaBold(), 12,TextAlign.CENTER)); // Adds footer elements to the template PageNumberingLabel pageNumLabel = new PageNumberingLabel("Page " + "%%CP%% of %%TP%%", 0, bodyBottom + 5, 720, 10, Font.getHelvetica(), 10, TextAlign.CENTER); template.getElements().add(pageNumLabel); Rectangle rect = new Rectangle(0, 50, 700, 16,RgbColor.getDarkGray(),RgbColor.getDarkGray()); //rect.setFillColor(new WebColor("0000A0")); template.getElements().add(rect); template.getElements().add(new Label("First Name",10 , 50, 156, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); template.getElements().add(new Label("Last Name",100 , 50, 86, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); template.getElements().add(new Label("Login ID",200 , 50, 156, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); template.getElements().add(new Label("Last Logged In",300 , 50, 156, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); template.getElements().add(new Label("Active",430 , 50, 156, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); template.getElements().add(new Label("Role ID",500 , 50, 156, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); template.getElements().add(new Label("Manager Name",580 , 50, 156, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); // template.getElements().add(new Line(0, 60, 504, 36)); } thee code for showing pdf in browser =----- private void openFileContent(HttpServletResponse res, ActionRequest req, String fileName, String baseDir, String fileExtn) throws Exception { logger.debug("BEGIN :: openFileContent() in ClientWebEntitlementReportAction"); System.out.println("BEGIN :: openFileContent() in ClientWebEntitlementReportAction"); BufferedInputStream bis = null; BufferedOutputStream bos = null; OutputStream out = null; File file1 = new File(baseDir, fileName); if ((!file1.exists())) { logger.info(":: In if Loop File Not Found ::"); // req.setAttribute(Constants.ERROR_MSG, Constants.ERROR_MSG); // SessionErrors.add(req, NoSuchNewsFoundException.class.getName()); // SessionErrors.add(req, EntitlementReportNotFoundException.class.getName()); return; } else { try { if (fileExtn.equalsIgnoreCase(FILE_EXTN_PDF)) { res.setHeader("Content-Disposition", "inline; filename=\"" + fileName); } else { res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName); } res.setHeader("Expires", "0"); res.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); res.setHeader("Pragma", "no-cache"); res.setBufferSize(8192); bis = new BufferedInputStream(new FileInputStream(file1)); byte[] bytes = new byte[8192]; int bytesRead = -1; out = res.getOutputStream(); bos = new BufferedOutputStream(out); while (-1 != (bytesRead = bis.read(bytes, 0, bytes.length))) { bos.write(bytes, 0, bytesRead); } } catch (Throwable e) { e.printStackTrace(); logger.fatal("Exception in openFileContent of ClientWebEntitlementReportAction", e); } finally { try { bos.close(); bis.close(); } catch (Exception e) { logger.fatal("Exception in openFileContent of ClientWebEntitlementReportAction while closing the resources", e); } } } logger.info("END :: openFileContent() in ClientWebEntitlementReportAction"); }

Re: Regarding Button additon problem in java

Support · Aug 25, 2008
Hello, Can you please try viewing the PDF file using the DrawToWeb method instead of setting the headers? DrawToWeb method sets all the headers internally. If you still get the errors please send over the error messages to our [^a|mailto:support@cete.com|Support Team^] so that they can look into this issue further. Also are you trying to add the buttons to existing PDF document or to new PDF documents? Thanks, ceTe Software Support Team

Re: Regarding Button additon problem in java

Forum member · Aug 25, 2008
the pdf document is created in a directory , then i read from the directory. The Pdf file is not deleted from the directory. Is that a problem . Is it necessary to delete the file? I have a method generatePdf called from action class is it required to pass responce and request objects also? which draw to web method to use. protected void generateReport(HttpServletResponse httpResp , ActionRequest req ,ActionResponse res) throws Exception { logger.info("BEGIN :: generateReport() in ClientWebEntitlementReportAction"); long organizationId = ParamUtil.getLong(req, "branchName"); // generate Pdf and store it in a dir. EntitlementReportGenerator generator = new EntitlementReportGenerator(); generator.generatePdf(organizationId); // read pdf file from the dir. boolean bAllowedFileExtensions = false; String fileExtn = null; //HttpServletRequest httpReq = null; httpResp = PortalUtil.getHttpServletResponse(res); //httpReq = PortalUtil.getHttpServletRequest(req); String fileName = com.finiti.website.clientweb.portal.util.Constants.ENTITLEMENT_REPORT_FILE; String entitlementReportDir = PropsUtil.get(PropsExtUtil.ENTITLEMENT_REPORT_DIR_PATH); fileExtn = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length()); if (fileExtn != null){ if (fileExtn.equalsIgnoreCase(FILE_EXTN_PDF)){ httpResp.setContentType(com.finiti.website.clientweb.portal.util.Constants.CONTENT_PDF); bAllowedFileExtensions = true; } } else { httpResp.setContentType(Constants.CONTENT_UNKNOWN); bAllowedFileExtensions = false; logger.debug("no file found : file not found exception"); } if (bAllowedFileExtensions){ logger.debug("in else loop : before calling openFileContent"); openFileContent(httpResp, req, fileName, entitlementReportDir, fileExtn); } logger.info("END :: generateReport() in ClientWebEntitlementReportAction"); } private void openFileContent(HttpServletResponse res, ActionRequest req, String fileName, String baseDir, String fileExtn) throws Exception { logger.debug("BEGIN :: openFileContent() in ClientWebEntitlementReportAction"); System.out.println("BEGIN :: openFileContent() in ClientWebEntitlementReportAction"); BufferedInputStream bis = null; BufferedOutputStream bos = null; OutputStream out = null; // ServletOutputStream out = null; File file1 = new File(baseDir, fileName); if ((!file1.exists())) { logger.info(":: In if Loop File Not Found ::"); return; } else { try { if (fileExtn.equalsIgnoreCase(FILE_EXTN_PDF)) { res.setHeader("Content-Disposition", "inline; filename=\"" + fileName); } else { res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName); } res.setHeader("Expires", "0"); res.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); res.setHeader("Pragma", "no-cache"); res.setBufferSize(8192); res.setContentLength((int)file1.length()); bis = new BufferedInputStream(new FileInputStream(file1)); byte[] bytes = new byte[8192]; int bytesRead = -1; out = res.getOutputStream(); bos = new BufferedOutputStream(out); while (-1 != (bytesRead = bis.read(bytes, 0, bytes.length))) { bos.write(bytes, 0, bytesRead); } } catch (Throwable e) { e.printStackTrace(); logger.fatal("Exception in openFileContent of ClientWebEntitlementReportAction", e); } finally { try { bos.close(); bis.close(); } catch (Exception e) { logger.fatal("Exception in openFileContent of ClientWebEntitlementReportAction while closing the resources", e); } } } logger.info("END :: openFileContent() in ClientWebEntitlementReportAction"); }

Re: Regarding Button additon problem in java

Forum member · Aug 25, 2008
I tried using the following :: objDocument.drawToWeb(httpReq, httpResp, out); I got the following error message :: with a pop up There was an error processing a page. A file I/O error has occured. Any clue to this....

Re: Regarding Button additon problem in java

Support · Aug 25, 2008
Hello, It is not required to delete the PDF document. Maybe the PDF document is currenly being used and therefore you do not have permissions to render it or use it. What is the exact exception you are seeing? Can you please send us the stack trace of the exception? Refer to the HelloWorld.java servlet example included with the download. Thanks, ceTe Software Support Team

Re: Regarding Button additon problem in java

Forum member · Aug 25, 2008
Ther is no stack trace only i get the pop up error as shown in my previous mail. I tried adding some other fields like textfield etc to a page. but it shows the same error.

Re: Regarding Button additon problem in java

Support · Aug 25, 2008
Hello, Can you please explain in detail what exactly you are trying to accomplish? Also send over your full source code to our [^a|mailto:support@cete.com|Support Team^] so that they can look into this issue further. Thanks, ceTe Software Support Team

Re: Regarding Button additon problem in java

Forum member · Aug 25, 2008
One problem i get is that in the pdf report all the records are displayed continuously , i.e a single page by page is not getting shown. I think that may be a problem . As the error shows "problem displaying a page , IO error". I have given a code that i have used for this. public class SimpleReport { // Template for document elements private static Template template = new Template(); // Page Dimensions of pages private static PageDimensions pageDimensions = new PageDimensions(PageSize.LETTER, PageOrientation.LANDSCAPE, 54.0f); // Current page that elements are being added to private Page currentPage; // Top Y coordinate for the body of the report private float bodyTop = 38; // Bottom Y coordinate for the body of the report private float bodyBottom = pageDimensions.body.getBottom() - pageDimensions.body.getTop(); // Current Y coordinate where elements are being added private float currentY; // Used to control the alternating background private boolean alternateBG; Connection connection; // main method public static void main(String args[]) { // Create a document and set it's properties Document objDocument = new Document(); objDocument.setCreator("SimpleReport.java"); objDocument.setAuthor("Your Name"); objDocument.setTitle("Simple Report"); SimpleReport simpleReport = new SimpleReport(); // Adds elements to the header template simpleReport.setTemplate(); objDocument.setTemplate(template); // simpleReport.setFooterTemplate(); ResultSet data = simpleReport.getContactListData(); // Builds the report simpleReport.buildDocument(objDocument, data); // Outputs the SimpleReport to the file. objDocument.draw("C:\\FIN0825\\pdf\\SimpleReport.pdf"); } private void setTemplate() { GregorianCalendar gc = new GregorianCalendar(); Date date = gc.getTime(); SimpleDateFormat sd1 = new SimpleDateFormat("dd MMM yyyy, H:mm:ss E"); // Adds elements to the header template template.getElements().add(new Label("Entitlement Report",100,10, 500, 18,Font.getHelveticaBold(), 18, TextAlign.CENTER)); template.getElements().add(new Label("Users List", 100, 30, 500, 30,Font.getHelvetica(), 12, TextAlign.CENTER)); //template.getElements().add(new Label(sd1.format(date), 0, 0, 504, 12,Font.getHelveticaBold(), 12)); //template.getElements().add(new Label("Finiti User List", 0, 0,504, 12, Font.getHelveticaBold(), 12,TextAlign.CENTER)); // Adds footer elements to the template PageNumberingLabel pageNumLabel = new PageNumberingLabel("Page " + "%%CP%% of %%TP%%", 0, bodyBottom + 5, 720, 10, Font.getHelvetica(), 10, TextAlign.CENTER); template.getElements().add(pageNumLabel); Rectangle rect = new Rectangle(0, 50, 700, 16,RgbColor.getDarkGray(),RgbColor.getDarkGray()); //rect.setFillColor(new WebColor("0000A0")); template.getElements().add(rect); template.getElements().add(new Label("First Name",10 , 50, 156, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); template.getElements().add(new Label("Last Name",100 , 50, 86, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); template.getElements().add(new Label("Screen Name",200 , 50, 156, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); template.getElements().add(new Label("Last Logged In",300 , 50, 156, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); template.getElements().add(new Label("Active",430 , 50, 156, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); template.getElements().add(new Label("Role ID",500 , 50, 156, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); template.getElements().add(new Label("Manager Name",580 , 50, 156, 12,Font.getHelveticaBold(), 12, TextAlign.LEFT,Grayscale.getWhite())); } private void setFooterTemplate() { System.out.println (":: Inside setFooterTemplate Starts::"); // Adds footer elements to the template PageNumberingLabel pageNumLabel = new PageNumberingLabel("Page " + "%%CP(i)%% of %%TP(i)%%", 0, bodyBottom + 5, 720, 10, Font.getHelvetica(), 10, TextAlign.CENTER); template.getElements().add(pageNumLabel); } private void buildDocument(Document document, ResultSet data) { // Builds the PDF document with data from the ResultSet addNewPage(document); try { while (data.next()) { // Add current record to the document addRecord(document, data); } } catch (SQLException ex) { } } private void addRecord(Document document, ResultSet data) { / Adds a new page to the document if needed if (currentY > bodyBottom) { addNewPage(document); } // Adds alternating background to document if needed if (alternateBG) { currentPage.getElements().add(new Rectangle(0, currentY + 30, 700 , 18,new WebColor("E0E0FF"), new WebColor("E0E0FF"))); } try { // Adds Labels to the document with data from current record currentPage.getElements().add(new Label(data.getString(1),10 ,currentY + 30, 156, 11,Font.getTimesRoman(), 11)); currentPage.getElements().add(new Label(data.getString(2),100 ,currentY + 30, 156, 11,Font.getTimesRoman(), 11)); } catch (SQLException ex) { System.err.println(":: Cannot retrieve data from ResultSet ::" + ex.getMessage()); } // Toggles alternating background alternateBG = !alternateBG; // Increments the current Y position on the page currentY += 18; } private void addNewPage(Document document) { System.out.println (":: Add New Page ::"); // Adds a new page to the document currentPage = new Page(pageDimensions); currentY = bodyTop; alternateBG = false; document.getPages().add(currentPage); } private ResultSet getContactListData() { ResultSet data = null; try { // Creates a ResultSet for the report PreparedStatement ps = connection.prepareStatement("select name,organizationid,parentOrganizationId,companyId,countryId from organization_"); data = ps.executeQuery(); } catch (SQLException ex) { System.err.println("cannot execute the query :"+ex.getMessage()); } return data; } }

Re: Regarding Button additon problem in java

Support · Aug 26, 2008
Hello, What build of our product are you using? Please make sure that you are using the latest build of our product. You can download the latest build from our CustomerArea by logging in using your current serial number. If you still have a problem then please send over a small sample application which can recreate the error to our [^a|mailto:support@cete.com|Support Team^] so that they can look into it further. Thanks, ceTe Software Support Team.