Different headers when printing - java

I need to export multiple HTML files into a single PDF one.
I'm using #page rules to apply a header.
I need to :
Display the pagecount ( page n out of total)
Display a different title in the header for each original HTML document
What I tried :
Exporting each HTML separately:
I get the good title but the pagecount is relative to the current HTML, not the global total.
Using jsoup to gather every HTML in one document before exporting:
I get the good pagecount but can't select which title to apply in the print header
I know about #page:first and #page:last but would like to be able to apply different styles for every page in between.

I found a solution !
You can specify some content to be on a named page :
#page A { #top-left { content:"..." } }
div-A { page: A; }
<div-A>
Content that has a specific header
</div-A>
Don't forget to apply some rules like "page-break-after: always"

Related

Link between different Sections in same PDF

We are creating a PDF document consisting of different sections generated separately and then added in single document. Now we need to provide a hyperlink to navigate the user to a point in different section.
We are using iText 7.0.5 and have tried using Link and Link Annotations, but no use. The mapping works for the same section but not between different sections.
Your use case: Merge pdfs and provide a mean of navigating in the resultant document.
How to do it?: Please look at the next iText sample: https://github.com/itext/i7js-examples/blob/develop/src/test/java/com/itextpdf/samples/sandbox/merge/MergeWithToc.java
In that sample some pdfs are merged and then a table of contents is created, so that one can click on its items and be navigated to the appropriate page.
In order to do so, it's suggested to set a named destination on some of elements of your "sections:
// Put the destination at the very first page of each merged document
if (i == 1) {
text.setDestination("p" + pageNo);
}
doc.add(new Paragraph(text).setFixedPosition(pageNo, 549, 810, 40).setMargin(0).setMultipliedLeading(1));
and then to set an action on an appropriate TOC element:
p.setAction(PdfAction.createGoTo("p" + entry.getKey()));
Perhaphs the same logic can be applied in your case as well.

JavaFx WebEngine - Overwriting a website's stylesheet with (local) files

I'd like to customise the appearance of a website that I am loading, so I created a little test.css file that does nothing but changing the look of all table rows:
tr {
height: 22px;
background-image: url("test.png");
}
How do I get he WebEngine to load this file and replace the page's own CSS rules with mine?
Also, i'd like to be able to load page-specific css files and not one huge file for all pages.
I found this page, but it only shows how to run through the DOM and assign a new style to the desired elements by hand. This is, of course, not what I want. Instead, I'd like the browser to use my files as 'user defaults'.
Thx for any help :)
First of I have to state, that I hope you know what you are doing, as these things can seriously damage a web site.
So here is what you can do:
You grab the Document from the WebEngine, retrieve the head element and add a style child element to it, containing the src location of the stylesheet you want to add.
Document doc = webView.getEngine().getDocument();
URL scriptUrl = getClass().getResource(pathToAttachedDocument); // pathToAttachedDocument = CSS file you want to attach
String content = IOUtils.toString(scriptUrl); // Use Apache IO commons for conveniance
Element appendContent = doc.createElement("style");
appendContent.appendChild(doc.createTextNode(content));
doc.getElementsByTagName("head").item(0).appendChild(appendContent);
By the way, JavaScript can be added in a similar way, it's just 'script' instead of 'style'
I would do like this to ADD or REPLACE any rules :
String css = getFileAsString("style.css");
Document doc = webEngine.getDocument();
Element e = doc.getElementById("my_style");
e.setTextContent(css);
... given a
<style id="my_style"></style>
tag in the HTML document.
setUserStyleSheetLocation()was designed for that very purpose: to let the user of the web page, style it as they want.
Usage:
webEngine.setUserStyleSheetLocation(styleSheetURL.toString());

jmesa renders the html as text

I'm using jmesa in Java directly using the tableModel.render() to get the HTML directly. Some of my web objects in my result lists contain HTML - example:
class blah {
String email;
public String getEmailLink() {
return "<a href='" + email + "</a>"
}
}
In my Java code I would just do this:
htmlRow.addColumn(new HtmlColumn("emailLink"));
jmesa is rendering this as text. How can I tell jmesa to render the text as-is to be html in the document?
TIA
Looking at the JMesa soure code, HtmlCellEditor automatically escapes HTML.
I haven't tested it, but you should be able to override the default HtmlCellEditor with a different type... such as the bare-bones BasicCellEditor. It shouldn't be too much extra code:
HtmlColumn emailLinkColumn = new HtmlColumn("emailLink");
emailLinkColumn.setCellEditor(new BasicCellEditor());
htmlRow.addColumn(emailLinkColumn);
Another option to all of this is to create a custom CellEditor and have it create your <a> tag for you instead of doing it in your bean. This page should get you started with custom CellEditors if you want to go that route.
BTW, if you are messing with just a value inside of a cell, overriding/replacing CellEditor is probably all you need (CellEditor it is analogous to the body of a <td>). CellRenderer is concerned with the entire cell (analogous to the <td> as well as its contents).
Use a HtmlCellRenderer as shown in this tutorial.

How to jump to specific page number using display tag in Spring framework

I want to jump to a specific page number using display tag with a textbox and a "go" button.
On the click of GO button calls a javascript in which it should go to that specific page through that .htm which is not happening.
please suggest an argument for this particular way of getting a specific page or else alternate suggestions are always welcome
Below are the arguments in displaytag.properties which i know so far
enter code here
{0}: numbered pages list
{1}: link to the first page
{2}: link to the previous page
{3}: link to the next page
{4}: link to the last page
{5}: current page
{6}: total number of pages
Below is the javascript function which is being called on click of GO button
function selectPage(){
alert("pageNo:" +document.portalDisplayform.selPageNo[0].value);
alert("pageNo:" +document.portalDisplayform.selPageNo[1].value);
var pageNo = document.portalDisplayform.selPageNo[0].value;
var pageNo = document.portalDisplayform.selPageNo[1].value;
document.portalDisplayform.action = '<%=request.getContextPath()% >'+"/portalAccessdisplay.htm?tokenId="+'<%=cachetoken%>'+pageNo;
document.portalDisplayform.submit();
}
It's not so easy, because the displaytag encodes parameters (to avoid naming conflicts with functional parameters), and uses a unique ID per table, in case several tables are on the same page. I suggest you download the source code of displaytag, have a look at the ParamEncoder and Pagination classes (and their callers) to discover how a link to a specific page is constructed by the displaytag. You'll have to use similar code to generate the URL, and you'll have to modify the value of the appropriate (encoded) parameter in your JavaScript code.
The very easiest way, you can modify the TableTag.java which is in the display-tag jar file. In this file modify the initParameters() method. Inside the method
place the below 4 line code.
After this line which is in initParameters() method.
this.pageNumber = (pageNumberParameter == null) ? 1 : pageNumberParameter.intValue();
Place the below code
if((request.getParameter("pageno") != null) && (request.getParameter("pageno") != ""))
{
this.pageNumber=Integer.parseInt(request.getParameter("pageno"))
}
And use a TextBox with the name pageno in your DisplayTag page. Also include the name in your DisplayTag property excludedparam.

How to create TOC or index using the Flying Saucer project?

I convert HTML files to PDF format using The Flying Saucer Project. This are documents containing repetitive information - premises and their addresses, let's call them elements. At the end of a document I need to create an index. Each index entry should have a page number referring to the page where element was added. The number of elements that can fit on one page will vary.
How can I create a document index? Or how can I get notified while library adds certain type of HTML element to the PDF document?
Try this:
In CSS
ol.toc a::after { content: leader('.') target-counter(attr(href), page);}
In HTML
<h1>Table of Contents</h1>
<ol class='toc'>
<li>Loomings</li>
<li>The Carpet-Bag</li>
<li>The Spouter-Inn</li>
</ol>
<div id="chapter1">Loomings</div>
I found possible answer. You have to start playing with org.xhtmlrenderer.render.BlockBox class. A method public void layout(LayoutContext c, int contentStart) is used to place properly any HTML element in the PDF document. This method iterates through an element a few times. After the last iteration a valid page number is set.
If you mark an element you want to index, by for example using a class attribute, then you can get a page number using following code:
String cssClass = getElement().getAttribute("class");
if(!cssClass.equals("index")) {
int pageNumber = c.getRootLayer().getPages().size();
/* ... */
}

Categories