TextField overflow and text split

Hello,

In the design of my report, a certain textfield should be capable of being filled with a string of variable length.
So the text can easily surpass the original width.
To enable this, I've allowed the textfield to 'stretch with overflow'. This works fine, except that the text is not cut at the end but at a space character somewhere in the text. This is logical for most texts.
In our case, it is critical that the text is cut instead of logically split at a space.
How to force this ?

Geert
GeertPoels's picture
1063
Joined: Mar 7 2007 - 12:31am
Last seen: 16 years 3 weeks ago

36 Answers:

This is not directly supported by JasperReports.

One way to achieve this is to process your text and insert a zero-width space ( Unicode 200B ) after each character. This way your text will be able to break after each character.

You could also post a feature request if you think such a functionality should be present in JasperReports.

Regards,
Lucian
Post edited by: lucianc, at: 2007/07/04 15:33
lucianc's picture
72609
Joined: Jul 17 2006 - 1:10am
Last seen: 1 hour 32 min ago
Thanks Lucian.
I will try this.
However this approach will significatly increase the processing time. :unsure:

I'll report the enhancement !
GeertPoels's picture
1063
Joined: Mar 7 2007 - 12:31am
Last seen: 16 years 3 weeks ago
GeertPoels's picture
1063
Joined: Mar 7 2007 - 12:31am
Last seen: 16 years 3 weeks ago
Hi Lucian,

I just 'enriched' the texts in question with both a zero-width space and its non breakable counterpart.
http://www.fileformat.info/info/unicode/char/feff/index.htm
http://www.fileformat.info/info/unicode/char/200b/index.htm

Yet neither gave the result I hoped for.
The text is cut at a hyphen sign... .

Do you have other ideas ?

Thanks already,

Geert
GeertPoels's picture
1063
Joined: Mar 7 2007 - 12:31am
Last seen: 16 years 3 weeks ago
Using non-breakable spaces is a rather surprising idea, since the goal is to allow the text to be broken.

I'm attaching a report that illustrates my suggestion (if you care about the performance use a direct algorithm in place of the regex replacement). Run it and see whether the result is as expected. Btw, which output format are you after?

Regards,
Lucian [file name=break.jrxml size=1002]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/bre...
lucianc's picture
72609
Joined: Jul 17 2006 - 1:10am
Last seen: 1 hour 32 min ago
The results are kinda unexpected.
In the PDF, the text still is splitted.
In the RTF, the text isn't splitted. Yet the special characters causes weird output in Word. [file name=example.pdf size=1308]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/exa...
GeertPoels's picture
1063
Joined: Mar 7 2007 - 12:31am
Last seen: 16 years 3 weeks ago
This is the RTF [file name=example.zip size=614]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/exa...
Post edited by: GeertPoels, at: 2007/07/06 06:37
GeertPoels's picture
1063
Joined: Mar 7 2007 - 12:31am
Last seen: 16 years 3 weeks ago
I've changed the RTF exporter to convert Unicode 200B chars to RTF \zwbo (zero-width break opportunity - "Used to insert break opportunity between two characters" according to the RTF spec) control words.

Does the attached RTF show fine in MS Word? [file name=break.zip size=602]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/bre...
Post edited by: lucianc, at: 2007/07/06 10:13
lucianc's picture
72609
Joined: Jul 17 2006 - 1:10am
Last seen: 1 hour 32 min ago
Regarding PDF: which JR/iText versions are you using? The PDF produced here look fine (see attached). [file name=break_0_0.pdf size=1344]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/bre...
lucianc's picture
72609
Joined: Jul 17 2006 - 1:10am
Last seen: 1 hour 32 min ago
This is how Word shows your latest RTF.
The output is still 'scrambled'. size=352]
Post edited by: GeertPoels, at: 2007/07/06 10:41
GeertPoels's picture
1063
Joined: Mar 7 2007 - 12:31am
Last seen: 16 years 3 weeks ago
Wow ! Indeed your PDF looks splendid.

I use the latest Jasper version.
This is my code :

OutputStream output = null;
try {
output = new FileOutputStream(pdf);
JasperExportManager.exportReportToPdfStream(print, output);
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
log.error(e);
}
}
}
GeertPoels's picture
1063
Joined: Mar 7 2007 - 12:31am
Last seen: 16 years 3 weeks ago
RTF: I'm left clueless. If the RTF \zwbo control word does not work, I don't know what would. I think Word has an option of showing otherwise "non-visible" characters as spaces/newlines/etc and it seems that you have it enabled. Could you try to disable it?

PDF: We need to identify what differs between the two environments. Which iText version are you using (i.e. what iText jar do you have on your classpath)? Also, what Java/JRE are you using (probably doesn't affect the PDF output, but it doesn't hurt knowing)?
lucianc's picture
72609
Joined: Jul 17 2006 - 1:10am
Last seen: 1 hour 32 min ago
Good news : RTF was indeed because of the non-visible chars. Output is excellent.

PDF: I had an older version of iText : 1.4.7, now 2.0.4.
Yet, I redid your test with the new iText and the output is the same. [file name=newITEXT.pdf size=1320]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/new...

My JDK is 1.5.07.
Post edited by: GeertPoels, at: 2007/07/06 12:11
GeertPoels's picture
1063
Joined: Mar 7 2007 - 12:31am
Last seen: 16 years 3 weeks ago
Here is your compiled jasper report. [file name=break-c57af771c3cbfd087d48d443f6bce499.zip size=4500]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/bre...
GeertPoels's picture
1063
Joined: Mar 7 2007 - 12:31am
Last seen: 16 years 3 weeks ago
Sorry, it was not the iText version (my bad).

The PDF output is fine once you set the "force line break policy" flag when exporting. You can either set this flag per export using the JRPdfExporterParameter.FORCE_LINEBREAK_POLICY parameter or globally by setting the net.sf.jasperreports.export.pdf.force.linebreak.policy property (in jasperreports.properties).

Regards,
Lucian
lucianc's picture
72609
Joined: Jul 17 2006 - 1:10am
Last seen: 1 hour 32 min ago
Hi Lucian,

To enable this parameter programmatically.

Do I put it in my parameters to the fillReport function ?
like :

param.put(JRPdfExporterParameter.FORCE_LINEBREAK_POLICY, true);
JasperPrint print = JasperFillManager.fillReport(report, param, source);

Geert
GeertPoels's picture
1063
Joined: Mar 7 2007 - 12:31am
Last seen: 16 years 3 weeks ago
No, this is an export parameter so you need to set it when exporting to PDF:
Code:
<br />
JRPdfExporter exporter = new JRPdfExporter();<br />
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);<br />
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, output);<br />
exporter.setParameter(JRPdfExporterParameter.FORCE_LINEBREAK_POLICY, Boolean.TRUE);<br />
exporter.exportReport();<br />
</td></tr></tbody></table><br />
<br />
Regards,<br />
Lucian
lucianc's picture
72609
Joined: Jul 17 2006 - 1:10am
Last seen: 1 hour 32 min ago
This works.
Thanks for your patience ! :)
GeertPoels's picture
1063
Joined: Mar 7 2007 - 12:31am
Last seen: 16 years 3 weeks ago
one row disappear when page brake (not always).. even though set FORCE_LINEBREAK_POLICY to TRUE..

I don't know how to avoid it..
imclive's picture
98
Joined: Aug 29 2007 - 4:11pm
Last seen: 1 month 1 week ago

Hi all,

I am new in the forum. I have the following problem:

I am using iReport 3.0.0 and jasper reports 2.0.5. I have a report that has several columns and one column has a large content. The content is cut in the last lines. The column has font name “SansSerif” and pdf font name “Helvetica”.

I prove putting the FORCE_LINEBREAK_POLICY and the FONT_MAP parameters as following but it didn’t work:


Does anyone know what it could be happening?

Thanks in advance!!

Code:
exporter.setParameter(JRPdfExporterParameter.FORCE_LINEBREAK_POLICY, Boolean.TRUE);
 
FontKey keyHelvetica = new FontKey("SansSerif", false, false);
PdfFont fontHelvetica = new PdfFont("Helvetica", "Cp1252", false);
FontKey keyHelveticaBold = new FontKey("SansSerif", true, false);
PdfFont fontHelveticaBold = new PdfFont("Helvetica-Bold", "Cp1252", false);
 
Map<FontKey, PdfFont> fontMap = new HashMap<FontKey, PdfFont>();
fontMap.put(keyHelvetica, fontHelvetica);
fontMap.put(keyHelveticaBold, fontHelveticaBold);
 
exporter.setParameter(JRPdfExporterParameter.FONT_MAP, fontMap);</td></tr></tbody></table>
flacap's picture
772
Joined: Aug 11 2009 - 12:36pm
Last seen: 13 years 7 months ago

Hi,

 

What you experience is explained in this FAQ here:

http://jasperforge.org/uploads/publish/jasperreportswebsite/trunk/faq.ht...

 

SansSerif is a Java logical font, not a physical font. It is not a good idea to use logical fonts, because you never know which is the real font behind that name. On different platform, that font is different. On Windows, it might resolve to Arial, on Unix, it might resolve to Helvetica.

The solution is to make sure what fonts are used, by shipping the fonts with your application/reports. You can ship the fonts by packaging them in what we call JasperReports font extensions. You can find one such font extension in the /demo/fonts folder of the JasperReports distribution package. All the samples under /demo/samples make use of fonts shipped in that font extension folder, because it is part of the samples classpath when they are run.

You could create a similar package and zip it up in a JAR file that you then add to your application classpath and all you have to take care after is the fontName attribute. Nothing else. Not even the FONT_MAP export parameter which is kind of deprecated nowadays, after font extension support was introduced.

 

I hope this helps.
Teodor

 

teodord's picture
44871
Joined: Jun 30 2006 - 9:00am
Last seen: 4 hours 32 min ago

Thank you very much for your quick response!!.

I changed the SansSerif font by Arial in the iReport and in the FONT_MAP map but the text is cut anyway.

I will see the example you mentioned in the JasperReports distribution package.

Thank you again!

Regards

 

flacap's picture
772
Joined: Aug 11 2009 - 12:36pm
Last seen: 13 years 7 months ago

I have another question, you say that "the FONT_MAP export parameter which is kind of deprecated nowadays, after font extension support was introduced". But I am using iReport 3.0.0 and jasper reports 2.0.5. Is font extension support introduced in this version? Because in the \\demo\\samples\\fonts directory I don't have a font.xml file but a build.xml file. I have a file named jasperreports.properties too. I only have to create the two files (build.xml and jasperreports.properties) and the font directory with the ttf fonts, do I?

Thanks in advance!

Regards

flacap's picture
772
Joined: Aug 11 2009 - 12:36pm
Last seen: 13 years 7 months ago

Hi,

 

Font extensions were introduced in JR 3.1.2, back in January. They greatly simplify work with fonts, so I suggest you upgrade.

Using version that old you might encounter bugs that are fixed for a long time now and would be basically a waste of time to get around them.

And I said /demo/fonts, not /demo/samples/fonts.

 

Thanks,

Teodor

 

teodord's picture
44871
Joined: Jun 30 2006 - 9:00am
Last seen: 4 hours 32 min ago

And BTW, make sure you use isStretchWithOverflow="true" for your text fields, if you want them to get larger at runtime in order to display their entire content.

 

I hope this helps.
Teodor

 

teodord's picture
44871
Joined: Jun 30 2006 - 9:00am
Last seen: 4 hours 32 min ago

I can't update the version because the application will be in production in the next days, so we can't risk updating the version now so close to the launch.


I already had the isStretchWithOverflow property in true and I had the FORCE_LINEBREAK_POLICY in true in the exporter but it didn’t work.


I put the font directory in the JAR file and I added it to the classpath but the error persists. It occurs when the text is long. The fontName in the iReport is Arial and the pdfFontName is Helvetica.


Theodor, I appreciated your help. Thank you very much!.


Kind regards



flacap's picture
772
Joined: Aug 11 2009 - 12:36pm
Last seen: 13 years 7 months ago

Hi,

 

if you are not using at least JR 3.1.2, adding the font extension jar to the classpath is useless.

Post a sample JRXML to this thread. So far I haven't seen any piece of JRXML presenting the problem.

Also attach the resulting PDFs you get filling those sample reports.

 

Thank you,

Teodor

 

teodord's picture
44871
Joined: Jun 30 2006 - 9:00am
Last seen: 4 hours 32 min ago

I can’t attach the pdf without the express authorization of the company because it contains information that is confidential. I attach an image of the pdf, it is of the second page of the report, which has several columns and one column (which is on the image) has a long text and it is cut.
I show the parts of the jrxml where this column is configurated.

Code:
<jasperReport
 
  ....
  <field name="commodity.field.product.name" class="java.lang.String">
    <property name="net.sf.jasperreports.print.keep.full.text" value="true" />
  </field>
  ....
 
  ....
  <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true"
       evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
    <reportElement x="159" y="1" width="67" height="15" key="textField-16">
      <printWhenExpression>
        <![CDATA[new Boolean($P{list.report.secondary.detail} != null)]]>  
      </printWhenExpression>
    </reportElement>
    <box leftPadding="2" rightPadding="2">
      <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
      <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
      <bottomPen lineWidth="0.0" lineColor="#000000"/>
      <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
    </box>
    <textElement>
      <font fontName="Arial" pdfFontName="Helvetica" size="6" isBold="false" 
          isPdfEmbedded ="false" pdfEncoding ="Cp1252"/>
    </textElement>
    <textFieldExpression class="java.lang.String">
      <![CDATA[$F{commodity.field.product.name}]]>
    </textFieldExpression>
  </textField>
  ....
</jasperReport></td></tr></tbody></table>
flacap's picture
772
Joined: Aug 11 2009 - 12:36pm
Last seen: 13 years 7 months ago

The fonts directory has the following fonts:
ARIAL.TTF, ARIALBD.TTF, ARIALBI.TTF, ARIALI.TTF
I changed  the code where I configure the FONT_MAP and FORCE_LINEBREAK_POLICY parameters as follows.
Thanks in advance!!!

Regards
 

Code:
@Override
  protected JRExporter createExporter() {
     JRExporter exporter = super.createExporter();
     exporter.setParameter(JRPdfExporterParameter.FORCE_LINEBREAK_POLICY,
         Boolean.TRUE);
     Map fontMap = new HashMap();
     FontKey keyArial = new FontKey("Arial", false, false);
     PdfFont fontArial = new PdfFont("ARIAL.TTF", "Cp1252", true);
     FontKey keyArialBold = new FontKey("Arial", true, false);
     PdfFont fontArialBold = new PdfFont("ARIALBD.TTF", "Cp1252", true);
     FontKey keyArialItalic = new FontKey("Arial", false, true);
     PdfFont fontArialItalic = new PdfFont("ARIALI.TTF", "Cp1252", true);
     FontKey keyArialBoldItalic = new FontKey("Arial", true, true);
     PdfFont fontArialBoldItalic = new PdfFont("ARIALBI.TTF", "Cp1252", true);
     fontMap.put(keyArial, fontArial);
     fontMap.put(keyArialBold, fontArialBold);
     fontMap.put(keyArialItalic, fontArialItalic);
     fontMap.put(keyArialBoldItalic, fontArialBoldItalic);
     exporter.setParameter(JRPdfExporterParameter.FONT_MAP, fontMap);
     return exporter;
  }</td></tr></tbody></table>
flacap's picture
772
Joined: Aug 11 2009 - 12:36pm
Last seen: 13 years 7 months ago

Hi,

 

We don't need to see any confidential company document. You could have created a report with a single text field and put inside it the hardcoded text value that gets cut off. I mean, you could have isolated the problem into a simpler document that would have helped us help you.

 

Anyway, you need to tell us which is the exact text that should appear in that text field that we only partially see in your screenshot. We don't know what gets cut and without having the text value we cannot reproduce the problem on our machines.

 

And BTW, the "net.sf.jasperreports.print.keep.full.text" has no effect inside a report field declaration. As our config reference says, it can be set globally, per report or at text element level:

http://jasperreports.sourceforge.net/config.reference.html#net.sf.jasper...

 

Thank you,

Teodor

teodord's picture
44871
Joined: Jun 30 2006 - 9:00am
Last seen: 4 hours 32 min ago

Hello Theodor

The complete text is: RESIN SOLN UN1866 SOLOTIOM, 3. UN1866 PG III FP 32C EMS: F-E, S-E ARTICLETT:NON-ASSIGNED 1 X 20 DRY CARGO STEEL IBCS: RESIN SOLOTIOM, 3. UN1866 PG III FP 32C EMS: F-E, S-E ARTICLE#:NON-ASSIGNED L X 20 DRY CARGO 5 FIBERBOARD BOXES 20 GLASS JARS RESIN SOLUTIOM 3 UNI 8 6 6 PG III FP 32C EMS FE SE CURING AGENTS NON HAZ EMPTY FIBERBOARD BOXES

It cuts FIBERBOARD BOXES words.

I attach the pdf and the jrxml.

I put the <property name="net.sf.jasperreports.print.keep.full.text" value="true" /> with the report properties.

Thanks in advance!

flacap's picture
772
Joined: Aug 11 2009 - 12:36pm
Last seen: 13 years 7 months ago

Hi Teodor,

I am working on this issue with Sandra and I would like to introduce some data about our current environment that could be helpful to clarify the problem.

Our application builds the reports using JasperServer 2.1. Basically, we have our jrxml files loaded in our JasperServer instance and we generate the JasperPrint object by the web service that JasperServer provides.

Once we obtain the JasperPrint from the web service, we export it using SpringMVC out of the box pdf View (org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView) which uses a JRPdfExporter. Actually, we have extended it to re-implement fillReport(java.util.Map) method because the JasperPrint object it is already filled by JasperServer.

The applications are running in the same JBoss instance on a Linux Server.

I believe that the fields height calculation is being made by our web application while exporting to PDF, but I am not completely sure.

Am I right with that?

In our web application we use JasperReport 2.0.5 and iText 1.3.1 in order to maintain a consistence with JasperServer 2.1, which is using those versions.

Thanks for all your help in this one.

 

Regards,

Fernando

fas81's picture
180
Joined: Aug 18 2009 - 10:21am
Last seen: 13 years 7 months ago

Guys,

 

I told you what the problem is back on August 12, when I told you to read this FAQ:

http://jasperforge.org/uploads/publish/jasperreportswebsite/trunk/faq.html?group_id=252#FAQ25

Did you guys read it carefully and made sure you understand what it says? Because since then we are just running in circles.

Have you paid attention to this fragment in particular:

"One thing to pay attention to is making sure that the fonts set in the report via fontName attributes are actually available in the Java VM, because AWT silently uses the default font when a specific font cannot be loaded. "

What this tells you is that you are using the Arial font as fontName attribute in your reports. But you run those reports on a Linux server, where there is no font called Arial!!! Is it? Open up a document editor in Linux. Can you see Arial available anywhere??

Arial is a Microsoft font available only on Windows. You either install Arial on that Linux Server, or use some other font that is available on Linux. This is what I said all along.

 

You'd say, OK, but didn't we use the FONT_MAP exporter parameter to locate the Arial TTF file? Of course you did, but that is another story! That is only for the sake of the PDF exporter, which needs to embed the font in PDF. But the font metrics calculations are already done in the fill process, using the wrong font, because you did not install the Arial font into the Linux system, did you? You just put it somewhere on disk. If you did not install it in the system as a system font, the JVM does not know about it and uses something else. Who knows what. With different font metrics. And you end up with a classical font metrics mismatch problem.

 

Now that you gave me more details about what you do. I'm not sure why you implemented a fillReport method, since all the report filling is done in JasperServer. Actually, I'm not sure why you have implemented the PDF export in your application at all, since the JasperServer web services are capable of delivering PDF directly, not only the JasperPrint object. if you did it for the sake of using the FONT_MAP in the exporter, then note JasperServer already has support for configuring the font maps with Spring files. Note the WEB-INF/pdfFontBeans.xml file inside JasperServer, which has most of its content commented out, just as an example of the font mappings you could do.

 

I hope this helps.
Teodor

 

teodord's picture
44871
Joined: Jun 30 2006 - 9:00am
Last seen: 4 hours 32 min ago

Hi Teodor,

Thanks. I have been tring to configure my environment and reports according to your explanation but I am still facing the same issue.

This is what I have done:

I installed Arial fonts in linux following this steps http://vietunicode.sourceforge.net/howto/fontlinux.html and referenced in Java using the JAVA_FONTS variable as it is also explained in that link.

I have updated the textfields from my jrxml files like this:

<textElement>
          <font fontName="Arial" pdfFontName="Arial" size="6"/>
</textElement>

I have also tried different alternatives like updating the JRE_HOME/lib/fontconfig.properties to map the "Serif" logical fonts to our arial.ttf or setting the fontName to "Serif"

And I have also tried using the FORCE_LINEBREAK_POLICY with values true and false.

I still can't have my reports working properly. Is there anything else that I am missing in my configuration?

I have noticed in different forums that some people talk about the font.properties file and I only have a fontconfig.properties in my JRE/lib, I also noticed that there is a psfontj2d.properties with a lot of mappings. Should I add/update any of those files to have my reports finally working?

Regarding the decision of exporting the files from the application instead of using JasperServer directly, that was done before we joined our application development and it is something we are not allowed to change, so we must continue filling the report with JasperServer and exporting it to PDF from our application using Spring's views.

Thanks for all your help and patience.

 

Regards

Fernando

fas81's picture
180
Joined: Aug 18 2009 - 10:21am
Last seen: 13 years 7 months ago

Hi,

 

I don't know what else could be wrong. I just gave you the explanations about how JR works and suggestions about how to work with fonts properly. There is a certain amount of debug work that you need to do yourself, to understand what happens, as it is clear by now that your environment is very special and thins do not work as expected.

 

I'm not a Unix guru, so I cannot tell you if you have installed the fonts propertly. That would be the first thing to test. See if the other document editors on the machine pick up the font. If not, it means it is not installed. The best thing would be to write a little Java program that lists the available font names using the java.awt.GraphicsEnvironmentclass.

Or simply launch iReport on the machine and see what fonts it proposes in the comobobx of the fontName property.

If you don't see Arial in the list, then it is not installed properly.

 

I hope this helps.
Teodor

 

teodord's picture
44871
Joined: Jun 30 2006 - 9:00am
Last seen: 4 hours 32 min ago

I tried setting the flag FORCE_LINEBREAK_POLICY to TRUE. But still that is not helping my cause.. Please Help..........

pratikpai's picture
Joined: Apr 14 2011 - 1:40am
Last seen: 11 years 11 months ago
Feedback
randomness