Remove any spacing, padding, and margin so a cell is filled completely with iText 7 - itext7

How do I make a table in iText 7 with cells that don't have any spacing, padding, or margins on the inside. When I put an image in the cell it needs to fill the cell to 100%, touching the borders without any space of any kind at all.
I tried adding this to my Cell:
Cell qrImageCell = new Cell();
qrImageCell.setMargin(0f);
qrImageCell.setPadding(0f);
qrImageCell.setHeight(44f);
qrImageCell.add(barcodeImage.setMargins(0f,0f,0f,0f).setPadding(0f));
I also tried setting it in the table itself:
table.setPadding(0f);
table.setMargin(0f);
But whatever I try there is always a white rectangular area between the QR Code and the border (FYI: I would of course remove the border once it works.

Test with iText 7.1.15
With this image:
And this code:
Table table = new Table(1);
Image barcodeImage = new Image(ImageDataFactory.create("image.png"));
Cell qrImageCell = new Cell();
qrImageCell.setBorder(new SolidBorder(ColorConstants.RED, 2));
qrImageCell.setPadding(0f);
qrImageCell.add(barcodeImage);
table.addCell(qrImageCell);
doc.add(table);
The resulting output is:
Potential causes
Incorrect height
You have qrImageCell.setHeight(44f) in your code. Maybe that height does not correspond correctly with the height of your image. It's unlikely though that this is the problem, because it would not increase the width of the cell.
Impact of other cells
Making the table of the test above a 2-column table and adding a few cells shows that the size of the cells in the same row and column will have an impact on the size of the cell.
table.addCell(new Cell().add(new Paragraph("cell (1,2)")).setHeight(300));
table.addCell(new Cell().add(new Paragraph("cell (2,1)")).setWidth(300));
table.addCell(new Cell().add(new Paragraph("cell (2,2)")));
It's also unlikely that this is the problem, because in your output the image is centered in the cell.
There is no white space
Maybe the white "margins" are simply part of your barcode image. In that case, there isn't any spacing, margin or padding, but it would visually seem so.
Verify your input image or set a background color for the cell, so you can verify whether the white area is part of the image or part of the cell: qrImageCell.setBackgroundColor(ColorConstants.GREEN)

In the end the backgroundcolor trick helped me a lot. In the end the QR Image which is also generated by iText7 for some reason has a white border around itself. It's generated by this code:
BarcodeQRCode qrCode = new BarcodeQRCode(text);
PdfFormXObject barcodeObject = qrCode.createFormXObject(ColorConstants.BLACK, pdfDoc);
Image barcodeImage = new Image(barcodeObject).setPadding(0f).scaleAbsolute(44f, 44f)
But indeed setting the padding to "0f" removes any padding in the cell. Just have to find out if there's a possibility to remove that border around the QR code.

Related

D3 expanding rectangle's text is not adjusted

I have a rectangle and a text created through D3 which are grouped together like below.
<g>
<rect></rect>
<text></text>
</g>
In a javascript function I'm expanding this rectangle's width to allow the the value of text to be displayed in one go. (If the value is too long as the text value is set dynamically). This is working fine. However even though the rectangle width is expanding depending on the text value size , D3 text element is not showing the complete text value that is added, eventually the text is moved to left and the first bit of it can't be seen.
Do I need to increase the text element's width too or what would be the solution for this?
[UPDATE]
Following is the current code.
var elms = d3.selectAll("[id=s]"); // has the rectangle and text elements
var s = d3.selectAll("[id=s]").selectAll("text"); // just the text elements
var minimumValue = 100;
// updating rectangle width
elms.attr('width', function() { return dynamic < minimumValue ? minimumValue : dynamic;});
Now I need a way to update the position of the text element when the rectangle is expanded. Hope this is more clear. I just tried manually updating 'x' property of text element that works. But can I derive this value dynamically to set it within this function?
In case someone is looking for this, was able to get the text new 'x' value by x of rectangle + half of rectangle's width (and similar for y but with the height).
Got the answer from this link (How to place and center text in an SVG rectangle)

Horizontal scrollView/UIImageView layout issue

The goal: Have a scroll view that displays an array of uiimageviews (photos) that you can horizontally scroll through them
How I understand to do this: Make the frame (CGRect) of each uiimageview the height and width of the scroll view, the y value to 0 on each, and set the first imgViews x value to 0. For every imgView after that, add the width of the scrollview to the x value. In theory, this would line the imgViews (Photos) up next to each other horizontally and not allow for any vertical scrolling or zooming, purely a horizontal photo viewer.
The storyboard setup: I am creating my scrollview in a xib file (It’s a custom uiCollectionViewCell), with these constraints:
— Top space to cell (0)
— Trailing space to cell (0)
— Leading space to cell (0)
— Height of 400
— Bottom space to a view (0)
—— (See Below for img)
Laying out the UIImgViews:
func layoutScrollView() {
for (index, img) in currentImages.enumerate() {
let imgView = UIImageView(frame: CGRect(x: CGFloat(index) * scrollView.bounds.width, y: CGFloat(0), width: scrollView.bounds.width, height: scrollView.bounds.height))
imgView.contentMode = .ScaleAspectFill
imgView.image = img
scrollView.addSubview(imgView)
scrollView.contentSize = CGSize(width: imgView.frame.width * CGFloat(index), height: scrollView.bounds.height)
scrollView.setNeedsLayout()
}
}
My suspicion: I suspect the issue is stemming from the auto layout constraints i’ve specified, but (considering Im asking a SO question) not sure
If there is a better way to do this (really the correct way) please let me know! I have been trying to wrap my head around this for a few days now.
I appreciate all responses! Thanks for reading
EDIT #1
I tried paulvs approach of setting setNeedsLayout & layoutIfNeeded before the "for" loop, and still no luck. Here is (out of three images selected) the second photo displaying. It seems that both the first and second photos are way longer than the content view and that would move the middle view over (Squished).
Your code looks fine except for a few details (that may be causing the problem):
Add:
view.setNeedsLayout()
view.layoutIfNeeded()
before accessing the scrollView's frame (a good place would be before the for-loop).
This is because when using Autolayout, if you access a view's frame before the layout engine has performed a pass, you will get incorrect frames sizes/positions.
Remove these lines from inside the for-loop:
scrollView.contentSize = CGSize(width: imgView.frame.width * CGFloat(index), height: scrollView.bounds.height)
scrollView.setNeedsLayout()
and place this line after (outside) the for loop:
scrollView.contentSize = CGSize(width: imgView.frame.width * CGFloat(currentImages.count), height: scrollView.bounds.height)

How to centrally align an Image in Excel using apache poi

HSSFPatriarch drawing = sheet.createDrawingPatriarch();
HSSFClientAnchor my_anchor = (HSSFClientAnchor) helper.createClientAnchor();
my_anchor.setAnchorType(HSSFClientAnchor.DONT_MOVE_AND_RESIZE);
my_anchor.setCol1(0);
// my_anchor.
my_anchor.setRow1(excelData.getRowNum());
strb.append(" ");
HSSFPicture my_picture = drawing.createPicture(my_anchor, my_picture_id);
/* Call resize method, which resizes the image */
my_picture.resize();
I am passing sheet, helper as parameter to my method.
With this code, still Image Icon can be moved in the excel sheet.
Also I want to set the vertical alignment for the icon in the cell as bottom aligned. Please suggest.
You can align the image using anchor, adjust the values of Dx1, Dy1, Dx2 and Dy2 accordingly. Below is the example:-
ClientAnchor anchor = sheet.getWorkbook().getCreationHelper().createClientAnchor();
anchor.setDx1(0);
anchor.setDy1(0);
anchor.setDx2(0);
anchor.setDy2(0);
Refer to the below URL for complete example:-
https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/ss/examples/AddDimensionedImage.java
You can align the image using anchor, adjust the values of Dx1, Dy1, Dx2 and Dy2, pict.resize() accordingly. Below is the example:
ClientAnchor anchor = sheet.getWorkbook().getCreationHelper().createClientAnchor();
Modify below values s per your original image size
anchor.setDx1(0);
anchor.setDy1(0);
anchor.setDx2(90);
anchor.setDy2(90);
// Creates a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
// Reset the image to the original size
Modify below value s per your original image size
pict.resize(1.75);
Please refer updated below URL for complete example: https://svn.apache.org/repos/asf/poi/trunk/poi-examples/src/main/java/org/apache/poi/examples/ss/AddDimensionedImage.java

BlueJ - GUI JTextField, how to make the corners of it round like an oval?

How do I make the corners of a TextField round?
I'm using Javax.swing.*;
Here's my code for BlueJ:
inputLine = new JTextField();
inputLine.setBounds(480, 350, 150, 30);
contentPane.add(inputLine);
inputLine.setForeground(Color.black);
inputLine.setBackground(Color.white);
inputLine.setBorder(BorderFactory.createLineBorder(Color.black));
inputLine.setFont(new Font("Tahoma", Font.BOLD, 13));
inputLine.setHorizontalAlignment(JTextField.CENTER);
inputLine.setEditable(true);
contentPane.add(inputLine);
inputLine.addActionListener(this);
inputLine.setText("ID Number");
what you can do, is first create a Jframe window.
If you want only a text Field, make the dimensions of the j Frame a bit bigger than the preferred text Field size.
Then, set the layout of the jFrame to absolute.
Create a Jlabel that covers the whole screen Jframe.
Create the text Field only after this step.
Then create the text Field at the preferred location in the j Frame.
Design a graphic / picture for the background and set the icon of the background jLabel to the graphic you have designed.
The graphic you have designed must have a rounded rectangular picture in it and the text Field must be exactly positioned on top of that.
Once that is done set the opacity of the Text Field to 0.0. This should work very well...
JFrame frame = new JFrame("FrameDemo");
jframe.setPreferredSize(new Dimension(x, y));
[please add the other methods to create a jframe window...these are only the basic commands]
pane.setLayout(null);//makes it an absolute layout so that it does not reshape itself
frame.setVisible(true);
after doing this, design your graphics.
then:
import it into the current working directory, where this program has been written.
then:
create a jLabel inside the jFrame.
JLabel label1 = new JLabel("JLabel Set Icon Example.");
set its icon to the graphic you have designed.
label1.setIcon(new ImageIcon("full path of the image _ with the proper file extension"));
then
textField = new JTextField(desired value);//please add the desired value into the brackets
//set its location and all the other thing that are required
set the location of the textField exactly over the the rounded rectangle in the graphic.
then:
textField.isOpaque("false");
jTextField1.setBorder(null);//this should work
and there you go...the user will think that the textField is a rounded rectangle but actually it is like a rectangle which has a transparent background...
let me know if there are any problems.
thanks.

Rotate paragraphs or cells some arbitrary number of degrees -- Itext

I have a web site where the users upload photos and create photobooks. Also, they can add text at absolute positions, rotations, and alignments. The text can have new lines.
I've been using the Itext Library to automatize the creation of the Photobooks High Quality Pdfs that are printed latter on.
Adding the user uploaded images to the PDFs was really simple, the problem comes when I try to add the text.
In theory what I would need to do, is to define a paragraph of some defined width and height, set the users text, font, font style, alignment (center, left, right, justify), and finally set the rotation.
For what i've read about Itext, i could create a paragraph set the user properties, and use a ColumnText Object to set the absolute position, width and height. However it's not possibly to set the rotation of anything bigger than single line.
I can't use table cells either, because the rotation method only allow degrees that are multiples of 90.
Is there a way to add a paragraph with some rotation (say 20 degrees) without having to add the text line by line using the ColumnText.showTextAligned() method and all math that involves?
---- Edit: 08-Ago-2013 ----
If it helps anyone, this is the code I used to solve this problem (thanks to Bruno):
//Create the template that will contain the text
PdfContentByte canvas = pdfWriter.getDirectContent();
PdfTemplate textTemplate = canvas.createTemplate(imgWidth, imgHeight); //The width and height of the text to be inserted
ColumnText columnText = new ColumnText(textTemplate);
columnText.setSimpleColumn(0, 0, imgWidth, imgHeight);
columnText.addElement(paragraph);
columnText.go();
//Create de image wraper for the template
Image textImg = Image.getInstance(textTemplate);
//Asign the dimentions of the image, in this case, the text
textImg.setInterpolation(true);
textImg.scaleAbsolute(imgWidth, imgHeight);
textImg.setRotationDegrees((float) -textComp.getRotation()); //Arbitrary number of degress
textImg.setAbsolutePosition(imgXPos, imgYPos);
//Add the text to the pdf
pdfDocument.add(textImg);
Create a PdfTemplate object; just a rectangle.
Draw your ColumnText on this PdfTemplate; don't worry about the rotation, just fill the rectangle with whatever content you want to add to the column.
Wrap the PdfTemplate inside an Image object; this is just for convenience, to avoid the math. This doesn't mean your text will be rasterized.
Now apply a rotation and an absolute position to the Image and add it to your document.
Your problem is now solved ;-)
PS: I'm the author of the iText in Action books.
thanks to both our friends (Bruno & BernalCarlos)
my final code for users that use "RTL" in their projects is here :
// step 1
Document document = new Document();
document.setPageSize(PageSize.A4);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destination_file));
CreateBorder event = new CreateBorder();
writer.setPageEvent(event);
// step 3
document.open();
// step 4
int imgWidth=400;
int imgHeight=50;
//Create the template that will contain the text
PdfContentByte canvas = writer.getDirectContent();
PdfTemplate textTemplate = canvas.createTemplate(imgWidth, imgHeight);
//The width and height of the text to be inserted
ColumnText columnText = new ColumnText(textTemplate);
columnText.setSimpleColumn(0, 0, imgWidth, imgHeight);
columnText.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
columnText.addElement(new Paragraph("محاسبه بار غیر متعادل", font_IranSemiBold));
columnText.go();
//Create de image wraper for the template
Image textImg = Image.getInstance(textTemplate);
//Asign the dimentions of the image, in this case, the text
textImg.setInterpolation(true);
textImg.scaleAbsolute(imgWidth, imgHeight);
textImg.setRotationDegrees(90); //Arbitrary number of degress
textImg.setAbsolutePosition(50, 200);
//Add the text to the pdf
document.add(textImg);
// step 5
document.close();

Resources