environment is : flyingsaucer r8 and itext 2.0.8
I'm going to create pdf file with flyingsaucer and itext, I added
table-layout:fixed;word-wrap:break-word;
to wrap the cell content. However, the generated pdf file looks like next :
from the above diagram, we can see that over long English sentence of column 'Description' can be wrapped correctly, but the over long number in 'Account Code' and 'Description' cannot be wrapped.
I also tried "word-break: break-all;", but it still doesn't work.
my xhtml file is :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
#page {-fs-flow-top: 'header';-fs-flow-bottom: 'footer';size:210mm 297mm;margin-top:40pt;#top-center {content: element(header)}
#bottom-center {content: element(footer)}page-break-before:always}#pagenumber:before { content: counter(page); }
#pagecount:before { content: counter(pages); }.pageNext{page-break-before: always;}
#header {position: running(header);font-style: italic;font-family: Arial Unicode MS;
-fs-move-to-flow: 'header';width: 100px; padding-top:10pt;}#footer {position: running(footer);font-style: italic;
font-family: Arial Unicode MS;-fs-move-to-flow: 'footer';color: #6c6ce8;}
body{font-size:13px;font-family:Arial Unicode MS;white-space:inherit;}b{font-size:13px;font-weight:bold;font-family:Arial;}
#dt th{text-align: center;font-family:Arial;font-weight:bold;}#title {font-size:15px;font-weight:bold;}#lab {font-size:15px;font-weight:bold;}
table.data{ border-top: 1px solid #333;border-bottom: 1px solid #333;width:100%;border:1px solid #333;}table{ border-collapse:collapse;}
table td{ padding:0 0 0 0; vertical-align:top;white-space:inherit;}
table.uReportStandard > thead > tr > th{ border:0.5pt #333 solid; background:#d5d0c3;color:#000;text-align:center;font-size:15px;
font-family:Arial,sans-serif;font-weight:bold;}
table.uReportStandard > tbody > tr > td{ padding:1px 1px; font-size:13px;}.data td.left_text{ font-size:13px;
font-family:Arial Unicode MS,sans-serif;width:300px;}.data td.right_text{ text-align:right;font-size:13px;
font-family:Arial Unicode MS,sans-serif;width:120px;}table#uPageCols td#uRightCol,table#uPageCols td#uRightCol aside{width:0;}
table.uReportStandard{border:0.5px #333 solid;}
</style>
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<title></title>
</head>
<body>
<div id='footer' style='text-align:center;margin-top:0;'>
Page <span id='pagenumber' /> of <span id='pagecount' /><span style='margin-left:150px;'>2016-05-23 16:03:07</span>
</div>
<div>
<table border='0' id='dt' style='width:100%;table-layout:fixed;word-wrap:break-word;'>
<thead>
<tr style='background-color: gainsboro;border:solid 1px #333;'>
<th style='border:solid 1px #333;width:10%;'>Account Code</th>
<th style='border:solid 1px #333;width:29%;'>Bank Name</th>
<th style='border:solid 1px #333;width:35%;'>Description</th>
<th style='border:solid 1px #333;width:13%;'>Load(CNY)</th>
<th style='border:solid 1px #333;width:13%;'>Borrow(CNY)</th>
</tr>
</thead>
<tr>
<td style='border:solid 1px #333;'>66020901039</td>
<td style='border:solid 1px #333;'>ABC DEF<br />
-Global Logistics LTD</td>
<td style='border:solid 1px #333;white-space:inherit;'>break-all Behaves the same as normal for Asian text, yet allows the line to break arbitrarily for non-Asian text. This value is suited to Asian text that contains some excerpts of non-Asian text. Debit: EUR 50.00</td>
<td style='text-align: right;border:solid 1px #333;'>367.47</td>
<td style='text-align: right;border:solid 1px #333;'>
</td>
</tr>
<tr>
<td style='border:solid 1px #333;'>220201</td>
<td style='border:solid 1px #333;'>ACCOUNT PAYABLE<br />
-Global Logistics LTD</td>
<td style='border:solid 1px #333;white-space:inherit;'>88888888888888888889999999999999999997777777777777 Credit: EUR 284.36</td>
<td style='text-align: right;border:solid 1px #333;'>
</td>
<td style='text-align: right;border:solid 1px #333;'>2,089.85</td>
</tr>
</table>
</div>
</body>
</html>
my code is :
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new File("test.xhtml"));;
renderer.layout();
FileOutputStream fos = new FileOutputStream("a.pdf");
renderer.createPDF(fos);
fos.close();
my questions are :
how table-layout:fixed;word-wrap:break-word; wrap content? it base on what to wrap?
why English/Chinese centense can be wrapped correctly, while number cannot?
how to wrap the number in my case?
Thanks in advance!
after using itext 5.5.9, number can be wrapped correctly, but now css : -fs-table-paginate: paginate; #pagenumber:before { content: counter(page); }#pagecount:before { content: counter(pages); } doesn't work in itext 5.5.9
as #Lonzak said, I just replaced core-renderer-r8.jar with flying-saucer-core.9.0.9.jar and flying-saucer-pdf.9.0.9.jar, use itext 2.1.7, then it works with css :
table-layout:fixed;word-wrap:break-word
, the long non-blank string or number can be wrapped correctly.
I was breaking my mind with that and i would recommend you to follow this solution.
If you are using thymeleaf try to use the .abbreviate method :
${#strings.abbreviate( ${exampleOfText}, 10)}
source
Related
Alright, I've been making a table that allows the user to enter certain input and acts like excel. Since this has nothing to do with the purpose of the program and more of how it displays things, I won't discuss its purpose in this post. Is there a way to add more decimal points to the demo output? I have tried adding 'total.toFixed(3) That didn't solve the issue. Then I tried what I have inserted now. I am out of ideas.
import java.text.NumberFormat;
private static NumberFormat percent = NumberFormat.getPercentInstance();
var rate = .00125;
var x = myFunction(.04375, rate);
function myFunction(a, b) {
return a + b;
}
var total = x*100;
percent = new DecimalFormat("0.000#%");
document.getElementById("demo").innerHTML = percent.format(total);
<!DOCTYPE>
<style>
th{
font-family: Arial Narrow;
background: #D6D6D6;
overflow: hidden;
font-size: 28px;
}
table, th, td{
border-color: black;
border: 1px solid black;
border-collapse: collapse;
}
.row2 {
font-family: Arial Narrow;
font-size: 28px;
}
td {
</style>
<html>
<head>
<table border="1">
<tr>
<th colspan="9" >FANNIE MAE MANDATORY</th>
</tr>
<tr class="row2">
<td></td>
<td colspan="2">30</td>
<td colspan="2">20</td>
<td colspan="2">15</td>
<td colspan="2">10</td>
</tr>
<tr class="row2">
<td></td>
<td>1 PT</td>
<td>0 PT</td>
<td>1 PT</td>
<td>0 PT</td>
<td>1 PT</td>
<td>0 PT</td>
<td>1 PT</td>
<td>0 PT</td>
</tr>
<tr class="row2">
<td>$35,000 - $99,999</td>
<td><p id="demo"></p></td>
<td id="c7"></td>
<td id="d7"></td>
<td id="e7"></td>
<td id="f7"></td>
<td id="g7"></td>
<td id="h7"></td>
<td id="i7"></td>
</tr>
</table>
Use a floating point with String.format
String.format("%.3f", number)
Try using string format by mentioning the type of decimals you need such as #xxx.xx it restricts the decimal of 2 based on rounding strategy
<div class="dataTables_scrollBody" style="position: relative; overflow: auto; height: 370px; width: 100%; min-height: 0px;">
<table id="offer_create" class="display article-list ranklist_drag table table-bordered dataTable no-footer" aria-describedby="offer_create_info" role="grid" style="position: absolute; top: 0px; left: 0px; width: 100%;" width="100%" cellspacing="0">
<thead>
<tbody>
<tr class="odd" role="row">
<td>test1</td>
<td>test2</td>
</tr>
How to get XPath and its in data table to be clickable at once?
Check for image data table looks like this:
WebElement htmltable=driver.findElement(By.xpath("//*[#id='main']/table[1]/tbody"));
List<WebElement> rows=htmltable.findElements(By.tagName("tr"));
for(int rnum=0;rnum<rows.size();rnum++)
{
List<WebElement> columns=rows.get(rnum).findElements(By.tagName("th"));
System.out.println("Number of columns:"+columns.size());
for(int cnum=0;cnum<columns.size();cnum++)
{
System.out.println(columns.get(cnum).getText());
}
}
Note: <tr> represent your row name and <th> represent your column name here.
I am trying to read a HTML file and add link to some of the texts :
for example :
I want to add link to "Campaign0" text. :
<td><p style="overflow: hidden; text-indent: 0px; "><span style="font-family: SansSerif;">101</span></p></td>
<td><p style="overflow: hidden; text-indent: 0px; "><span style="font-family: SansSerif;">Campaign0</span>
<td><p style="overflow: hidden; text-indent: 0px; "><span style="font-family: SansSerif;">unknown</span></p></td>
Link to be added:
<a href="Second.html">
I need a JAVA program that modify html to add hyperlink over "Campaign0" .
How i do this with Jsoup ?
I tried this with JSoup :
File input = new File("D://First.html");
Document doc = Jsoup.parse(input, "UTF-8", "");
Element span = doc.select("span").first(); <-- this is only for first span tag :(
span.wrap("");
Is this correct ?? It's not working :(
In short : is there anything like-->
if find <span>Campaign0</span>
then replace by <span>Campaign0</span>
using JSoup or any technology inside JAVA code??
Your code seems pretty much correct. To find the span elements with "Campaign0", "Campaign1", etc., you can use the JSoup selector "span:containsOwn(Campaign0)". See additional documentation for JSoup selectors at jsoup.org.
After finding the elements and wrapping them with the link, calling doc.html() should return the modified HTML code. Here's a working sample:
input.html:
<table>
<tr>
<td><p><span>101</span></p></td>
<td><p><span>Campaign0</span></p></td>
<td><p><span>unknown</span></p></td>
</tr>
<tr>
<td><p><span>101</span></p></td>
<td><p><span>Campaign1</span></p></td>
<td><p><span>unknown</span></p></td>
</tr>
</table>
Code:
File input = new File("input.html");
Document doc = Jsoup.parse(input, "UTF-8", "");
Element span = doc.select("span:containsOwn(Campaign0)").first();
span.wrap("");
span = doc.select("span:containsOwn(Campaign1)").first();
span.wrap("");
String html = doc.html();
BufferedWriter htmlWriter =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output.html"), "UTF-8"));
htmlWriter.write(html);
htmlWriter.close();
output:
<html>
<head></head>
<body>
<table>
<tbody>
<tr>
<td><p><span>101</span></p></td>
<td><p><span>Campaign0</span></p></td>
<td><p><span>unknown</span></p></td>
</tr>
<tr>
<td><p><span>101</span></p></td>
<td><p><span>Campaign1</span></p></td>
<td><p><span>unknown</span></p></td>
</tr>
</tbody>
</table>
</body>
</html>
I need to convert ByteArrayOutputStream to String but I can't figure out the encoding. Please help ? I tried Using ICUJ library but It only works for input stream. A conversion from byte array to input stream is also fine.
Here's a sample of what I'm getting using the default encoding. Clearly the new lines are not supposed be there.
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n
<html>
\n
<head>
\n
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
\n
<style type=\"text/css\">\n .style_0 { font-family: sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; color: black; text-indent: 0em; letter-spacing: normal; word-spacing: normal; text-transform: none; white-space: normal; line-height: normal;}\n .style_1 { height: 5.062in; width: 8.01in;}\n </style>
\n <script type=\"text/javascript\">\n //<![CDATA[\n function redirect(target, url){\n if (target =='_blank'){\n open(url);\n }\n else if (target == '_top'){\n window.top.location.href=url;\n }\n else if (target == '_parent'){\n location.href=url;\n }\n else if (target == '_self'){\n location.href =url;\n }\n else{\n open(url);\n }\n }\n //]]>\n </script>\n
</head>
\n <body class=\"style_0\" style=\" margin:0px;\">\n <table cellpadding=\"0\" style=\"empty-cells: show; border-collapse:collapse; width:8in; overflow: hidden; table-layout:fixed;\">\n
<col>
</col>\n
<tr>
\n
<td></td>
\n
</tr>
\n
<tr>
\n
<td valign=\"top\"></td>
\n
</tr>
\n
<tr>
\n
<td>
\n
<div style=\"overflow:hidden; height:0.5in\">\n <div style=\" overflow:hidden;\">Dec 23, 2013, 7:11 PM</div>
\n </div>\n
</td>
\n
</tr>
\n </table>\n
<hr style=\"color:red\"/>
\n
<div style=\"color:red\">
\n
<div>The following items have errors:\n </div>
\n <br>\n
<div>
\n
<div id=\"error_title\" style=\"text-decoration:underline\">
Chart (id = 12):
\n
I tried Using ICUJ library but It only works for input stream.
You can get the byte array from the ByteArrayOutputStream, and then wrap it in a ByteArrayInputStream ... and pass that to the ICUJ method.
(Bear in mind that there is a chance that ICUJ will intuit the wrong encoding. Or that the bytes might not represent text in any known encoding.)
It won't much help but java.nio.charset.CharsetDecoder has a detectedCharset() method to auto identify charset of character encoded bytes. But unfortunately current impl of CharSetDecoder in Java SE7 (the one that is received by calling the method Charset.newDecoder()) is not an auto detecting charset decoder so calling detectedCharSet() method throws UnsupportedOperationException.
I want to select a row and delete it,but id of row is dynamically generated so how can i access it using java in selenium webdriver. when i will add a row by using the application a new tr tag will be added with different id.so how can i select the row.Below is the html code for a table that has two rows with different id's.
<table id="SlotTable" class="noborder" cellspacing="0" cellpadding="0" align="left"
paging="false" style="border-top: 0px none; table-layout: fixed; width: 984px;">
<tbody id="tableBody">
<script>
<tr id="97.115.104.105.115.104" style="background-color: rgb(221, 221, 221);">
<td width="254px" style="text-overflow: ellipsis; overflow: hidden; white-space: nowrap; width: 254px;">
<td width="110px" style="text-overflow: ellipsis; overflow: hidden; white-space: nowrap; width: 110px;">
<td width="60px" style="text-align: right; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; width: 60px;">
<td width="170px" style="text-align: right; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; width: 170px;">
<td width="100px" style="text-align: right; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; width: 100px;">
<td width="120px" style="text-overflow: ellipsis; overflow: hidden; white-space: nowrap; width: 120px;">
<td width="170 px" style="text-align: right; text-overflow: ellipsis; overflow: hidden; white-space: nowrap;">
</tr>
<tr id="107.117.109.97.114" style="background-color: rgb(232, 232, 232);">
</tbody>
</table>
</div>
</td>
</tr>
<tr>
</tbody>
</table>
You can select the rows for example by using CSS-selectors like :
#SlotTable > tr:nth-child(1)
#SlotTable > tr:nth-child(2)
The style of the elements seem to be the same, so you could use them as identifier and select all elements with that particular style and then do your actions on those.
Try to get row using xpath:
//tbody[#id='tableBody']//tr[1]
//tbody[#id='tableBody']/script/tr[1]
Change number to access the row you need.
Put this method you will get list of all the ids of rows and you can perform all the activity after that using their id's :
public ArrayList<String> ListOfIdsOfRows()
{
WebElement table =driver.findElement(By.id("SlotTable"));
WebElement tbody=table.findElement(By.tagName("tbody"));
List<WebElement> rows=tbody.findElements(By.tagName("tr"));
ArrayList<String> ListOdIds=new ArrayList<>();
for(int i=0;i<rows.size();i++)
{
WebElement row = tbody.findElement(By.xpath("//table[#id='SlotTable']/tbody/tr["+(i+1)+"]"));
String rowId=row.getAttribute("id");
ListOdIds.add(rowId);
}
return ListOdIds;
}