How to change text direction in excel cell using java poi? - java

I want to change Text_Direction (not Alignment) of a single cell (not worksheet) on an excel file using java Apache_poi library.
how can I do it? thanks.

after looking a lot I found another way that will fix the problem.
As explained here , it's just needed to change the direction of the text that will be placed in the cell.
the solution will be like below for right to left:
String rtl = "\u200F" + otherTexts;
and like this for LTR:
String left2right_text = "\u200E" + otherTexts;

Related

Adding Hyperlink to text inside Boxable Cell - PDFBox

Hope all are good. I am relatively new to Boxable and PDFBox and was hoping someone could help me in this regard. I am trying to generate a PDF using PDFBox and since I was required to create tables which I did, now I need to put a hyperlink text in it. I am trying but unable to find any help on this. I know PDFBox has a way of doing it using 'PDAnnotationLink' but that is not working when I'm trying to insert it into a Boxable Cell since that requires only String values.
Bit of my code is something like this,
Row<PDPage> row = table.createRow(rowHeight);
Cell<PDPage> cell = otherDocumentsRow.createCell(30, "Other Documents");
String str = "Attachment link here";
cell = row.createCell(70, str);
Now instead of str text, I am looking to insert a hyperlink text which points to some location (www.example.com). Is this possible or is there a workaround around this.
Thank you in advance and looking forward to hearing from you

Add a Border and change Font Size - docx4j

I'm trying to create a document in java
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
wordPackage.getMainDocumentPart().addStyledParagraphOfText("Title","User comments");
for(
... adding comments with a line between
)
SaveToZipFile saver = new SaveToZipFile(wordPackage);
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.setHeader("content-disposition","filename=My_file.doc");
saver.save(response.getOutputStream());
To add comments with the line, I'm adding a border to my content following the setup here: http://www.docx4java.org/forums/docx-java-f6/tip-constructing-complex-objects-t7.html
With regards to the border.. It doesn't show up properly unless it contains both the addParagraph lines, which means I have extra white space, I'm trying to fix (even out) this by adjusting the font size of my empty addParagraph (with the border) to 2 (or 0) so that there is equal space above and below. The CTBorder.setSz() doesn't seem to be doing anything and neither does Spacing.setAfter() / Spacing.setBefore()
The point I'm at is trying to figure out how to create or edit a style to apply a font size to the empty paragraph, which I can't figure out... However I'm also open to other suggestions to solve my problem.
Instead of using addStyledParagraphOfText, I'd suggest you upload a docx sample containing what you want to the docx4j code generator, then copy/paste the code for the paragraph you want.
Then just add it: wordPackage.getMainDocumentPart().getContent().add(yourp)

Cell Comments created with Apache POI have transparent background

I am using Apache POI 3.9 to create cell comments.
I have been using the code for HSSF sheets suggested by Erik Pragt in creating cell comments using HSSFClientAnchor in apache poi for a few years, and it works well.
However now I have a need to add cell comments to XSSF sheets.
I have tried the code suggested by lastnitescurry in the same page, which works nicely, but it creates for me comments with a transparent background.
The code is reproduced below.
protected void setCellComment(Cell cell, String message) {
Drawing drawing = cell.getSheet().createDrawingPatriarch();
CreationHelper factory = cell.getSheet().getWorkbook()
.getCreationHelper();
// When the comment box is visible, have it show in a 1x3 space
ClientAnchor anchor = factory.createClientAnchor();
anchor.setCol1(cell.getColumnIndex());
anchor.setCol2(cell.getColumnIndex() + 1);
anchor.setRow1(cell.getRowIndex());
anchor.setRow2(cell.getRowIndex() + 1);
anchor.setDx1(100);
anchor.setDx2(100);
anchor.setDy1(100);
anchor.setDy2(100);
// Create the comment and set the text+author
Comment comment = drawing.createCellComment(anchor);
RichTextString str = factory.createRichTextString(message);
comment.setString(str);
comment.setAuthor("Apache POI");
// Assign the comment to the cell
cell.setCellComment(comment);
}
How can I change the background to a yellow background?
NOTE: If one edits the apache-poi created comment in Excel, then it will appear with a yellow background temporarily. However if one tries to format this comment to change the background from within Excel, then one can't. (The Color and Lines menu does not appear)
The answer is that my Java was manipulating an Excel .xlsm file that had "Show All Comments" set to true. Once I changed this setting, the java created the comments correctly.
I managed to reproduce the described behavior in a sheet that contained a Button Form Control.
I deleted the button and the comments are showing correctly.
As an alternative to a button, I used this guide to run a Macro from a cell
https://www.extendoffice.com/documents/excel/4354-excel-click-on-cell-to-run-macro.html

Creating a Java file with right-aligned strings

I am trying to write Persian text to a .txt file in Java. Because Persian (Farsi) is read from right to left, how can I write each line such that it is right aligned?
I am currently using Apache's FileUtils.writeLines(), but I am open to other alternatives in order to achieve the problem.
Thanks!
Text alignment is determined by UI that would show your text file. If you are using a plain text file, so it does not have facilities to tell it its text alignment.
If you insist on it, there are special Unicode characters that can tell UI it must be interpreted as right-to-left text. Please see here.
You can wrap each line into a String.format
String.format("%s", formatter.format(i))
or
Apache StringUtils has several methods: leftPad, rightPad, center and repeat.
Read following thread.
How can I pad a String in Java?
You just add spaces if you want to have lines with specific size, otherwise it depends on the tool you use for reading it.

Alignment in iText library for java

I just got a question about alignment in the library iText. Say if i was to do a quote program.
For example
On the Left side of the page I would want to put Quote # 01 and on the same line on the other side of the page I want to put the date.
Is this possible using the Paragraph class? If not can any body help me out with any other solutions?
To the best of my knowledge, you can't do this within a single paragraph. You'll have to get the current position (cb.getYLine()) and place a separate chunk with the date. See itext positioning text absolutely if you need pointers on placing the chunk.

Categories