What is the equivalent of getWidthPoint() in itextpdf7? - java

Upgraded to itextpdf7 and I am using:
new Text(" TEST ").setFont(testFont).setFontSize(FONT).setBold().getStrokeWidth();
but it always returns null.
With the previous version of itext I was using:
new Chunk(" TEST ", FONT_BOLD).getWidthPoint();
Is there another way in ItextPdf7 to get the width of a text?

Related

How do I iterate over an entire document in OpenOffice/LibreOffice with UNO

I am writing java code to access a document open in Libre Office.
I now need to write some code which iterate over the entire document, hopefully in the same order it is shown in the editor.
I can use this code to iterate over all the normal text:
XComponent writerComponent=xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps);
XTextDocument mxDoc=UnoRuntime.queryInterface(XTextDocument.class, writerComponent);
XText mxDocText=mxDoc.getText();
XEnumerationAccess xParaAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, mxDocText);
XEnumeration xParaEnum = xParaAccess.createEnumeration();
Object element = xParaEnum.nextElement();
while (xParaEnum.hasMoreElements()) {
XEnumerationAccess inlineAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, element);
XEnumeration inline = inlineAccess.createEnumeration();
// And I can then iterate over this inline element and get all the text and formatting.
}
But the problem is that this does not include any chart objects.
I can then use
XDrawPagesSupplier drawSupplier=UnoRuntime.queryInterface(XDrawPagesSupplier.class, writerComponent);
XDrawPages pages=drawSupplier.getDrawPages();
XDrawPage drawPage=UnoRuntime.queryInterface(XDrawPage.class,page);
for(int j=0;j!=drawPage.getCount();j++) {
Object sub=drawPage.getByIndex(j);
XShape subShape=UnoRuntime.queryInterface(XShape.class,sub);
// Now I got my subShape, but how do I know its position, relative to the text.
}
And this gives me all charts (And other figures I guess), but the problem is: How do I find out where these charts are positioned in relation to the text in the model. And how do I get a cursor which represent each chart?
Update:
I am now looking for an anchor for my XShape, but XShape don't have a getAnchor() method.
But If I use
XPropertySet prop=UnoRuntime.queryInterface(XPropertySet.class,shape);
I get the prop class.
And I call prop.getPropertyValue("AnchorType") which gives me an ancher type of TextContentAnchorType.AS_CHARACTER
but I just can't get the anchor itself. There are no anchor or textrange property.
btw: I tried looking into installing "MRI" for libre office, but the only version I could find hav libreoffice 3.3 as supported version, and it would not install on version 7.1
----- Update 2 -----
I managed to make it work. It turns out that my XShape also implements XTextContent (Thank you MRI), so all I had to do was:
XTextContent subContent=UnoRuntime.queryInterface(XTextContent.class,subShape);
XTextRange anchor=subContent.getAnchor();
XTextCursor cursor = anchor.getText().createTextCursorByRange(anchor.getStart());
cursor.goRight((short)50,true);
System.out.println("String=" + cursor.getString());
This gives me a cursor which point to the paragraph, which I can then move forward/backward to find out where the shape is. So this println call will print the 50 chars following the XShape.
How do I find out where these charts are positioned in relation to the text in the model. And how do I get a cursor which represent each chart?
Abridged comments
Anchors pin objects to a specific location. Does the shape have a method getAnchor() or property AnchorType? I would use an introspection tool such as MRI to determine this. Download MRI 1.3.4 from https://github.com/hanya/MRI/releases.
As far as a cursor, maybe it is similar to tables:
oText = oTable.getAnchor().getText()
oCurs = oText.createTextCursor()
Code solution given by OP
XTextContent subContent=UnoRuntime.queryInterface(XTextContent.class,subShape);
XTextRange anchor=subContent.getAnchor();
XTextCursor cursor = anchor.getText().createTextCursorByRange(anchor.getStart());
cursor.goRight((short)50,true);
System.out.println("String=" + cursor.getString());

JxBrowser How to get value from a html node within java

Hello guys I am trying out the jxBrowser component and I am unable to the value of selected html component...
List<DOMElement> paragraphs = divRoot.findElements(By.cssSelector("p"));
for (DOMElement paragraph : paragraphs) {
System.out.println("paragraph.getNodeValue() = " +
paragraph.getNodeValue());
}
I am able to find paragraphs.. But can't get their node's value.. or simply <p>I cant get this value<p/> The code must be okay because its just a pure copy of their own sample code: here
So my question is... What have I done wrong? It seems properly imported.. I am using library version 6.19.1 on a macbook. ( And I even tried it on a windows 10 with same result.. )
Or if there is other java browser solution with similar functions.. What I need is to load a page, get some values out of some divs and then simulate click.
DOMElement.getNodeValue() returns the value of this node, depending on its DOMNodeType. The text you are trying to get is a children node for the node, so you need to get it with the following code paragraph.getChildren().get(0).
So, the final code will look like the following:
for (DOMElement paragraph : paragraphs) {
System.out.println("paragraph.getNodeValue() = " +
paragraph.getChildren().get(0).getNodeValue());
}

How to use FunctionScoreQueryBuilder in ES 6 java api?

I want to upgrade from ES 1.7 to 6.0. I made all necessary changes and now I have a problem with FunctionScoreQueryBuilder.
I create a BoolQueryBuilder filter and works fine. Now I want to add score to my results but results are same with those I have without add ScoreFunction
ScoreFunctionBuilder fb = ScoreFunctionBuilders.scriptFunction("_score * (doc['field'].value!=0? 50000:1) ")
FunctionScoreQueryBuilder fsb = new FunctionScoreQueryBuilder(filter,fb)
SearchRequestBuilder srbPaged = client.prepareSearch(indexName)
.setFetchSource(includes, excludes)
.setQuery(fsb)
The code you posted just change the value of field[score],
you should sort results by score.
Just like
srbPaged.sort("_score", SortOrder.DESC)

Exception when using POI FontFormatting setFontColor

Trying to use POI conditional formatting. I have the following method:
private final static Color PEAK_ORANGE = new Color(255, 239, 221);
public ConditionalFormattingRule getConditionalFormatting(ConditionalFormattingRule formattingContainer, FormatSpecs format){
FontFormatting fontFmt = formattingContainer.createFontFormatting();
fontFmt.setFontStyle(true, false);
// fontFmt.setFontColorIndex((short)11);
fontFmt.setFontColor(new XSSFColor(PEAK_ORANGE));
PatternFormatting patternFmt = formattingContainer.createPatternFormatting();
patternFmt.setFillBackgroundColor(new XSSFColor(PEAK_ORANGE));
return formattingContainer;
}
When I use the setFontColor() method I get index out of bounds exception.
When I use the setFontColorIndex() method using some arbitrary index value, I do not get the exception. Notice however that I use the exact same color reference in the call to set the background color
patternFmt.setFillBackgroundColor(new XSSFColor(PEAK_ORANGE));
and this works fine, no exceptions.
Has anyone else run into this? Am I missing something in the call to set the font color? I prefer to use my colors rather than those from the IndexedColors class.
This looks like a small bug in Apache POI, a simple workaround is to first set an index-color and then set the actual intended full color, i.e.
fontFmt.setFontColorIndex((short)1);
fontFmt.setFontColor(new XSSFColor(PEAK_ORANGE));
The setFontColorIndex() will initialize the internal structures so that setFontColor() will work.
FYI, the bug should be fixed in time for release 4.0 of Apache POI.

How to generate QRCode of version 4 (33x33) using any of the libraries?

I need to generate QR Code of version-4. The current library I use is ZXing's QRCodeWriter which gives me QRCode of version-1 I suppose, if not please correct me.
Thanks in advance.
I had the same problem--needed to generate a QR Code of version-4 (33 by 33 pixels), at the request of my company.
I'm working in C#, but I believe the idea is the same. When setting your QrCodeEncodingOptions, you can specify the version:
QrCodeEncodingOptions options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = 100,
Height = 100,
QrVersion = 4
};
Note** The lower the version number, the less data it will be able to contain. For example, if you set the version to 3 or 4, then try to write a bunch of data into the QR code, an error will be thrown.

Categories

Resources