I want to print in block letters in a jasper print. E.g. I have a Bank Format and I want the name to come in the boxes. How does one do that?
Name: [F][I][R][S][T][ ][N][A][M][E]
Option1 - Print cell by cell with a ${FIRST_NAME}.charAt(x) - which is unsustainable
Option2 - Add spaces between characters but this changes based on font size
Option3 - Add padding spaces to the end of the text. Stretch the text to the full width available and characters get distributed equally - how does one do this?
This is normal achieved using a monospaced font.
If you like to achieve it with other fonts or the spacing is very large you can use a subreport (that is called every time you need the fixed space)
Example
Main report (I'm using parameter to test)
Call the subreport with a datasource that contains each letter, I'm using $P{testText}.split("") , since char[] is not allowed in the JRBeanArrayDataSource.
Note: split will give empty first String in java7 but not in java8
<?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="MainFixedSpace" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d2bcb5ab-c751-4f39-8753-561b8a6ac629">
<parameter name="testText" class="java.lang.String">
<defaultValueExpression><![CDATA["Hello world"]]></defaultValueExpression>
</parameter>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["C:\\jdd\\projects\\StackTrace\\jasper\\"]]></defaultValueExpression>
</parameter>
<title>
<band height="25" splitType="Stretch">
<subreport>
<reportElement x="0" y="0" width="555" height="25" uuid="76f53ca9-da1f-46c8-bb3b-aca0dc43d2d3"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanArrayDataSource($P{testText}.split(""))]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "MainFixedSpace_subreport.jasper"]]></subreportExpression>
</subreport>
</band>
</title>
</jasperReport>
Subreport
Setup the the fixed space column count in my case 20 columns on each row (you need to adapted it to your case) and set printOrder="Horizontal" The _THIS field will let you access the letter.
<?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="MainFixedSpace_subreport" columnCount="20" printOrder="Horizontal" pageWidth="555" pageHeight="802" columnWidth="27" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="77ba37de-32e1-4ec6-8496-58d716d0340d">
<field name="_THIS" class="java.lang.String"/>
<detail>
<band height="25" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="27" height="25" uuid="ffb27000-41ba-419f-8836-b24dbb0dbb25"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Output
Related
Need Help with the below.
If i wanted to capture a value for a particular tag , how do i approach the same ?. for example
I wanted to capture the value(800.422.2762 (U.S. and Canada)) from the highlighted tag .
<text top="89" left="611" width="177" height="11" font="1">800.422.2762 (U.S. and Canada)</text>
in short , I wanted to hardcode this tag to capture the underlying value every time my program runs .
Sample XML:
<?xml version="1.0" encoding="UTF-8"?>
<pdf2xml producer="popple`enter code here`r" version="0.51.0">
<page number="1" position="absolute" top="0" left="0" height="1188" width="918">
<fontspec id="0" size="27" family="Helvetica" color="#000000"/>
<fontspec id="1" size="9" family="Helvetica" color="#000000"/>
<fontspec id="2" size="9" family="Helvetica" color="#000000"/>
<fontspec id="3" size="9" family="Times" color="#000000"/>
<fontspec id="4" size="12" family="Helvetica" color="#000000"/>
<fontspec id="5" size="12" family="Helvetica" color="#000000"/>
<fontspec id="6" size="9" family="Helvetica" color="#000000"/>
<image top="27" left="54" width="203" height="108" src="ext-resources\bin\asdf-1_1.jpg"/>
<text top="103" left="346" width="123" height="28" font="0"><b>INVOICE</b></text>
<text top="75" left="611" width="211" height="11" font="1">+1 913.217.6000, Fax +1 913.341.3742</text>
<text top="89" left="611" width="177" height="11" font="1">800.422.2762 (U.S. and Canada)</text>
<text top="102" left="611" width="230" height="11" font="1">headquarters#armaintl.org, www.arma.org</text>
<text top="32" left="611" width="104" height="11" font="1">ARMA International</text>
</page>
</pdf2xml>
So far, i have tried the below approach.
I am succeeded in extracting the data but i wanted to extract a particular value based on the hard coded tag. Kindly Help with the approach.
WITH data
AS (SELECT xmltype (
'<?xml version="1.0" encoding="UTF-8"?>
<pdf2xml producer="popple`enter code here`r" version="0.51.0">
<page number="1" position="absolute" top="0" left="0" height="1188" width="918">
<fontspec id="0" size="27" family="Helvetica" color="#000000"/>
<fontspec id="1" size="9" family="Helvetica" color="#000000"/>
<fontspec id="2" size="9" family="Helvetica" color="#000000"/>
<fontspec id="3" size="9" family="Times" color="#000000"/>
<fontspec id="4" size="12" family="Helvetica" color="#000000"/>
<fontspec id="5" size="12" family="Helvetica" color="#000000"/>
<fontspec id="6" size="9" family="Helvetica" color="#000000"/>
<image top="27" left="54" width="203" height="108" src="ext-resources\bin\asdf-1_1.jpg"/>
<text top="103" left="346" width="123" height="28" font="0"><b>INVOICE</b></text>
<text top="75" left="611" width="211" height="11" font="1">+1 913.217.6000, Fax +1 913.341.3742</text>
<text top="89" left="611" width="177" height="11" font="1">800.422.2762 (U.S. and Canada)</text>
<text top="102" left="611" width="230" height="11" font="1">headquarters#armaintl.org, www.arma.org</text>
<text top="32" left="611" width="104" height="11" font="1">ARMA International</text>
</page>
</pdf2xml>')
xmldoc
FROM DUAL)
SELECT x.*
FROM data,
XMLTABLE ('/pdf2xml/page/text'
PASSING xmldoc
COLUMNS text VARCHAR2 (50) PATH '/text') x
/
Output:
TEXT
--------------------------------------------------
INVOICE
+1 913.217.6000, Fax +1 913.341.3742
800.422.2762 (U.S. and Canada)
headquarters#armaintl.org, www.arma.org
ARMA International
Just change the XQuery from
'/pdf2xml/page/text'
to
'/pdf2xml/page/text[#top=89]'
and the result will be
800.422.2762 (U.S. and Canada)
If you only have one source document and only want one node value, you could use XMLQuery instead of XMLTable, with a slightly variation on #wolφi's XPath:
select XMLQuery('/pdf2xml/page/text[#top=89]/text()'
passing xmldoc
returning content) as text
from data;
which gives you an XML fragment, or
select XMLQuery('/pdf2xml/page/text[#top=89]/text()'
passing xmldoc
returning content).getStringVal() as text
from data;
which gives you a string:
TEXT
------------------------------
800.422.2762 (U.S. and Canada)
XMLTable is the way to go if you really have multiple documents or nodes though, of course.
Or change the query into:
SELECT x.*
FROM data,
XMLTABLE ('/pdf2xml/page/text'
PASSING xmldoc
COLUMNS
text VARCHAR2 (50) PATH '/text',
top number PATH '#top',
left number PATH '#left',
width number PATH '#width',
height number PATH '#height',
font number PATH '#font'
) x
where x.top = 89
and x.left = 611
and x.width = 177
and x.height = 11
and x.font = 1;
My report is something like below.
In the shown report, text in black color is static and the text in RED are fields to be replaced with some value dynamically by java.
In the above picture, line 1 and 2 I have no problem.
But for the paragraph I am not sure what to use? Should I use static-text box or Text field? If I use part static-text box and part dynamic, its becoming clumsy and difficult to maintain the line spacing.
So please advise how to design the following report in jasper studio 5.6.
Use a textField and then String concatenation on your text, when you need to number format use the NumberFormat API or the DecimalFormat API
If you need to break line, set bold text you can use html to achive this by setting markup="html" on the textElement
Example
<?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="test" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0ef33a9d-fd2b-46cc-9ec4-c6906db8097b">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="testNumber" class="java.lang.Double" isForPrompting="false">
<defaultValueExpression><![CDATA[new Double(10000.23445)]]></defaultValueExpression>
</parameter>
<title>
<band height="57" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="373" height="57" uuid="2f795f8a-43fa-4b89-a173-76854a13ba48"/>
<textElement markup="html"/>
<textFieldExpression><![CDATA["Just use a textField and string concat the other text a " + java.text.NumberFormat.getNumberInstance().format($P{testNumber}) + " as you see you can even format it and remeber if you set as html you can <br/> break line and <b>bold</b> text"]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
Output
I think I am having a problem with evaluation time. In this example, you see the variable $P{REPORT_PARAMETERS_MAP}.get("LastPageNumber") reflecting the last page correctly; it updates correctly. But when I use it for the "printWhenExpression" of a detail band field (experiment) or for the page header band (my goal) it does not work.
I am using "page" evaluation time throughout which seems to work for the variable itself. But the printWhenExpressions are not working using this variable.
Here is my xml file :
<?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="report3" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<queryString>
<![CDATA[select * from users]]>
</queryString>
<field name="userName" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<pageHeader>
<band height="23" splitType="Stretch">
<textField evaluationTime="Page">
<reportElement x="120" y="0" width="238" height="20">
<printWhenExpression><![CDATA[!$V{PAGE_NUMBER}.equals($P{REPORT_PARAMETERS_MAP}
.get("LastPageNumber"))]]></printWhenExpression>
</reportElement>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["please don't print on last page - page header"]]></textFieldExpression>
</textField>
<textField evaluationTime="Page">
<reportElement x="380" y="0" width="175" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["last page variable is: " + $V{PAGE_NUMBER}.equals($P{REPORT_PARAMETERS_MAP}
.get("LastPageNumber"))]]></textFieldExpression>
</textField>
<textField>
<reportElement x="2" y="2" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["Page header"]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<detail>
<band height="21" splitType="Stretch">
<textField evaluationTime="Page">
<reportElement x="381" y="0" width="174" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["last page variable is: " + $V{PAGE_NUMBER}.equals($P{REPORT_PARAMETERS_MAP}
.get("LastPageNumber"))]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{userName}]]></textFieldExpression>
</textField>
<textField evaluationTime="Page">
<reportElement x="119" y="0" width="191" height="20">
<printWhenExpression><![CDATA[!$V{PAGE_NUMBER}.equals($P{REPORT_PARAMETERS_MAP}
.get("LastPageNumber"))]]></printWhenExpression>
</reportElement>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["please don't print on last page - detail"]]></textFieldExpression>
</textField>
</band>
</detail>
<lastPageFooter>
<band height="50">
<line>
<reportElement x="188" y="21" width="100" height="1">
<printWhenExpression><![CDATA[new Boolean($P{REPORT_PARAMETERS_MAP}.put("LastPageNumber",
$V{PAGE_NUMBER}))]]></printWhenExpression>
</reportElement>
</line>
<textField evaluationTime="Page">
<reportElement x="380" y="2" width="174" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["last page variable is: " + $V{PAGE_NUMBER}.equals($P{REPORT_PARAMETERS_MAP}
.get("LastPageNumber"))]]></textFieldExpression>
</textField>
</band>
</lastPageFooter>
</jasperReport>
My solution was to make two reports (one for the last page) and then join them (jasperPrints) using the array list option for he jasper exporter:
List jasperPrintList = new ArrayList();
JasperPrint jasperPrint1 =
JasperFillManager.fillReport(
jasperReport1,
parameters,
c
);
JasperPrint jasperPrint2 =
JasperFillManager.fillReport(
jasperReport2,
parameters,
c
);
jasperPrintList.add(jasperPrint1);
jasperPrintList.add(jasperPrint2);
JRPdfExporter exporter = new JRPdfExporter();
ByteArrayOutputStream byte_report = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "test_report_name");
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, byte_report);
exporter.exportReport();
I want to add bullet point to my static text and to a text field, i added style tag to the jrxml but it didn't work for me :
Here's the example :
TextField :
<textField isBlankWhenNull="true">
<reportElement uuid="cfd514bc-d1c5-4369-b10d-71042b046e37" x="11" y="0" width="400" height="12"/>
<textElement/>
<textFieldExpression><![CDATA[<style size="40">.</style>$F{LMSG}]]></textFieldExpression>
</textField>
StaticText:
<staticText>
<reportElement uuid="c1485aba-09a4-4c7b-9106-0893341f1368" x="44" y="107" width="309" height="15"/>
<textElement>
<font size="9"/>
</textElement>
<text><![CDATA[<style size="40">.</style>Je déduis cet avoir de ma commande]]></text>
</staticText>
You can use styled markup.
Try to use <li> tag.
The sample
The jrxml file:
<?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="bullet_sample" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="aa7ced41-689a-4b8d-94aa-ee79c243a7a8">
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement uuid="bf39def6-a3a7-4fa1-9e99-e488b3567974" x="159" y="31" width="100" height="20"/>
<textElement markup="styled"/>
<textFieldExpression><![CDATA["<li>Text with bullet</li>"]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
The result will be (via preview in iReport):
In case using field, the right expression will be:
<textField>
<reportElement x="171" y="0" width="100" height="20"/>
<textElement markup="styled"/>
<textFieldExpression><![CDATA["<li>" + $F{fieldName} + "</li>"]]></textFieldExpression>
</textField>
Note:
You can find more info about styled text in Style a text field in JasperReports post and in Styled Text sample
I used the <li> tag, but it introduced padding which did not look good. So, I used "\u2022" which gives a round symbol like
<textFieldExpression><![CDATA["\u2022 Line1\n\u2022 Line2"]]></textFieldExpression>
So, this looks much cleaner
I am using iReport 4.5.0, and want to embed an SVG image to generate a PDF.
When using iReport's preview option to run the report, the image won't get rendered to the PDF (i.e. there's an empty space where the image is supposed to be).
I do not see any exceptions in the logs during execution, what's going on?
Here's the image tag code snippet:
<?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="reportSVGTest" language="groovy" pageWidth="595"
pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20"
topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="50">
<staticText>
<reportElement x="81" y="13" width="100" height="20"/>
<textElement/>
<text><![CDATA[Hello]]></text>
</staticText>
</band>
</title>
<detail>
<band height="443" splitType="Stretch">
<image hAlign="Center" vAlign="Middle" isUsingCache="true" isLazy="true">
<reportElement x="12" y="17" width="512" height="403"/>
<imageExpression class="net.sf.jasperreports.engine.JRRenderable">
<![CDATA[
net.sf.jasperreports.renderers.BatikRenderer.getInstance(
new java.io.File("C:\\Users\\akshayd\\Desktop\\polygons1.svg"))
]]>
</imageExpression>
</image>
</band>
</detail>
</jasperReport>
I wanted to embed a SVG text in my report, and this topic helped me succeed.
I am using Jaspersoft Studio with full support for JasperReports 5.5.0.
Here is how i did :
Create a image element
Choose Custom expression and let expression empty
Modify JRXML source: add a tag imageExpression in the tag image
<image>
<reportElement x="0" y="60" width="50" height="50" uuid="c0d0856e-0bb1-4634-b8b0- 6f7ace0e3ff3"/>
<imageExpression><![CDATA[net.sf.jasperreports.renderers.BatikRenderer.getInstanceFromText("<svg:svg xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.0\" width=\"88\" height=\"21\" id=\"svg\"><svg:g id=\"layer1\"><svg:path id=\"path1\" d=\"M 61.799149,2.0342178 L 67.119461,2.0342178 L 67.119461,19.213905 L 61.799149,19.213905 L 61.799149,2.0342178 z\" style=\"text-anchor:start;fill:#FF0000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;\" /></svg:g> </svg:svg>")]]></imageExpression>
</image>
So, i use the method getInstanceFromText of BatikRenderer.
Do not forget to protect double quotes in your SVG text.