取得iReport / JasperReports報表內特定元件的方法 [修訂]
最近因為專案必須將國際條碼輸出到低解析度的顯示設備,因為解析度不足而吃足苦頭,當輸出到螢幕或印表機時,條碼都能正確呈現,但產生成1-Bit Bitmap檔時,條碼的直線卻總是會掉線,也就是粗線變細線,有的細線就不見了,導致掃描器經常都無法掃描成功。
後來只好用程式去讀iReport(底層是JasperReports)報表內的條碼元件,再變更其屬性,希望能解決此狀況。本文是由報表樣板檔取出特定元件的寫法。
JaserperFile jasperFile = new File("c:/report/test.jasper"); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperFile);
方法1:用getElementByKey
程式重點在如何讀取報表上的某一個元件,先用jasperReport.getXXX取得需要的JRBand物件:
method | 功用 |
---|---|
jasperReport.getTitle() | 取得Title Band |
jasperReport.getBackground() | 取得Background Band |
jasperReport.getColumnHeader() | 取得Column Header Band |
jasperReport.getColumnFooter() | 取得Column Footer Band |
jasperReport.getDetailSection()().getBands() | 取得Detail Band的陣列 |
jasperReport.getLastPageFooter() | 取得Last Page Footer Band |
jasperReport.getNoData() | 取得No Data Band |
jasperReport.getPageHeader() | 取得Page Header Band |
jasperReport.getPageFooter() | 取得Page Footer Band |
jasperReport.getSummary() | 取得Summary Band |
取到Band後再用getElementByKey("barcode")取得條碼元素,由調色盤拖入的條碼元件必須指定其key屬性為barcode(請自行命名)。
JRBand band = jasperReport.getDetailSection().getBands()[0]; JRElement element = (JRElement) band.getElementByKey("barcode"); System.out.println("(x,y)=" + _oBarcode.getX() + "," + _oBarcode.getY()); System.out.println("Width=" + _oBarcode.getWidth()); System.out.println("Height=" + _oBarcode.getHeight());
方法2:巡行getElements
另一個方法是巡行Band上的各個元件以找到特定的條碼元件後再做特別處理,本處使用的是Barbecue,iReport另外還可以使用Barcode4J:
int _iLeft = 0, _iTop = 50; JRElement[] _aElements = _oBand.getElements(); JRElement _oBarcodeElement = null; for (int i = 0; i < _aElements.length; i++) { JRElement _oElement = _aElements[i]; if (_oElement instanceof JRBaseComponentElement) { JRBaseComponentElement _oCompElement = (JRBaseComponentElement) _oElement; net.sf.jasperreports.engine.component.Component _oComponent = (net.sf.jasperreports.engine.component.Component) _oCompElement.getComponent(); if (_oComponent instanceof StandardBarbecueComponent) { _oBarcodeElement = (JRElement) _oElement; StandardBarbecueComponent _oBarcodeComponent = (StandardBarbecueComponent) _oComponent; _sBarcodeType = _oBarcodeComponent.getType(); _iLeft = _oBarcodeElement.getX(); _iTop = _oBarcodeElement.getY(); System.out.println("Width=" + _oBarcodeElement.getWidth()); System.out.println("(x,y)=" + _oBarcodeElement.getX() + "," + _oBarcodeElement.getY()); System.out.println("Height=" + _oBarcodeElement.getHeight()); break; } } }
##
您可能也會有興趣的類似文章
- [iReport] JasperReport 文件下載 (2則留言, 2005/02/27)
- [Java] JasperReports終於成為1.0版了 (0則留言, 2005/07/21)
- 試用iReport 0.5.2 (2則留言, 2005/11/15)
- 解決 iReport 升級後的問題 (1則留言, 2005/11/22)
- iReport 0.5更新與Groovy (2則留言, 2005/05/24)
- [iReport] 測試圖形功能(iReport+JFreeChart) (0則留言, 2005/04/28)
- [JasperReports] 0.6.7更新與JasperSoft提供商業服務 (0則留言, 2005/05/17)
- [iReport] 元件顏色與快速鍵 (修正) (0則留言, 2005/04/06)
- [iReport] Text Field中引用的欄位、變數與參數 (0則留言, 2005/04/16)
- iReport 0.4測試 (0則留言, 2005/02/12)
- iReport 3.7.1/4.0.2產生PDF報表的字體設定 (0則留言, 2014/08/05)
- 新版iReport PDF顯示中文的設定 (0則留言, 2011/05/14)
- [iReport] 奇偶列變換底色 (0則留言, 2005/11/29)
- [iReport] 列印名條標籤 (0則留言, 2005/06/13)
- [iReport] 兩個文字元件:Static Text與Text Field (0則留言, 2005/04/15)
上面的留言,空白被去除了,所以我用底線代替空白
AAA – BBBB <- 但是客戶要求斷行且縮排
______BBBB
用兩個text field, 在程式裡塞兩個字串如何?
有想過此方法,但因為AAA的長度不固定,所以會影響 ” – “的位置,也會改變BBBB的縮排寬度
程式求出減號的位置,第2列就補幾個空白,選用等寬的字型(如細明體),這樣應該可以吧?
你好,剛好最近我的工作都是利用iReport製作帳單版面,但是我遇到了一個縮排的問題,問題如下:
AAA – BBBBBBBB <- 這是我程式中所組成的資料內容,假設表格寬度為 8個字 ,所以我必須斷行
AAA – BBBB <- 這是目前iReport所能做到的斷行顯示
BBBB
AAA – BBBB <- 但是客戶要求斷行且縮排
BBBB
如果AAA的資料長度是固定的,還有辦法辦到縮排的效果,但是目前AAA長度是不固定的,可能會是0~N個
不曉得您有沒有好的建議,謝謝 :)