如何在图像周围包装文字? [英] How to wrap text around an image?

查看:157
本文介绍了如何在图像周围包装文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文字包裹在图像周围。



我需要创建的结构如下图所示:





这就是我的尝试



我有尝试使用没有成功的表。我很难将文本字段用作html并将图像传递给它并将对齐设置为左边,但我发现无法在文本字段中传递图像。我开始使用 jaspersoft Studio pro 及其 html元素来解决问题。
但是我无法弄清楚如何将数据库中的图像字段作为html源传递。 html元素中的表达式为:

 < img src ='$ F {Image}'align ='left' />< p>+ $ F {KN_Zusatzinfo_DV_Einleitungstext} +< / p> 

此代码不显示图像,而是显示框而不是图像。
并且打印的文本没有拉伸。我使用的页面样式是两列。 HTML输出如图所示





使用像图像一样的静态路径可以工作,但它不是我的选择,因为我需要从数据库中读取图像:

 < style> .left {float:left; margin-left:1em; width:40%;}< / style> ;< p>< img class = left src = C:/Users/iman/JaspersoftWorkspace/Qreport/sample1.png /> 

如果我只使用带有文本框的图片元素,那么输出和设计视图就像这些图片一样:







HTML源代码的html方法是:

 <!DOCTYPE html PUBLIC -  / / W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
< html>

< head>
< title>< / title>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< style type =text / css>
a {
text-decoration:none
}
< / style>
< / head>

< body text =#000000link =#000000alink =#000000vlink =#000000>
< table width =100%cellpadding =0cellspacing =0border =0>
< tr>
< td width =50%>& nbsp;< / td>
< td align =center>

< a name =JR_PAGE_ANCHOR_0_1>< / a>
< table class =jrPagecellpadding =0cellspacing =0border =0style =empty-cells:show; width:595px; border-collapse:collapse; background-color:白;>
< tr valign =topstyle =height:0>
< td style =width:10px>< / td>
< td style =width:20px>< / td>
< td style =width:40px>< / td>
< td style =width:210px>< / td>
< td style =width:32px>< / td>
< td style =width:58px>< / td>
< td style =width:10px>< / td>
< td style =width:182px>< / td>
< td style =width:9px>< / td>
< td style =width:24px>< / td>
< / tr>
< tr valign =topstyle =height:25px>
< td colspan =10>
< / td>
< / tr>
< tr valign =topstyle =height:5px>
< td colspan =6>
< / td>
< td colspan =3rowspan =2style =text-indent:0px; text-align:right;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#6B66FA; font-size:10px; line-height:1.2578125;> HCH-A - Aortenklappenchirurgie,isoliert(Konventionell chirurgisch)< / span>
< / td>
< td>
< / td>
< / tr>
< tr valign =topstyle =height:20px>
< td>
< / td>
< td colspan =2style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#BABABA; font-size:10px; line-height:1.2578125;> Ergebnisse< /跨度>
< / td>
< td colspan =3>
< / td>
< td>
< / td>
< / tr>
< tr valign =topstyle =height:20px>
< td colspan =7>
< / td>
< td colspan =2style =text-indent:0px; text-align:right;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#000000; font-size:10px; line-height:1.2578125;> Dr XYZ< /跨度>
< / td>
< td>
< / td>
< / tr>
< tr valign =topstyle =height:88px>
< td colspan =10>
< / td>
< / tr>
< tr valign =topstyle =height:74px>
< td colspan =5>
< / td>
< td colspan =3rowspan =2>
< div style ='width:250px; height:90px; background-color:#FFFFFF;溢出:隐藏;>
< img src ='((java.awt.Image)field_Image.getValue())'align ='left'/>
< p> Bei der Aortenklappe handelt es sich um dasVentilzwischen der linken HerzkeAortenklappenchirurgie,isoliert - konventionell - Aortenklappenchirurgie,isoliert - kathetergestütztDieQualitätsindikatorenderk ........
appenchirauch Indikationsstellung betrachtet。 Sofern nicht anders angegeben,ist die BeschreibungderQualitätsindikatoreneineFortschreibung der QIDB 2014 des AQUA-Institutes。< / p>
< / div>
< / td>
< td colspan =2>
< / td>
< / tr>
< tr valign =topstyle =height:16px>
< td colspan =2>
< / td>
< td colspan =2rowspan =2>
< div style ='width:250px; height:90px; background-color:#FFFFFF;溢出:隐藏;>
< img src ='((java.awt.Image)field_Image.getValue())'align ='left'/>
< p> Bei der Aortenklappe handelt es sich um dasVentilzwischen der linken Herzkammer und der Hauptschlagader(Aorta)。 SchließtdieKlappe nicht mehr dicht,spriitätsindikatorender konventionellen Aortenklappenchirurgie fokussieren schwere Komplikationen und Sterblichkeit。
BeiderkathetergestütztenAortenklappenchirurgiewirddarüberhinausauch die Indikationsstellung betrachtet。 Sofern nicht anders angegeben,ist die BeschreibungderQualitätsindikatoreneineFortschreibung der QIDB 2014 des AQUA-Institutes。< / p>
< / div>
< / td>
< td>
< / td>
< td colspan =2>
< / td>
< / tr>
< tr valign =topstyle =height:74px>
< td colspan =2>
< / td>
< td colspan =6>
< / td>
< / tr>
< tr valign =topstyle =height:520px>
< td colspan =10>
< / td>
< / tr>
< / table>
< br />
< br />

< a name =JR_PAGE_ANCHOR_0_2>< / a>
< table class =jrPagecellpadding =0cellspacing =0border =0style =empty-cells:show; width:595px; border-collapse:collapse; background-color:白;>
< tr valign =topstyle =height:0>
< td style =width:10px>< / td>
...
< td style =width:24px>< / td>
< / tr>
< tr valign =topstyle =height:27px>
< td colspan =7>
< / td>
< / tr>
< tr valign =topstyle =height:20px>
< td>
< / td>
< td colspan =2style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#BABABA; font-size:10px; line-height:1.2578125;> Ergebnisse< /跨度>
< / td>
< td colspan =4>
< / td>
< / tr>
< tr valign =topstyle =height:7px>
< td colspan =7>
< / td>
< / tr>
< tr valign =topstyle =height:21px>
< td colspan =5>
< / td>
< td style =text-indent:0px; text-align:right;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#6B66FA; font-size:10px; line-height:1.2578125;> HCH-A - Aortenklappenchirurgie,isoliert< / span>
< / td>
< td>
< / td>
< / tr>
< tr valign =topstyle =height:125px>
< td colspan =7>
< / td>
< / tr>
< tr valign =topstyle =height:90px>
< td colspan =2>
< / td>
< td colspan =2>
< div style ='width:250px; height:90px; background-color:#FFFFFF;溢出:隐藏;>
< img src ='((java.awt.Image)field_Image.getValue())'align ='left'/>
< p> ....删除了一些文字,因为它超出了限制。< / p>
< / div>
< / td>
< td colspan =3>
< / td>
< / tr>
< tr valign =topstyle =height:552px>
< td colspan =7>
< / td>
< / tr>
< / table>
< br />
< br />

< a name =JR_PAGE_ANCHOR_0_3>< / a>
< table class =jrPagecellpadding =0cellspacing =0border =0style =empty-cells:show; width:595px; border-collapse:collapse; background-color:白;>
< tr valign =topstyle =height:0>
< td style =width:595px>< / td>
< / tr>
< tr valign =topstyle =height:842px>
< td>
< / td>
< / tr>
< / table>

<![if IE]>
< script>
var links = document.querySelectorAll('link.jrWebFont');
setTimeout(function(){
if(links){
for(var i = 0; i< links.length; i ++){
links.item(i) .href = links.item(i).href;
}
}
},0);
< / script>
<![endif]>
< / td>
< td width =50%>& nbsp;< / td>
< / tr>
< / table>
< / body>
< / html>

Image元素加上文本字段的HTML源代码为:

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp:// www .w3.org / TR / XHTML1 / DTD / XHTML1-transitional.dtd> 
< html>

< head>
< title>< / title>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< style type =text / css>
a {
text-decoration:none
}
< / style>
< / head>

< body text =#000000link =#000000alink =#000000vlink =#000000>
< table width =100%cellpadding =0cellspacing =0border =0>
< tr>
< td width =50%>& nbsp;< / td>
< td align =center>

< a name =JR_PAGE_ANCHOR_0_1>< / a>
< table class =jrPagecellpadding =0cellspacing =0border =0style =empty-cells:show; width:595px; border-collapse:collapse; background-color:白;>
< tr valign =topstyle =height:0>
< td style =width:10px>< / td>
< td style =width:60px>< / td>
< td style =width:232px>< / td>
< td style =width:33px>< / td>
< td style =width:35px>< / td>
< td style =width:10px>< / td>
< td style =width:5px>< / td>
< td style =width:186px>< / td>
< td style =width:1px>< / td>
< td style =width:23px>< / td>
< / tr>
< tr valign =topstyle =height:25px>
< td colspan =10>
< / td>
< / tr>
< tr valign =topstyle =height:5px>
< td colspan =5>
< / td>
< td colspan =3rowspan =2style =text-indent:0px; text-align:right;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#6B66FA; font-size:10px; line-height:1.2578125;> HCH-A - Aortenklappenchirurgie,isoliert(Konventionell chirurgisch)< / span>
< / td>
< td colspan =2>
< / td>
< / tr>
< tr valign =topstyle =height:20px>
< td>
< / td>
< td style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#BABABA; font-size:10px; line-height:1.2578125;> Ergebnisse< /跨度>
< / td>
< td colspan =3>
< / td>
< td colspan =2>
< / td>
< / tr>
< tr valign =topstyle =height:20px>
< td colspan =6>
< / td>
< td colspan =2style =text-indent:0px; text-align:right;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#000000; font-size:10px; line-height:1.2578125;> Dr XYZ< /跨度>
< / td>
< td colspan =2>
< / td>
< / tr>
< tr valign =topstyle =height:94px>
< td colspan =10>
< / td>
< / tr>
< tr valign =topstyle =height:50px>
< td colspan =4>
< / td>
< td colspan =3>
< img src =report7921484792758494686.html_files / img_0_0_3style =width:50pxalt =/>
< / td>
< td colspan =3>
< / td>
< / tr>
< tr valign =topstyle =height:24px>
< td colspan =10>
< / td>
< / tr>
< tr valign =topstyle =height:477px>
< td colspan =3>
< / td>
< td colspan =6style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#000000; font-size:10px; line-height:1.2578125;> Bei der Aortenklappe handelt es sinken Herzkammer und der Hauptschlagader(Aorta)。 EinsatzeinerkünstlichenHerzklappebehandelt。< br />< br /> BeibeidenkathetergestütztenMethodenwirdüberinspeziellesBallonkathetersystemzunächstderBereich der< / span>
< / td>
< td>
< / td>
< / tr>
< tr valign =topstyle =height:127px>
< td colspan =10>
< / td>
< / tr>
< / table>
< br />
< br />

< a name =JR_PAGE_ANCHOR_0_2>< / a>
< table class =jrPagecellpadding =0cellspacing =0border =0style =empty-cells:show; width:595px; border-collapse:collapse; background-color:白;>
< tr valign =topstyle =height:0>
< td style =width:10px>< / td>
...
< td style =width:24px>< / td>
< / tr>
< tr valign =topstyle =height:27px>
< td colspan =7>
< / td>
< / tr>
< tr valign =topstyle =height:20px>
< td>
< / td>
< td colspan =2style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#BABABA; font-size:10px; line-height:1.2578125;> Ergebnisse< /跨度>
< / td>
< td colspan =4>
< / td>
< / tr>
< tr valign =topstyle =height:7px>
< td colspan =7>
< / td>
< / tr>
< tr valign =topstyle =height:21px>
< td colspan =5>
< / td>
< td style =text-indent:0px; text-align:right;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#6B66FA; font-size:10px; line-height:1.2578125;> HCH-A - Aortenklappenchirurgie,isoliert< / span>
< / td>
< td>
< / td>
< / tr>
< tr valign =topstyle =height:139px>
< td colspan =7>
< / td>
< / tr>
< tr valign =topstyle =height:264px>
< td colspan =2>
< / td>
< td colspan =2style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#000000; font-size:10px; line-height:1.2578125;> alten,verengten Aortenklappe erweitert。 - des AQUA-Institutes。< / span>
< / td>
< td colspan =3>
< / td>
< / tr>
< tr valign =topstyle =height:364px>
< td colspan =7>
< / td>
< / tr>
< / table>
< br />
< br />

< a name =JR_PAGE_ANCHOR_0_3>< / a>
< table class =jrPagecellpadding =0cellspacing =0border =0style =empty-cells:show; width:595px; border-collapse:collapse; background-color:白;>
< tr valign =topstyle =height:0>
< td style =width:10px>< / td>
< td style =width:10px>< / td>
...
< td style =width:1px>< / td>
< td style =width:23px>< / td>
< / tr>
< tr valign =topstyle =height:27px>
< td colspan =11>
< / td>
< / tr>
< tr valign =topstyle =height:20px>
< td>
< / td>
< td colspan =3style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#BABABA; font-size:10px; line-height:1.2578125;> Ergebnisse< /跨度>
< / td>
< td colspan =7>
< / td>
< / tr>
< tr valign =topstyle =height:7px>
< td colspan =11>
< / td>
< / tr>
< tr valign =topstyle =height:21px>
< td colspan =8>
< / td>
< td style =text-indent:0px; text-align:right;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#6B66FA; font-size:10px; line-height:1.2578125;> HCH-A - Aortenklappenchirurgie,isoliert< / span>
< / td>
< td colspan =2>
< / td>
< / tr>
< tr valign =topstyle =height:65px>
< td colspan =11>
< / td>
< / tr>
< tr valign =topstyle =height:66px>
< td colspan =7>
< / td>
< td colspan =3rowspan =4style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#000000; font-size:10px; line-height:1.2578125;>< br /> Beibeidenkathetergestützten我死于Indikationsstellung betrachtet。< br />< br /> Sofern nicht anders angegeben,ist die BeschreibungderQualitätsindikatoreneineFortschreibung der QIDB 2014 des AQUA-Institutes。< / span>
< / td>
< td>
< / td>
< / tr>
< tr valign =topstyle =height:50px>
< td colspan =3>
< / td>
< td colspan =2>
< img src =report7921484792758494686.html_files / img_0_2_2style =width:50pxalt =/>
< / td>
< td colspan =2>
< / td>
< td>
< / td>
< / tr>
< tr valign =topstyle =height:24px>
< td colspan =7>
< / td>
< td>
< / td>
< / tr>
< tr valign =topstyle =height:161px>
< td colspan =2>
< / td>
< td colspan =4rowspan =2style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#000000; font-size:10px; line-height:1.2578125;> Bei der Aortenklapprzspitze übereinen3 bis 5 cm langen Hautschnitt im Rippenbereich(linker Brustkorb,im 4. oder 5. Rippenzwischenraum)freigelegt。 < br /> - BeimoreovaskulärenAortenklappenersatzist lediglich ein gezielter Einstich(Punktion),zumeist in die Leistenarterie,notwendig。 <峰; br />< /跨度>
< / td>
< td>
< / td>
< td>
< / td>
< / tr>
< tr valign =topstyle =height:279px>
< td colspan =2>
< / td>
< td colspan =5>
< / td>
< / tr>
< tr valign =topstyle =height:122px>
< td colspan =11>
< / td>
< / tr>
< / table>
< br />
< br />

< a name =JR_PAGE_ANCHOR_0_4>< / a>
< table class =jrPagecellpadding =0cellspacing =0border =0style =empty-cells:show; width:595px; border-collapse:collapse; background-color:白;>
< tr valign =topstyle =height:0>
< td style =width:10px>< / td>
< td style =width:60px>< / td>
...
< td style =width:1px>< / td>
< td style =width:23px>< / td>
< / tr>
< tr valign =topstyle =height:27px>
< td colspan =9>
< / td>
< / tr>
< tr valign =topstyle =height:20px>
< td>
< / td>
< td style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#BABABA; font-size:10px; line-height:1.2578125;> Ergebnisse< /跨度>
< / td>
< td colspan =7>
< / td>
< / tr>
< tr valign =topstyle =height:7px>
< td colspan =9>
< / td>
< / tr>
< tr valign =topstyle =height:21px>
< td colspan =5>
< / td>
< td colspan =2style =text-indent:0px; text-align:right;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#6B66FA; font-size:10px; line-height:1.2578125;> HCH-A - Aortenklappenchirurgie,isoliert< / span>
< / td>
< td colspan =2>
< / td>
< / tr>
< tr valign =topstyle =height:91px>
< td colspan =9>
< / td>
< / tr>
< tr valign =topstyle =height:50px>
< td colspan =4>
< / td>
< td colspan =2>
< img src =report7921484792758494686.html_files / img_0_3_2style =width:50pxalt =/>
< / td>
< td colspan =3>
< / td>
< / tr>
< tr valign =topstyle =height:24px>
< td colspan =9>
< / td>
< / tr>
< tr valign =topstyle =height:477px>
< td colspan =3>
< / td>
< td colspan =5style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif; r Brustkorb,im 4. oder 5. Rippenzwischenraum)freigelegt。< br /> - BeimoreovaskulärenAortenklappenersatz ist lediglich ein gezielter Einstich(Punktion),zumeist in die Leistenarterie,notwendig。< br />< br /> BeibeidenkathetergestütztenDisodenwirdüberinspeziellesBallonkathetersystemzunächstderBereich der< / span>
< / td>
< td>
< / td>
< / tr>
< tr valign =topstyle =height:125px >
< td colspan =9>
< / td>
< / tr>
< / table>
< br />
< br />

< a name =JR_PAGE_ANCHOR_0_5>< / a>
< table class =jrPagecellpadding = 0cellspacing =0border =0style =empty-cells:show;宽度:595px;边界崩溃:崩溃; background-color:white;>
< tr valign =topstyle =height:0>
< td style =width:10px>< / td> ;
< td style =width:10px>< / td>
< td style =width:50px>< / td>
< td style =width:220px>< / td>
< td style =width:80px>< / td>
< td style =width:201px> ;< / td>
< td style =width:24px>< / td>
< / tr>
< tr valign =topstyle = height:27px>
< td colspan =7>
< / td>
< / tr>
< tr valign =top style =height:20px>
< td>
< / td>
< td colspan =2style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#BABABA; font-size:10px; line-height:1.2578125;> Ergebnisse< / span>
< / td>
< td colspan =4>
< / td>
< / tr>
< tr valign =topstyle =height:7px>
< td colspan =7>
< / td>
< / tr>
< tr valign =topstyle =height:21px>
< td colspan =5>
< / td>
< td style =text-indent:0px; text-align:right;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#6B66FA; font-size:10px; line-height:1.2578125;> HCH-A - Aortenklappenchirurgie,isoliert< / span>
< / td>
< td>
< / td>
< / tr>
< tr valign =topstyle =height:139px>
< td colspan =7>
< / td>
< / tr>
< tr valign =topstyle =height:264px>
< td colspan =2>
< / td>
< td colspan =2style =text-indent:0px; text-align:left;>
< span style =font-family:'DejaVu Sans',Arial,Helvetica,sans-serif;颜色:#000000; font-size:10px; line-height:1.2578125;> alten,verengten Aortenklappeerweitert.Anschließendwirdeine zusammengefaltete Herzklappenchirurgie fokussieren schwere Komplikationen und Sterblichkeit.BeiderkathetergestütztenAortenklappenchirurgiewirddarüberinausauch die Indikationsstellung betrachtet。< br />< br /> Sofern nicht anders angegeben,ist die BeschreibungderQualitätsindikatorendesAQUA-Institutes。< / span>
< / td>
< td colspan =3>
< / td>
< / tr>
< tr valign =topstyle =height:364px>
< td colspan =7>
< / td>
< / tr>
< / table>
< br />
< br />

< a name =JR_PAGE_ANCHOR_0_6>< / a>
< table class =jrPagecellpadding =0cellspacing =0border =0style =empty-cells:节目;宽度:595px;边界崩溃:崩溃; background-color:white;>
< tr valign =topstyle =height:0>
< td style =width:595px>< / td> ;
< / tr>
< tr valign =topstyle =height:842px>
< td>
< / td>
< / tr>
< / table>

<![if IE]>
< script>
var links = document。 querySelectorAll('link.jrWebFont');
setTimeout(function(){
if(links){
for(var i = 0; i< links.length; i ++){
links.item(i).href = links.item(i).href;
}
}
},0);
< / script>
<![endif]>
< / td>
< td width =50%>& nbsp;< / td>
< / tr>
< / table>
< / body>

< / html>

何我可以实现这种结构吗?

解决方案

如果你使用html组件来包装图像周围的文字,你不仅有从图像中提取图像的问题吗?数据库,html组件也创建了一个html的图像,因此可以根据html(图像)组件的文本长度和设置调整内容的大小。



I建议在图像周围使用两个 textField s(参见jrxml),然后计算文本在下一个字段中的位置,这样就可以获得第一个文本的第一部分 textField ,第二部分在第二个 textField



Java代码到计算文本的断点



此代码演示了如何使用 FontMetric 来计算Jasper报告将取决于文本的位置关于 textField 的大小(代码的说明在注释中)。代码在 NullPointer null 文本)和非溢出文本上​​并非完全安全,此外它可以是优化,但我会把它留给OP

  import java.awt.Font; 
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;

公共类WrapImage {

/ **
*获取字符串将突破的位置
* @param文本,文本
* @param宽度,组件的宽度
* @param高度,组件的高度
* @return位置
* /
public static int getBreakPosition(String text, int width,int height){
//开始字体上下文
AffineTransform affinetransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affinetransform,true,true);

//Set same font as used in jasper-report
Font font = new Font(\"SansSerif\", Font.PLAIN, 10);
//Get height to understand how many lines
double textheight = font.getStringBounds(text, frc).getHeight();
int nrLines = (int)Math.floor(height/textheight);

//init variables
int breakPos = 0;
int line = 1;

//loop the lines
while (line<=nrLines){
//get remaining text
String textPart = text.substring(breakPos,text.length());
//get how much text will fit in line
breakPos += getLineBreakPosition(textPart, width, font, frc)+1;
line++;
}

return breakPos;

}

/**
* Get where a single line will break
* @param text, the text
* @param width, width of component
* @param font, the font used
* @param frc, the FontRenderContext
* @return
*/
protected static int getLineBreakPosition(String text, int width, Font font, FontRenderContext frc){
int breakPos = 0;
String tmpText = text;
while (font.getStringBounds(tmpText, frc).getWidth()>width){
//the break position is space
breakPos = tmpText.lastIndexOf(’ ’);
if (breakPos<=0){
breakPos = 0;
休息;
}
tmpText = tmpText.substring(0,breakPos);
}
return breakPos;
}
}



jrxml example using java code



In this short example, I use a parameter with some


I'm trying to wrap my text around an Image.

The structure I need to create is as in the picture bellow:

This is what I have tried

I have tried using table which did not succeed. I tough of using text field as html and passing image to it and set the alignment to left, but I found out that it is not possible to pass the image in text field. I started to solve the problem using jaspersoft Studio pro and with its html element. However I could not figure out how to pass the image field from data base as the html source. The expression in the html element is:

"<img src= '$F{Image}' align='left'/> <p>"+$F{KN_Zusatzinfo_DV_Einleitungstext} +"</p>"

This code does not show the image, but a box instead of the image. And the text which is printed is not stretched. The page style which I use is two column. The HTML out put is as in the picture

Using a static path like one bellow to an image works, but it is not an option for me since I need to read the Image from data base:

    "<style>.left { float: left;  margin-left: 1em; width :40%;}</style><p><img class = left src= C:/Users/iman/JaspersoftWorkspace/Qreport/sample1.png />

If I just use an Image Element with a text box under that the out put and design view are as these pictures:

The HTML source of the html approach is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <style type="text/css">
    a {
      text-decoration: none
    }
  </style>
</head>

<body text="#000000" link="#000000" alink="#000000" vlink="#000000">
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
      <td width="50%">&nbsp;</td>
      <td align="center">

        <a name="JR_PAGE_ANCHOR_0_1"></a>
        <table class="jrPage" cellpadding="0" cellspacing="0" border="0" style="empty-cells: show; width: 595px; border-collapse: collapse; background-color: white;">
          <tr valign="top" style="height:0">
            <td style="width:10px"></td>
            <td style="width:20px"></td>
            <td style="width:40px"></td>
            <td style="width:210px"></td>
            <td style="width:32px"></td>
            <td style="width:58px"></td>
            <td style="width:10px"></td>
            <td style="width:182px"></td>
            <td style="width:9px"></td>
            <td style="width:24px"></td>
          </tr>
          <tr valign="top" style="height:25px">
            <td colspan="10">
            </td>
          </tr>
          <tr valign="top" style="height:5px">
            <td colspan="6">
            </td>
            <td colspan="3" rowspan="2" style="text-indent: 0px; text-align: right;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #6B66FA; font-size: 10px; line-height: 1.2578125;">HCH-A - Aortenklappenchirurgie, isoliert (Konventionell chirurgisch)</span>
            </td>
            <td>
            </td>
          </tr>
          <tr valign="top" style="height:20px">
            <td>
            </td>
            <td colspan="2" style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #BABABA; font-size: 10px; line-height: 1.2578125;">Ergebnisse</span>
            </td>
            <td colspan="3">
            </td>
            <td>
            </td>
          </tr>
          <tr valign="top" style="height:20px">
            <td colspan="7">
            </td>
            <td colspan="2" style="text-indent: 0px; text-align: right;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #000000; font-size: 10px; line-height: 1.2578125;">Dr XYZ</span>
            </td>
            <td>
            </td>
          </tr>
          <tr valign="top" style="height:88px">
            <td colspan="10">
            </td>
          </tr>
          <tr valign="top" style="height:74px">
            <td colspan="5">
            </td>
            <td colspan="3" rowspan="2">
              <div style='width:250px;height:90px;background-color: #FFFFFF; overflow:hidden;'>
                <img src='((java.awt.Image)field_Image.getValue())' align='left' />
                <p>Bei der Aortenklappe handelt es sich um das „Ventil" zwischen der linken HerzkeAortenklappenchirurgie, isoliert - konventionell - Aortenklappenchirurgie, isoliert - kathetergestützt Die Qualitätsindikatoren der k........
                 appenchirauch die Indikationsstellung betrachtet. Sofern nicht anders angegeben, ist die Beschreibung der Qualitätsindikatoren eine Fortschreibung der QIDB 2014 des AQUA-Institutes.</p>
              </div>
            </td>
            <td colspan="2">
            </td>
          </tr>
          <tr valign="top" style="height:16px">
            <td colspan="2">
            </td>
            <td colspan="2" rowspan="2">
              <div style='width:250px;height:90px;background-color: #FFFFFF; overflow:hidden;'>
                <img src='((java.awt.Image)field_Image.getValue())' align='left' />
                <p>Bei der Aortenklappe handelt es sich um das „Ventil" zwischen der linken Herzkammer und der Hauptschlagader (Aorta). Schließt die Klappe nicht mehr dicht, spriitätsindikatoren der konventionellen Aortenklappenchirurgie fokussieren schwere Komplikationen und Sterblichkeit.
                  Bei der kathetergestützten Aortenklappenchirurgie wird darüber hinaus auch die Indikationsstellung betrachtet. Sofern nicht anders angegeben, ist die Beschreibung der Qualitätsindikatoren eine Fortschreibung der QIDB 2014 des AQUA-Institutes.</p>
              </div>
            </td>
            <td>
            </td>
            <td colspan="2">
            </td>
          </tr>
          <tr valign="top" style="height:74px">
            <td colspan="2">
            </td>
            <td colspan="6">
            </td>
          </tr>
          <tr valign="top" style="height:520px">
            <td colspan="10">
            </td>
          </tr>
        </table>
        <br/>
        <br/>

        <a name="JR_PAGE_ANCHOR_0_2"></a>
        <table class="jrPage" cellpadding="0" cellspacing="0" border="0" style="empty-cells: show; width: 595px; border-collapse: collapse; background-color: white;">
          <tr valign="top" style="height:0">
            <td style="width:10px"></td>
            ...
            <td style="width:24px"></td>
          </tr>
          <tr valign="top" style="height:27px">
            <td colspan="7">
            </td>
          </tr>
          <tr valign="top" style="height:20px">
            <td>
            </td>
            <td colspan="2" style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #BABABA; font-size: 10px; line-height: 1.2578125;">Ergebnisse</span>
            </td>
            <td colspan="4">
            </td>
          </tr>
          <tr valign="top" style="height:7px">
            <td colspan="7">
            </td>
          </tr>
          <tr valign="top" style="height:21px">
            <td colspan="5">
            </td>
            <td style="text-indent: 0px; text-align: right;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #6B66FA; font-size: 10px; line-height: 1.2578125;">HCH-A - Aortenklappenchirurgie, isoliert </span>
            </td>
            <td>
            </td>
          </tr>
          <tr valign="top" style="height:125px">
            <td colspan="7">
            </td>
          </tr>
          <tr valign="top" style="height:90px">
            <td colspan="2">
            </td>
            <td colspan="2">
              <div style='width:250px;height:90px;background-color: #FFFFFF; overflow:hidden;'>
                <img src='((java.awt.Image)field_Image.getValue())' align='left' />
                <p>....deleted some text because it exceeded the limit..</p>
              </div>
            </td>
            <td colspan="3">
            </td>
          </tr>
          <tr valign="top" style="height:552px">
            <td colspan="7">
            </td>
          </tr>
        </table>
        <br/>
        <br/>

        <a name="JR_PAGE_ANCHOR_0_3"></a>
        <table class="jrPage" cellpadding="0" cellspacing="0" border="0" style="empty-cells: show; width: 595px; border-collapse: collapse; background-color: white;">
          <tr valign="top" style="height:0">
            <td style="width:595px"></td>
          </tr>
          <tr valign="top" style="height:842px">
            <td>
            </td>
          </tr>
        </table>

        <![if IE]>
        <script>
          var links = document.querySelectorAll('link.jrWebFont');
          setTimeout(function() {
            if (links) {
              for (var i = 0; i < links.length; i++) {
                links.item(i).href = links.item(i).href;
              }
            }
          }, 0);
        </script>
        <![endif]>
      </td>
      <td width="50%">&nbsp;</td>
    </tr>
  </table>
</body>
</html>

And the HTML source of the Image element plus text field is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <style type="text/css">
    a {
      text-decoration: none
    }
  </style>
</head>

<body text="#000000" link="#000000" alink="#000000" vlink="#000000">
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
      <td width="50%">&nbsp;</td>
      <td align="center">

        <a name="JR_PAGE_ANCHOR_0_1"></a>
        <table class="jrPage" cellpadding="0" cellspacing="0" border="0" style="empty-cells: show; width: 595px; border-collapse: collapse; background-color: white;">
          <tr valign="top" style="height:0">
            <td style="width:10px"></td>
            <td style="width:60px"></td>
            <td style="width:232px"></td>
            <td style="width:33px"></td>
            <td style="width:35px"></td>
            <td style="width:10px"></td>
            <td style="width:5px"></td>
            <td style="width:186px"></td>
            <td style="width:1px"></td>
            <td style="width:23px"></td>
          </tr>
          <tr valign="top" style="height:25px">
            <td colspan="10">
            </td>
          </tr>
          <tr valign="top" style="height:5px">
            <td colspan="5">
            </td>
            <td colspan="3" rowspan="2" style="text-indent: 0px; text-align: right;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #6B66FA; font-size: 10px; line-height: 1.2578125;">HCH-A - Aortenklappenchirurgie, isoliert (Konventionell chirurgisch)</span>
            </td>
            <td colspan="2">
            </td>
          </tr>
          <tr valign="top" style="height:20px">
            <td>
            </td>
            <td style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #BABABA; font-size: 10px; line-height: 1.2578125;">Ergebnisse</span>
            </td>
            <td colspan="3">
            </td>
            <td colspan="2">
            </td>
          </tr>
          <tr valign="top" style="height:20px">
            <td colspan="6">
            </td>
            <td colspan="2" style="text-indent: 0px; text-align: right;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #000000; font-size: 10px; line-height: 1.2578125;">Dr XYZ</span>
            </td>
            <td colspan="2">
            </td>
          </tr>
          <tr valign="top" style="height:94px">
            <td colspan="10">
            </td>
          </tr>
          <tr valign="top" style="height:50px">
            <td colspan="4">
            </td>
            <td colspan="3">
              <img src="report7921484792758494686.html_files/img_0_0_3" style="width: 50px" alt="" />
            </td>
            <td colspan="3">
            </td>
          </tr>
          <tr valign="top" style="height:24px">
            <td colspan="10">
            </td>
          </tr>
          <tr valign="top" style="height:477px">
            <td colspan="3">
            </td>
            <td colspan="6" style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #000000; font-size: 10px; line-height: 1.2578125;">Bei der Aortenklappe handelt es sinken Herzkammer und der Hauptschlagader (Aorta). Einsatz einer künstlichen Herzklappe behandelt.<br/><br/>Bei beiden kathetergestützten Methoden wird über ein spezielles Ballonkathetersystem zunächst der Bereich der </span>
            </td>
            <td>
            </td>
          </tr>
          <tr valign="top" style="height:127px">
            <td colspan="10">
            </td>
          </tr>
        </table>
        <br/>
        <br/>

        <a name="JR_PAGE_ANCHOR_0_2"></a>
        <table class="jrPage" cellpadding="0" cellspacing="0" border="0" style="empty-cells: show; width: 595px; border-collapse: collapse; background-color: white;">
          <tr valign="top" style="height:0">
            <td style="width:10px"></td>
            ...
            <td style="width:24px"></td>
          </tr>
          <tr valign="top" style="height:27px">
            <td colspan="7">
            </td>
          </tr>
          <tr valign="top" style="height:20px">
            <td>
            </td>
            <td colspan="2" style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #BABABA; font-size: 10px; line-height: 1.2578125;">Ergebnisse</span>
            </td>
            <td colspan="4">
            </td>
          </tr>
          <tr valign="top" style="height:7px">
            <td colspan="7">
            </td>
          </tr>
          <tr valign="top" style="height:21px">
            <td colspan="5">
            </td>
            <td style="text-indent: 0px; text-align: right;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #6B66FA; font-size: 10px; line-height: 1.2578125;">HCH-A - Aortenklappenchirurgie, isoliert </span>
            </td>
            <td>
            </td>
          </tr>
          <tr valign="top" style="height:139px">
            <td colspan="7">
            </td>
          </tr>
          <tr valign="top" style="height:264px">
            <td colspan="2">
            </td>
            <td colspan="2" style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #000000; font-size: 10px; line-height: 1.2578125;">alten, verengten Aortenklappe erweitert. -  des AQUA-Institutes.</span>
            </td>
            <td colspan="3">
            </td>
          </tr>
          <tr valign="top" style="height:364px">
            <td colspan="7">
            </td>
          </tr>
        </table>
        <br/>
        <br/>

        <a name="JR_PAGE_ANCHOR_0_3"></a>
        <table class="jrPage" cellpadding="0" cellspacing="0" border="0" style="empty-cells: show; width: 595px; border-collapse: collapse; background-color: white;">
          <tr valign="top" style="height:0">
            <td style="width:10px"></td>
            <td style="width:10px"></td>
           ...
            <td style="width:1px"></td>
            <td style="width:23px"></td>
          </tr>
          <tr valign="top" style="height:27px">
            <td colspan="11">
            </td>
          </tr>
          <tr valign="top" style="height:20px">
            <td>
            </td>
            <td colspan="3" style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #BABABA; font-size: 10px; line-height: 1.2578125;">Ergebnisse</span>
            </td>
            <td colspan="7">
            </td>
          </tr>
          <tr valign="top" style="height:7px">
            <td colspan="11">
            </td>
          </tr>
          <tr valign="top" style="height:21px">
            <td colspan="8">
            </td>
            <td style="text-indent: 0px; text-align: right;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #6B66FA; font-size: 10px; line-height: 1.2578125;">HCH-A - Aortenklappenchirurgie, isoliert </span>
            </td>
            <td colspan="2">
            </td>
          </tr>
          <tr valign="top" style="height:65px">
            <td colspan="11">
            </td>
          </tr>
          <tr valign="top" style="height:66px">
            <td colspan="7">
            </td>
            <td colspan="3" rowspan="4" style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #000000; font-size: 10px; line-height: 1.2578125;"><br/>Bei beiden kathetergestützten Me die Indikationsstellung betrachtet.<br/><br/>Sofern nicht anders angegeben, ist die Beschreibung der Qualitätsindikatoren eine Fortschreibung der QIDB 2014 des AQUA-Institutes.</span>
            </td>
            <td>
            </td>
          </tr>
          <tr valign="top" style="height:50px">
            <td colspan="3">
            </td>
            <td colspan="2">
              <img src="report7921484792758494686.html_files/img_0_2_2" style="width: 50px" alt="" />
            </td>
            <td colspan="2">
            </td>
            <td>
            </td>
          </tr>
          <tr valign="top" style="height:24px">
            <td colspan="7">
            </td>
            <td>
            </td>
          </tr>
          <tr valign="top" style="height:161px">
            <td colspan="2">
            </td>
            <td colspan="4" rowspan="2" style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #000000; font-size: 10px; line-height: 1.2578125;">Bei der Aortenklapprzspitze über einen 3 bis 5 cm langen Hautschnitt im Rippenbereich (linker Brustkorb, im 4. oder 5. Rippenzwischenraum) freigelegt. <br/>- Beim endovaskulären Aortenklappenersatz ist lediglich ein gezielter Einstich (Punktion), zumeist in die Leistenarterie, notwendig. <br/></span>
            </td>
            <td>
            </td>
            <td>
            </td>
          </tr>
          <tr valign="top" style="height:279px">
            <td colspan="2">
            </td>
            <td colspan="5">
            </td>
          </tr>
          <tr valign="top" style="height:122px">
            <td colspan="11">
            </td>
          </tr>
        </table>
        <br/>
        <br/>

        <a name="JR_PAGE_ANCHOR_0_4"></a>
        <table class="jrPage" cellpadding="0" cellspacing="0" border="0" style="empty-cells: show; width: 595px; border-collapse: collapse; background-color: white;">
          <tr valign="top" style="height:0">
            <td style="width:10px"></td>
            <td style="width:60px"></td>
           ...
            <td style="width:1px"></td>
            <td style="width:23px"></td>
          </tr>
          <tr valign="top" style="height:27px">
            <td colspan="9">
            </td>
          </tr>
          <tr valign="top" style="height:20px">
            <td>
            </td>
            <td style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #BABABA; font-size: 10px; line-height: 1.2578125;">Ergebnisse</span>
            </td>
            <td colspan="7">
            </td>
          </tr>
          <tr valign="top" style="height:7px">
            <td colspan="9">
            </td>
          </tr>
          <tr valign="top" style="height:21px">
            <td colspan="5">
            </td>
            <td colspan="2" style="text-indent: 0px; text-align: right;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #6B66FA; font-size: 10px; line-height: 1.2578125;">HCH-A - Aortenklappenchirurgie, isoliert </span>
            </td>
            <td colspan="2">
            </td>
          </tr>
          <tr valign="top" style="height:91px">
            <td colspan="9">
            </td>
          </tr>
          <tr valign="top" style="height:50px">
            <td colspan="4">
            </td>
            <td colspan="2">
              <img src="report7921484792758494686.html_files/img_0_3_2" style="width: 50px" alt="" />
            </td>
            <td colspan="3">
            </td>
          </tr>
          <tr valign="top" style="height:24px">
            <td colspan="9">
            </td>
          </tr>
          <tr valign="top" style="height:477px">
            <td colspan="3">
            </td>
            <td colspan="5" style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif;r Brustkorb, im 4. oder 5. Rippenzwischenraum) freigelegt. <br/>- Beim endovaskulären Aortenklappenersatz ist lediglich ein gezielter Einstich (Punktion), zumeist in die Leistenarterie, notwendig. <br/><br/>Bei beiden kathetergestützten Methoden wird über ein spezielles Ballonkathetersystem zunächst der Bereich der </span>
            </td>
            <td>
            </td>
          </tr>
          <tr valign="top" style="height:125px">
            <td colspan="9">
            </td>
          </tr>
        </table>
        <br/>
        <br/>

        <a name="JR_PAGE_ANCHOR_0_5"></a>
        <table class="jrPage" cellpadding="0" cellspacing="0" border="0" style="empty-cells: show; width: 595px; border-collapse: collapse; background-color: white;">
          <tr valign="top" style="height:0">
            <td style="width:10px"></td>
            <td style="width:10px"></td>
            <td style="width:50px"></td>
            <td style="width:220px"></td>
            <td style="width:80px"></td>
            <td style="width:201px"></td>
            <td style="width:24px"></td>
          </tr>
          <tr valign="top" style="height:27px">
            <td colspan="7">
            </td>
          </tr>
          <tr valign="top" style="height:20px">
            <td>
            </td>
            <td colspan="2" style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #BABABA; font-size: 10px; line-height: 1.2578125;">Ergebnisse</span>
            </td>
            <td colspan="4">
            </td>
          </tr>
          <tr valign="top" style="height:7px">
            <td colspan="7">
            </td>
          </tr>
          <tr valign="top" style="height:21px">
            <td colspan="5">
            </td>
            <td style="text-indent: 0px; text-align: right;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #6B66FA; font-size: 10px; line-height: 1.2578125;">HCH-A - Aortenklappenchirurgie, isoliert </span>
            </td>
            <td>
            </td>
          </tr>
          <tr valign="top" style="height:139px">
            <td colspan="7">
            </td>
          </tr>
          <tr valign="top" style="height:264px">
            <td colspan="2">
            </td>
            <td colspan="2" style="text-indent: 0px; text-align: left;">
              <span style="font-family: 'DejaVu Sans', Arial, Helvetica, sans-serif; color: #000000; font-size: 10px; line-height: 1.2578125;">alten, verengten Aortenklappe erweitert. Anschließend wird eine zusammengefaltete Herzklappenchirurgie fokussieren schwere Komplikationen und Sterblichkeit. Bei der kathetergestützten Aortenklappenchirurgie wird darüber hinaus auch die Indikationsstellung betrachtet.<br/><br/>Sofern nicht anders angegeben, ist die Beschreibung der Qualitätsindikatoren des AQUA-Institutes.</span>
            </td>
            <td colspan="3">
            </td>
          </tr>
          <tr valign="top" style="height:364px">
            <td colspan="7">
            </td>
          </tr>
        </table>
        <br/>
        <br/>

        <a name="JR_PAGE_ANCHOR_0_6"></a>
        <table class="jrPage" cellpadding="0" cellspacing="0" border="0" style="empty-cells: show; width: 595px; border-collapse: collapse; background-color: white;">
          <tr valign="top" style="height:0">
            <td style="width:595px"></td>
          </tr>
          <tr valign="top" style="height:842px">
            <td>
            </td>
          </tr>
        </table>

        <![if IE]>
        <script>
          var links = document.querySelectorAll('link.jrWebFont');
          setTimeout(function() {
            if (links) {
              for (var i = 0; i < links.length; i++) {
                links.item(i).href = links.item(i).href;
              }
            }
          }, 0);
        </script>
        <![endif]>
      </td>
      <td width="50%">&nbsp;</td>
    </tr>
  </table>
</body>

</html>

How can I achieve this structure?

解决方案

If you use the html component to wrap text around image, not only do you have a problem of pulling the image from the database, the html component also creates an image of the html, hence the content may be resized/clipped depending on text length and settings on html (image) component.

I would suggest to use two textFields wrapped around the image (see jrxml) and then calculate where the text will break into next field, so you get first part of text in first textField and second part in second textField

Java code to calculate text's break point

This code demonstrate how you can use FontMetric to calculate where Jasper Report will break the text depending on the size of your textField (explanation of code is in comments). The code is not perfectly safe on NullPointer (null text) and on non over-flowing text, furthermore it can be optimized but I will leave this to OP

import java.awt.Font;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;

public class WrapImage {

    /**
     * Get position where string will break
     * @param text, the text
     * @param width, the width of the component
     * @param height, the height of the component
     * @return the position
     */
    public static int getBreakPosition(String text, int width, int height){
        //Start font context
        AffineTransform affinetransform = new AffineTransform();     
        FontRenderContext frc = new FontRenderContext(affinetransform,true,true);     

        //Set same font as used in jasper-report
        Font font = new Font("SansSerif", Font.PLAIN, 10);
        //Get height to understand how many lines
        double textheight = font.getStringBounds(text, frc).getHeight();
        int nrLines = (int)Math.floor(height/textheight);

        //init variables
        int breakPos = 0;
        int line = 1;

        //loop the lines
        while (line<=nrLines){
            //get remaining text
            String textPart = text.substring(breakPos,text.length());
            //get how much text will fit in line
            breakPos += getLineBreakPosition(textPart, width, font, frc)+1;
            line++;
        }

        return breakPos;

    }

    /**
     * Get where a single line will break
     * @param text, the text
     * @param width, width of component
     * @param font, the font used
     * @param frc, the FontRenderContext
     * @return
     */
    protected static int getLineBreakPosition(String text, int width, Font font, FontRenderContext frc){
        int breakPos  = 0;
        String tmpText = text;
        while (font.getStringBounds(tmpText, frc).getWidth()>width){
            //the break position is space
            breakPos = tmpText.lastIndexOf(' ');
            if (breakPos<=0){
                breakPos = 0;
                break;
            }
            tmpText = tmpText.substring(0,breakPos);
        }
        return breakPos;
    }
}

jrxml example using java code

In this short example, I use a parameter with some arbitrary sample text. The WrapImage class is in class path. I set a variable to the calculated break point. Then use substring on the text to get first part in first textfield, second in second textField To simplify example, I'm using an image from desktop, for how to pull image from database see: Using images stored in database

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="wrappingText" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isSummaryWithPageHeaderAndFooter="true" uuid="cd3e4316-a3c4-4847-a085-3bfa1d9425af">
    <parameter name="longText" class="java.lang.String">
        <defaultValueExpression><![CDATA["Whether you've come to ask questions, or to generously share what you know, remember that we’re all here to learn, together. Be welcoming and patient, especially with those who may not know everything you do. Oh, and bring your sense of humor. Just in case. That basically covers it. But these three guidelines may help: Rudeness and belittling language are not okay. Your tone should match the way you'd talk in person with someone you respect and whom you want to respect you. If you don't have time to say something politely, just leave it for someone who does. Be welcoming, be patient, and assume good intentions. Don't expect new users to know all the rules — they don't. And be patient while they learn. If you're here for help, make it as easy as possible for others to help you. Everyone here is volunteering, and no one responds well to demands for help. Don't be a jerk. These are just a few examples. If you see them, flag them. In summary, have fun, and be good to each other."]]></defaultValueExpression>
    </parameter>
    <variable name="breakPos" class="java.lang.Integer">
        <initialValueExpression><![CDATA[WrapImage.getBreakPosition($P{longText}, 250, 100)]]></initialValueExpression>
    </variable>
    <title>
        <band height="220" splitType="Stretch">
            <image>
                <reportElement x="10" y="20" width="80" height="80" uuid="6b4bb467-f7fd-4a15-994b-7c1a01b86428"/>
                <imageExpression><![CDATA["C:\\Users\\pette\\Desktop\\queen_bee.jpg"]]></imageExpression>
            </image>
            <textField>
                <reportElement x="100" y="0" width="250" height="100" uuid="f4507624-0410-4feb-9dc5-7d3342b882f0"/>
                <textElement textAlignment="Justified" verticalAlignment="Bottom"/>
                <textFieldExpression><![CDATA[$P{longText}.substring(0,$V{breakPos}.intValue())]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="100" width="350" height="120" uuid="8790bbdd-2066-4ceb-9fc9-dad6154df88c"/>
                <textElement textAlignment="Justified"/>
                <textFieldExpression><![CDATA[$P{longText}.substring($V{breakPos}.intValue()+1)]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

Output example

这篇关于如何在图像周围包装文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆