Cell Comments created with Apache POI have transparent background - java

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

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

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

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;

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)

Multiline text in Excel cells

I'm trying to write multiline text to excel cells.
cell.setCellValue("line1 \n line2");
But when I open the document, I see only one line until I double-click it for editing, then it becomes two-lined. Why is it so? Thanks
You need to set the row height to accomodate two lines of text.
row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
You need to set the wrap text = true to get the new line.
Try this :
Here wb is the Workbook.
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
I found that you have to now include a third step after you follow the accepted answer to this question. You have to auto-size the column after you've added all of your data.
Assuming you're concerned about column 2,
sheet.autoSizeColumn(2);
See this example from Apache for context. It works for me with Java 8, poi-3.15.jar, and Excel for Mac.
This worked for me
cell.setCellValue("line1 \n\r line2");

How to: JTable look and feel like Windows 7 Details View or Vuze table

In Windows 7 if we set the content of a Folder in Details view, then it turns into a table like structure, in which if we hover a row it renders a rectangular shape with light blue color and slightly curvy corner on that row and if we select a row a similar shape with blue color set on that row. This similar effect is shown by Vuze's table.
Is there any way to achieve this table rendering for JTable? If so what is the way to get it?
Any information will be helpful to me.
Thanks in advance.
The highlighted portions in the following image shows what I intended to achieve. The first highlight is the selected row and the second one is hovered.
you can do that by implement Substance Custom Look and Feel for JTreeTable,
plugin for SwingX
TreeTable by aepryh (best and open code for TreeTable)
notice you have to change XxxRenderer to SubstanceXxxRenderer (works on Xp / Win7 / 2008R2)
There's nothing built-in to achieve this. You can achieve the hover effect by using custom cell renderers and mouseover listeners. This answer gives you an overall picture of what to do.
As for the rectangular effect, again - custom cell renderers only, with either images, or drawRoundRect
I just noticed this post. JIDE has a component that does what you need. It is in paid JIDE Grids product. You can find a screenshot at http://www.jidesoft.com/images/navigation-components.png. Of course you can do this by yourself by overriding the paintComponent of a regular JTable. Using cell renderer approach won't work as the rollover effect needs to span the whole row.

Categories