HTML code in Dynamic jasper - java

I tried to add an HTML styled text to my dynamic jasper report
JRDesignBand band = new JRDesignBand();
band.setHeight(20); // Set band height
JasperDesign jasperDesign = new JasperDesign();
JRDesignTextField textField = new JRDesignTextField();
textField.setX(0); // x position of text field.
textField.setY(0); // y position of text field.
textField.setWidth(860); // set width of text field.
textField.setHeight(20); // set height of text field.
JRDesignExpression jrExpression = new JRDesignExpression(); // new instanse of expression. We need create new instance always when need to set expression.
jrExpression.setText("<p>text space</p>"); // Added String before field in expression.
textField.setExpression(jrExpression);
textField.setMarkup("html");
band.addElement(textField);
((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);
JasperDesignViewer.viewReportDesign(jasperDesign);
But the text still has HTML tags.

As far as I know, the html tags allowed are very limited and for basic text formatting.
<b>bold</b>
<i>italic</i>
<u>underlined</u>
And thats it.
There is a discussion here about "html" and "styled" markup having issues when exporting to PDF http://community.jaspersoft.com/questions/521757/html-markup-pdf

Related

Get start position of character styled texts in a paragraph in Aspose Words for Android

It has being parsed Ms Word documents with Aspose Words for Android below code. All of paragraphs in the document have inline character styled texts seperatelly. I've text and style of them but are there any way to get start position of them in its paragraph string like String.indexOf() ? It may be convert to string, but style control is not possible in this case.
Document doc = new Document(file); // Get word document.
NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true); // get all paragraphs.
for (Paragraph prg : (Iterable<Paragraph>) paras) {
for (Run run : (Iterable<Run>) prg.getChildNodes(NodeType.RUN, true)){
boolean defaultPrgFont = run.getFont().getStyle().getName().equals("Default Paragraph Font");
// Get different styled texts only.
if (!defaultPrgFont){
// Text in different styled according to paragraph.
String runText = run.getText();
// Style of the different styled text.
String runStyle = run.getFont().getStyle().getName()
// Start position of the different styled text in its paragraph.
int runStartPosition; // ?
}
}
}
You can calculate length of text in runs before the styled run. Something like this.
Document doc = new Document("C:\\Temp\\in.docx"); // Get word document.
NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true); // get all paragraphs.
for (Paragraph prg : (Iterable<Paragraph>) paras) {
int runStartPosition = 0;
for (Run run : (Iterable<Run>) prg.getChildNodes(NodeType.RUN, true)){
boolean defaultPrgFont = run.getFont().getStyle().getName().equals("Default Paragraph Font");
// Get different styled texts only.
if (!defaultPrgFont){
// Text in different styled according to paragraph.
String runText = run.getText();
// Style of the different styled text.
String runStyle = run.getFont().getStyle().getName();
System.out.println(runStartPosition);
}
// Position is increased for all runs in the paragraph.
// Note that some runs might represent field codes and are not normally displayed.
runStartPosition += run.getText().length();
}
}

Select single word or string (doubleclick) in order to edit

I'm testing the functions of text editor and I would like to be able to select text (either a word or a string between 'p' tags) and then edit it by clicking a button (B for bold or I for Italic). Much like the editor used here at Stackoverflow. This will add an html B tag so that it shows as bold.
I know the following Xpath leads to a string of text but I can't find how to select this text or how to select a word in this text.
This is part of the page source (text editor is in an iframe)
<html>
<head>
<body>
<p>
This is a line of text to test Bold
</p>
<p>
This is a line of text to test Italic
</p>
</body>
Where this
driver.findElement(By.xpath("/html/body/p[1]")
leads to a text:
This is a line of text to test Bold
I would like to select the word 'Bold' (or the entire sentence if this is easier) and then click a button above the text to make it Bold
Try this
Actions a = new Actions(driver);
a.doubleClick(driver.findElement(By.xpath("/html/body/p[1]")).build().perform();
What you could do is to use an <html> Tag within JLabels for instance.
What I have in mind looks like the following:
boldButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String formattedText = "<html><b>" + text + "</b></html>";
label.setText(formattedText);
}
}
italicButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String formattedText = "<html><i>" + text + "</i></html>";
label.setText(formattedText);
}
}
Unforunately, I do not know how your text editor looks like, but this is the way I would approach the problem.
Anyways, in a larger scale you can still use HTML tags to format the text (e.g., in a TextArea). In order to get the highlighted/selected items you might want to have a look at this thread how to get highlighted text in jtextarea.
So you could try something like this:
// retrieve selected text from text area
String selectedText = textArea.getSelectedText();
// get whole text
StringBuffer text = new StringBuffer(textArea.getText());
// remove the selected text
text.remove(textArea.getSelectionStart(), textArea.getSelectionEnd());
// make the selected text, e.g., bold.
selectedText = "<b>" + selectedText + "</b>";
// add the bold text to the whole text
text.insert(textArea.getSelectionStart(), selectedText);
//eventually apply outer <html> tags
String result = "<html>" + text.toString() + "</html>";
// set new text (with bold element) to the text area
textArea.setText(result);
Please note that I have not tried this, so you might tweak some things in order to make it work.

Pdf Writer is not printing all html text after parsing html text with XMLWorkerHelper

I have below html text.
<p>Use your card when shopping online at Our store and receive 20% off on your very first order.</p> <p>Our store delivers the latest styles at affordable prices for young trendsetters. For those who love putting a sexy spin on their everyday wardrobe, look no further--Our store is the ultimate style destination! Shop the latest pants, dresses and more. Shop now! We've got everything you're looking for.</p> <div> <p>To redeem this offer, click the Redeem button, sign up for our newsletter, and we will email you a discount code, entitling you to 70% off on your first order.</p> </div>
And i am parsing it with XMLWorkerHelper like below.
StringReader strReader = new StringReader("<html><head><style> body {font-family:ArialUnicodeMS; font-size:9pt;} </style></head><body>" + htmlString+"</body></html>");
try {
PdfHtmlElementHandler eh = new PdfHtmlElementHandler();
p = new Paragraph();
XMLWorkerHelper.getInstance().parseXHtml(eh, strReader);
List<Element> htmlList = eh.getHtmlList();
for(int i=0; i<htmlList.size(); i++) {
p.addElement(htmlList.get(i));
}
}
PdfPCell cell_2 = new PdfPCell();
PdfPTable table = new PdfPTable(2);
cell_2.addElement(p);
table.addCell(cell_2);
Now, See my html text carefully.It has a <div> tag in the last. After parsing with XMLWorkerHelper, i am adding element in paragraph object like this
p.addElement(htmlList.get(i));
Now the generated pdf is not displaying the complete text.Its printing text by trimming last <div> tag text like below.
Use your card when shopping online at Our store and receive 20% off on your very first order.
Our store delivers the latest styles at affordable prices for young trendsetters. For those who love putting a sexy spin on their everyday wardrobe, look no further--Our store is the ultimate style destination! Shop the latest pants, dresses and more. Shop now! We've got everything you're looking for.
How to resolve this issue.I want full text to be printed.Is this because i am adding into paragraph object?
Because when i used below code.
StringReader strReader = new StringReader("<html><head><style> body {font-family:ArialUnicodeMS; font-size:9pt;} </style></head><body>" + htmlString+"</body></html>");
try {
PdfHtmlElementHandler eh = new PdfHtmlElementHandler();
pdfDiv = new PdfDiv();
XMLWorkerHelper.getInstance().parseXHtml(eh, strReader);
List<Element> htmlList = eh.getHtmlList();
for(int i=0; i<htmlList.size(); i++) {
pdfDiv.addElement(htmlList.get(i));
}
}
PdfPCell cell_2 = new PdfPCell();
PdfPTable table = new PdfPTable(2);
cell_2.addElement(pdfDiv);
table.addCell(cell_2);
Its printing full text.I just replaced p with pdfDiv.But i got another issue.
If the text length is large and is not able to be printed on one page,it should be printed on other page.But its not happening in case of pdfDiv.
Summery:-
With p its printing on two pages but not the full html text.
With pdfDiv its printing html text fully, but if the text is large
,it prints that much text as comes on one pdf page.
Please help.Its really urgent.I am using Itext library for pdf generation.
Issue is resolved by changing the version of itext from 5.3.2 to 5.5.7.

How to make some letters in string BOLD

I'm looking for an easy way to make some letters in a label bold.
I have a string like this:
String r = "y = "+output0+" "+output1+"sin(x) "+output2+"cos(x)";
and a label:
Label s = new Label(r);
and I need to make the "y" and "sin(x), cos(x)" bold. I tried using HTML, but it didn't work (maybe i was using it wrong). If I set font for that label, then the whole label is bold (including those outputs) which is not what I need.
You can use html style in JLabels and in some other Java components. If you start your text with <html> and end it with </html>, the html code in your JLabel will be rendered.
This should resolve your issue:
JLabel myLabel = new JLabel();
// The following line is required to make this JLabel's text not bold as JLabel's text is bold be default.
myLabel.setFont(myLabel.getFont().deriveFont(Font.PLAIN));
myLabel.setText("<html><strong>y</strong> = " + output0 + " " + output1 + "<strong>sin(x)</strong> " + output2 + "<strong>cos(x)</strong></html>");
Try using HTML (you have to start the string with <html> and end it with </html>):
new Jlabel("<html>Normal text. <strong>This is bold.</strong></html>")
Explanation! <html> tells Java to render HTML (Hyper Text Markup Language), and <strong> tells HTML that the text inside is of strong importance, normally represented in bold.

How to edit jasper file?

I am editing .jasper file using report tool. I am changing the alignment for parameter left to centre then i save that file. it generates the "jrxml" file also. in my Java code I can pass the .jasper location to print some items. but my changes not affect and the old design remains same..
Help me , How can I edit and save .jasper???
public static JasperPrint createRefundPrint(Ticket ticket, HashMap map) throws Exception {
final String FILE_RECEIPT_REPORT = "/com/floreantpos/report/template/RefundReceipt.jasper";
TicketDataSource dataSource = new TicketDataSource(ticket);
return createJasperPrint(FILE_RECEIPT_REPORT, map, new JRTableModelDataSource(dataSource));
}
if you use a IReport for editing your .jrxml reports, you should just press the "Preview" button. Then the .jasper report file will be generated automatically in folder that contains the .jrxml file
To edit jasper report, you make your changes in the JRXML file. Once you build or compile your report it will generate a .jasper file which is used in your java code.
you try make dynamic report.
You do not have a location problem when you generate your own report.
Example;
public JasperDesign createDesign() throws JRException {
JasperDesign jasperDesign = new JasperDesign();
jasperDesign.setName("sampleDynamicJasperDesign");
jasperDesign.setPageWidth(595); // page width
jasperDesign.setPageHeight(842); // page height
jasperDesign.setColumnWidth(515); // column width of page
jasperDesign.setColumnSpacing(0);
jasperDesign.setLeftMargin(40);
jasperDesign.setRightMargin(40);
jasperDesign.setTopMargin(20);
jasperDesign.setBottomMargin(20);
JRDesignExpression expression = new JRDesignExpression();
//Set style of page.
JRDesignStyle normalStyle = new JRDesignStyle();
normalStyle.setName("Sans_Normal");
normalStyle.setDefault(true);
normalStyle.setFontName("DejaVu Sans");
normalStyle.setFontSize(12);
normalStyle.setPdfFontName("Helvetica");
normalStyle.setPdfEncoding("Cp1252");
normalStyle.setPdfEmbedded(false);
jasperDesign.addStyle(normalStyle);
/*
* Generate field dynamically
* */
JRDesignField field = new JRDesignField();
field.setName("firstName");
field.setValueClass(String.class);
jasperDesign.addField(field);
field = new JRDesignField();
field.setName("lastName"); // set name for field.
field.setValueClass(String.class); // set class for field. Its always depends upon data type which we want to get in this field.
jasperDesign.addField(field); // Added field in design.
field = new JRDesignField();
field.setName("age");
field.setValueClass(Integer.class);
jasperDesign.addField(field);
JRDesignBand band = new JRDesignBand();
//Title Band
band = new JRDesignBand();
band.setHeight(30);
JRDesignStaticText staticText = new JRDesignStaticText();
staticText.setText("Person's Specification");
staticText.setX(0);
staticText.setY(0);
staticText.setHeight(20);
staticText.setWidth(515);
staticText.setHorizontalAlignment(HorizontalAlignEnum.CENTER);
band.addElement(staticText);
jasperDesign.setTitle(band);
band = new JRDesignBand(); // New band
band.setHeight(20); // Set band height
/*Create text field dynamically*/
JRDesignTextField textField = new JRDesignTextField();
textField.setX(0); // x position of text field.
textField.setY(0); // y position of text field.
textField.setWidth(160); // set width of text field.
textField.setHeight(20); // set height of text field.
JRDesignExpression jrExpression = new JRDesignExpression(); // new instanse of expression. We need create new instance always when need to set expression.
jrExpression.setText(""" + "First Name: " + """ + "+" + "$F{firstName}"); // Added String before field in expression.
textField.setExpression(jrExpression); // set expression value in textfield.
band.addElement(textField); // Added element in textfield.
textField = new JRDesignTextField();
textField.setX(160);
textField.setY(0);
textField.setWidth(160);
textField.setHeight(20);
jrExpression = new JRDesignExpression();
jrExpression.setText("$F{lastName}" + "+" + """ + " :Last Name" + """); // Added string after field value
textField.setExpression(jrExpression);
band.addElement(textField);
textField = new JRDesignTextField();
textField.setX(320);
textField.setY(0);
textField.setWidth(160);
textField.setHeight(20);
jrExpression = new JRDesignExpression();
String age = """ + "<font>" + """ + "+" + """ + "Age is: " + """ + "+" + """ + "</font><font>" + """ + "+" + "$F{age}" + "+" + """ + "</font>" + """; // added html in text field with different color.
jrExpression.setText(age);
textField.setExpression(jrExpression);
textField.setMarkup("html"); // By Default markup is none, We need to set it as html if we set expression as html.
band.addElement(textField);
((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);
return jasperDesign;
}
You can compile a .jrxml in Project Explorer window of Jaspersoft Studio. For that first find your .jrxml report and rightclick on it. In context menu choose 'Compile report'. Then you get compiled .jasper report.

Categories