Add a Border and change Font Size - docx4j - java

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)

Related

How to (vertically) align text of PDTextField in PDFBox?

I have writen a form with LibreOffice that includes some named fields to fill out via PdfBox. The fields are properly set up with text-alignment (all left-centered) but PdfBox simply ignores the vertical aligment. Both on text fields and on labels. The fields get aligned bottom and the labels top. Besides the optical difference in presentation, the values of the fields also get cut on lower letters like "g" etc.
I would provide a image but i don`t have enough reputation...
Provided that I cannot be the only one with this problem, I`ve found this answer here on SO How to (horizontally) align text of PDTextField in PDFBox?.
There is a function mentioned that provides this function for horizontal alignment
textBox.setQ(PDTextField.QUADDING_CENTERED);
I`ve tested this and it works.
The overall call in my programm is rather simple:
PDDocumentCatalog docCatalog = _pdf.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDTextField field = (PDTextField) acroForm.getField(name);
if (field != null)
{
field.setQ(PDTextField.QUADDING_LEFT);
field.setValue(value);
}
else
{
LOGGER.log(Level.ERROR, "No field found with name:" + name);
}
Unfortunately (or out of sheer stupidity on my end; I blame the temperatures) I didn`t find any way to force PdfBox to any aligment on the vertical axis.
Any help would be much appreciated. Thanks.
EDIT:
As per request here is an example (not the real version because it is a document from work - nonetheless it is the same construction as the original):
https://drive.google.com/file/d/1JKyuFqkb9B8Q9M61uxncAJXzMOo7zvJ5/view?usp=sharing
The field is named err_desc.
Also I provides in the pdf doc a picture of the result in the original filled form.
I've also tried to simply make the vertical space of the text field bigger. That unfortunately didn't work as the text seems to stick to the bottom and simply left more space above the text.

Vaadin 10 Grid add custom image icon

I am using a Vaadin 10 (Flow) Grid to stream data to new rows, and I am trying to add a custom image icon to my TemplateRenderer.
I have a TemplateRenderer setup like this:
TemplateRenderer<TradeUni> sizee = TemplateRenderer.<TradeUni>
of("<div class$=\"[[item.class]]\">[[item.size]]</div>")
.withProperty("class", cssClassProvider)
.withProperty("size", TradeUni::getSize);
Which displays my numbers correctly in the grid cell like this:
I would like to have my image rendered inside the same cell, to the left of the numbers.
This was my (very crude) attempt to import it using html:
TemplateRenderer<TradeUni> sizee = TemplateRenderer.<TradeUni>
of("<div class$=\"[[item.class]]\"><img src=\"i.imgur.com/3LQBglR.png\">[[item.size]]</div>")
.withProperty("class", cssClassProvider)
.withProperty("size", TradeUni::getSize);
Which give's me this:
I think that I might have to wrap the image and numbers into a HorizontalLayout with the image being a separate component - I think I could handle that, but I am unsure of how to do the actual rendering of the image. I can probably accomplish it simply with the internal Vaadin Icons set, but I need to use my own little images.. all icons that I am going to use will be at or less than 25 x 25px.
Thank you so much in advance!
I think you're on the right track, but there's one small detail that causes you trouble. The URL i.imgur.com/3LQBglR.png doesn't define a protocol, which means that the entire string will be treated as a path relative to the location of the hosting page. It will thus try to find a file in a directory named i.imgur.com on your own server.
To fix this, you need to also include the protocol in the image URL, i.e. https://i.imgur.com/3LQBglR.png.
I can also offer a small suggestion for how to make the code easier to read. HTML also supports using ' for enclosing attribute values. This is more convenient to use from Java string since you don't need to use \ to escape ' characters. Your template string could thus be "<div class$='[[item.class]]'><img src='https://i.imgur.com/3LQBglR.png'>[[item.size]]</div>".

Java iText - Get Font size and family of a text selected from a pdf file

I'm writing to inquire a bout a problem when I'm using iText library to extract text contents from PDF file.I would able to extract all the text, but couldn't find the method to extract font styles.
First you need to read the answer to this question: how can i get text formatting with iTextSharp
In this question, you'll discover that the TextRenderInfo has a getFont() method that allows you to get the PostScriptFontName. If you are in luck, this PostScriptFontName will give you information about the style.
Note that this won't always work. Please read the answer to this question: What are the ways of checking if piece of text in PDF documernt is bold using iTextSharp
That question shows an example of a font that doesn't reveal anything about its style.
To get the fontsize of a font in itext, use this code:
renderInfo.getAscentLine().getStartPoint().get(1)-
renderInfo.getDescentLine().getStartPoint().get(1)
This will give you the exact fontsize.

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

PDF find out if text is underlined or a table cell

I have been playing around with PdfBox and PDFTextStripperByArea method.
I was able to extract information if the text is bold or italic, but I'm unable to get the underline information.
As far as I understand it in PDF, underline is done by drawing lines. So in theory I should be able to get some sort of information about lines somewhere around the text. Giving this information I could then find out if either text is underlined or in a table.
Here is my code so far:
List<TextPosition> textPos = charactersByArticle.get(index);
for (TextPosition t : textPos)
{
if (t.getFont().getFontDescriptor() != null)
{
if (t.getFont().getFontDescriptor().getFontWeight() > BOLD_WEIGHT ||
t.getFont().getFontDescriptor().isForceBold())
{
isBold = true;
}
if (t.getFont().getFontDescriptor().isItalic())
{
isItalic = true;
}
}
}
I have tried to play around the PDGraphicsState object which is processed in the processEncodedText method in PDFStreamEngine class but no information of lines found there.
Any suggestions where this information could be retrieved from ?
Here is what I have found out so far:
PDFBox uses a resource file to bound PDF operators/instructions to certain classes which then process the information.
If we take a look at the PDFTextStripper.properties resource file under:
pdfbox\src\main\resources\org\apache\pdfbox\resources\
we can see that for instance the BT operator is bound to the
org.apache.pdfbox.util.operator.BeginText class and so on.
The PDFTextStripper under
pdfbox\src\main\java\org\apache\pdfbox\util\
takes this into account and utilizes the processing of the PDF with this classes.
BUT all graphical objects are ignored, therefore no information of underline or table structure!
Now if we take a look at the PageDrawer.properties resource file we can see that this one bounds to almost all operators available. Which is utilized by PageDrawer class under
pdfbox\src\main\java\org\apache\pdfbox\pdfviewer\
The "trick" is now to find out which graphical operators are those who represent underline and tables and to use them in combination with PDFTextStripper.
Now this would mean reading the PDF file specification, which is currently way to much work.
If someone knows which operators are responsible for which actions to draw underlines and table lines please let me know.
As you mention -- PDFBox uses resource files, to bind PDF operators/ instructions to visitors which will process the information.
You'd probably best start by copying PDFBox's existing visitor into your own source-folder, and then adding/ extending the implementation from there.
My long-ago PostScript experience recalls 'moveto' and 'lineto' operators. Since PDF is roughly PS-based, you'll be looking for something similar.
http://learnpostscript.wordpress.com/category/lineto/
PDF format is a b*tch -- it's HTML, done wrong. It represents graphical implementation, not semantics. Even reconstructing sentences is difficult -- words or even individual characters are positioned, the 'space' or 'newline' must be algorithmically reconstructed. In short, Adobe are a*holes. And Reader is an non-ergonomic, bug-riddled, insecure, bloated pig.
However, you can accomplish your requirement -- if you are willing to put, say, 12+ hours of work in. As well as detecting by position, underlines will typically be emitted in the PDF immediately after their text.. so you can latch your detection by PDF document-order, not just page position.
Also, try constructing a trivial two-line PDF with underlined text. Then see what you can make of it, parsing it back in! The underline should stick out like dog's bananas, and once you can detect that, you'll be well on the way.
PDFBox is not very good for extensibility, it's mainly just a big pile of algorithms. For this reason, just copy the PDFTextStripper source (and maybe have PageDrawer for reference) and prototype from there.
Hope this helps!
you can use Itext to generate pdf reports.
by using itext you can able to put the lines in easy way.
try the follwing.
document.add(new LineSeparator(0.5f, 50, null, 0, 198));
the above code is used to generate lines in pdf report. and set the dimensions according to your choice.
hope this will help you.
As far as I have understood the pdfbox, there is no option by which you can read underline. Maybe you can try itextpdf for this purpose.
According to the api getfont() returns The font size.
You can use getStyle() method and it will return STYLE_UNDERLINE for underlined font. Thus you can retrieve underline style.

Categories