package com.qoppa; import java.util.Vector; import android.app.Activity; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.FontMetrics; import android.graphics.Rect; import android.os.Bundle; import android.util.Log; import com.qoppa.android.pdfProcess.PDFCanvas; import com.qoppa.android.pdfProcess.PDFDocument; import com.qoppa.android.pdfProcess.PDFFont; import com.qoppa.android.pdfProcess.PDFFontStandard; import com.qoppa.android.pdfProcess.PDFFontStandard.PDFFontFamily; import com.qoppa.android.pdfProcess.PDFPage; import com.qoppa.android.pdfProcess.PDFPaint; import com.qoppa.android.pdfViewer.fonts.StandardFontTF; public class TextWrapSample extends Activity { String text= "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; public void onCreate(Bundle bundle) { super.onCreate(bundle); StandardFontTF.mAssetMgr = getAssets(); try { int pageWidth = 800; PDFDocument doc = new PDFDocument(); PDFPage page = doc.appendNewPage(pageWidth, 1000); PDFCanvas canvas = page.createCanvas(); //draw a square on the canvas, to mark the text bounds Rect textBounds = new Rect(100, 50, 300, 400); canvas.drawRect(textBounds.left, textBounds.top, textBounds.right, textBounds.bottom, new PDFPaint()); PDFFontStandard font = new PDFFontStandard(PDFFontFamily.HELVETICA, PDFFont.Style.NORMAL, 24); Paint paint = font.getTextPaint(); Vector lines = new Vector(); int length = text.length(); for(int i = 0; i < length; i++) { float textWidth = paint.measureText(text.substring(0, i)); if(textWidth >= textBounds.width()) { //the string was too wide when measured from 0 to i, so substring should be 0 to i - 1 String line = text.substring(0, i - 1); lines.add(line); text = text.substring(i - 1); //reset the iterator length = text.length(); i = 0; } } //add the last line if(text.length() > 0) { lines.add(text); } FontMetrics fm = font.getFontMetrics(); float lineHeight = Math.abs(fm.ascent) + Math.abs(fm.descent); float y = lineHeight + textBounds.top; for(String line : lines) { canvas.drawText(line, Color.BLACK, textBounds.left, y, font); y += lineHeight; } doc.saveCopy("/sdcard/autowrapsample.pdf"); Log.e("sample finished", "autowrap sample finished!!!!!!!!!!!!!!!!!!!!"); } catch (Exception e) { Log.e("exception caught", Log.getStackTraceString(e)); } } }