In my recent project I needed to build a pdf and email it to a user. Android has a great library for this and I found it only took a few hours to get a reasonably readable pdf onto my screen.
The basic outline goes as follows
//create the pdf <pre>PdfDocument reportDocument = new PdfDocument(); //create the page</pre> <pre>PdfDocument.Page page = reportDocument.startPage(pageInfo);</pre> <pre> /* * Do some stuff here. see further down */ </pre> <pre>//end the page reportDocument.finishPage(page);</pre> <pre>//send it away to a file for storage or in my case an email for sending</pre> <pre> pdfPath = getPDFFile(openProject.getProjectNme()); FileOutputStream fos = new FileOutputStream(pdfPath); reportDocument.writeTo(fos); reportDocument.close(); success = true;</pre>
Now for writing stuff on the pdf itself
//I had made a chart earlier and wanted it displayed <pre>page.getCanvas().drawBitmap(charts[0], 5, graphy, paint) //drawing some text on the page by setting up a paint object</pre> <pre> Paint paint = new Paint(); paint.setColor(Color.BLACK); paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(25); //then writing</pre> <pre>page.getCanvas().drawText("Source of Sound",width/2, titley, paint);</pre>
Really this is a super hackey way of doing it. There must be a cleaner API or if i ever need to do this again I will write one. But it works so I guess Ill come back to that at a later time.
Complete code here.
<pre>private void makePDF() { Log.i(TAG, "makePDF: started"); String sHighestDec; String sAverageHighestDecibel; //region String weighting = "For the purpose of soundproofing a nuisance noise must be 10 decibel lower than the ambient decibels of "; String weighting2 = "the rooms future use. So an office/library of 40 decibels would need its soundproofing to bring exterior"; String weighting3 = "nuisance noise to 30 decibels to not be noticed by end users. Frequently due to uncontrollable ambient "; String weighting4 = "factors it will be impossible to measure the new low of your target sound if it drop below 40 db"; String outerLimits = "Remember this is app is a crude measurement and educational tool."; String outerlimits2 = "Your device may not produce or record some sounds due to hardware limitations"; String outerlimits3 = "Secondly, you may not be able to hear all range of sounds. Wear ear protection!"; //endregion int width = 1224; int height = 1584; int left = 5; float titley = ((float)(height * .04)); float titleOney = ((float)(height * .06)); float titleTwoy = ((float)(height * .08)); float graphy = ((float)(height * .1)); float lineOney = ((float)(height * .55)); float lineTwoy = ((float)(height * .57)); float lineThreey = ((float)(height * .59)); float lineFoury = ((float)(height * .61)); float lineFivey = ((float)(height * .63)); float lineSixy = ((float)(height * .65)); float lineSeveny = ((float)(height * .67)); float lineEighty = ((float)(height * .69)); float lineNiney = ((float)(height * .71)); PdfDocument reportDocument = new PdfDocument(); //page 1 PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(width,height,1).create(); PdfDocument.Page page = reportDocument.startPage(pageInfo); Paint paint = new Paint(); paint.setColor(Color.BLACK); //title of page Log.i(TAG, "makePDF: page 1"); paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(25); //title String info1 = openProject.getProjectNme() + " " + openProject.getProjectLocation(); page.getCanvas().drawText("Source of Sound",width/2, titley, paint); page.getCanvas().drawText(info1,width/2, titleOney , paint); info1 = openProject.getDate(); page.getCanvas().drawText(info1,width/2, titleTwoy, paint); //chart page.getCanvas().drawBitmap(charts[0], 5, graphy, paint); paint.setTextAlign(Paint.Align.LEFT); //highest decibel and at what frequency recordedHighestAverage sHighestDec = "The highest decibel recorded was : " + String.valueOf(valuesChartOne[0]) + "db at approximately " + String.valueOf(valuesChartOne[1] + " hz"); page.getCanvas().drawText(sHighestDec, left, lineOney, paint); //average decibel recorded during total running average sAverageHighestDecibel = "The average decibel recorded was : " + String.valueOf(valuesChartOne[2]); page.getCanvas().drawText(sAverageHighestDecibel, left, lineTwoy, paint); //outerlimits page.getCanvas().drawText(outerLimits, left, lineThreey, paint); page.getCanvas().drawText(outerlimits2, left, lineFoury, paint); page.getCanvas().drawText(outerlimits3, left, lineFivey, paint); reportDocument.finishPage(page); //page 2 Log.i(TAG, "makePDF: page 2"); if (charts[1] != null) { pageInfo = new PdfDocument.PageInfo.Builder(width, height, 2).create(); page = reportDocument.startPage(pageInfo); //title of page paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(25); page.getCanvas().drawText("Receiving Area", width / 2, titley, paint); //chart of recording page.getCanvas().drawBitmap(charts[1], 5, graphy, null); paint.setTextAlign(Paint.Align.LEFT); //highest decibel and at what frequency sHighestDec = "The highest decibel recorded was : " + String.valueOf(valuesChartTwo[0]) + "db at approximately " + String.valueOf(valuesChartTwo[1] + " hz"); page.getCanvas().drawText(sHighestDec, left, lineOney, paint); //average decibel and at what frequency sAverageHighestDecibel = "The average decibel recorded was : " + String.valueOf(valuesChartTwo[2]); page.getCanvas().drawText(sAverageHighestDecibel, left, lineTwoy, paint); generateSTC(); //if barrier what stl or not calculatable if (stcSuccess){ String values = "Db lost : "; for (double d : diff) { //Log.i(TAG, "makePDF: " + String.valueOf(d)); int t = (int) Math.round(d); values += String.valueOf(t) + " "; } page.getCanvas().drawText(values , left, lineThreey, paint); page.getCanvas().drawText("Stc rating of this barrier is " + String.valueOf(stcRating), left, lineFoury, paint); }else{ page.getCanvas().drawText("Stc rating was not successfully calculated", left, lineThreey, paint); } // weighting a sound page.getCanvas().drawText(weighting, left, lineFivey, paint); page.getCanvas().drawText(weighting2, left, lineSixy, paint); page.getCanvas().drawText(weighting3, left, lineSeveny, paint); page.getCanvas().drawText(weighting4, left, lineEighty, paint); reportDocument.finishPage(page); } //page 3 if (charts[2] != null) { Log.i(TAG, "makePDF: page 3"); pageInfo = new PdfDocument.PageInfo.Builder(width, height, 3).create(); page = reportDocument.startPage(pageInfo); Material m = chosenMaterial.get(0); //title of page paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(25); page.getCanvas().drawText("Simulation", width / 2, titley, paint); info1 = m.getMaterialName() + " - " + m.getMaterialDescription(); if (info1.length() < 100) { page.getCanvas().drawText(info1, width / 2, titleOney, paint); } else { String one = info1.substring(0, info1.length()/2); String two = info1.substring(info1.length()/2 +1, info1.length()); page.getCanvas().drawText(one, width / 2, titleOney, paint); page.getCanvas().drawText(two, width / 2, titleTwoy, paint); } //chart of recording page.getCanvas().drawBitmap(charts[2], left, graphy, null); paint.setTextAlign(Paint.Align.LEFT); //highest decibel and at what frequency sHighestDec = "The highest decibel recorded was : " + String.valueOf(valuesChartThree[0]) + " at approximately " + String.valueOf(valuesChartThree[1] + " hz"); page.getCanvas().drawText(sHighestDec, left, lineOney, paint); //average decibel not being calculated //describe green and red marks info1 = "The green bars are "A" weighted to human perception based on frequency."; page.getCanvas().drawText(info1,left,lineFoury , paint); info1 = "The red dots are NC25 rating. (Allowable sound penetration for many use types)"; page.getCanvas().drawText(info1,left,lineFivey , paint); reportDocument.finishPage(page); } boolean success = false; try { pdfPath = getPDFFile(openProject.getProjectNme()); FileOutputStream fos = new FileOutputStream(pdfPath); reportDocument.writeTo(fos); reportDocument.close(); success = true; }catch (IOException f) { Log.e(TAG, "makePDF: ", f); }finally { if (success) { notifyObserver(1); }else { notifyObserver(2); } } }</pre>
The post Building a pdf programmatically on Android appeared first on SignalHillTechnology.
Powered by WPeMatico