I try to render an image in a grid cell as follows:
grid.addColumn(probe ->
new ThemeResource("img/" + probe.getStructureData().getImageFileName()),
new ImageRenderer()
).setCaption("Structure");
I want to set the size of the image in pixel. How can I do this? It seems the ImageRenderer has no method for this.
Of cause I could size my images before. But I would like to add a big image that should be shown as tooltip and a small version of the same image should be shown in the grid cell. I would like to avoid adding the same image in 2 sizes.
Thank you!
Try doing it this way.
grid.addComponentColumn(probe -> {
Image image = new Image("", new ThemeResource("img/" + probe.getStructureData().getImageFileName()));
image.setWidth(100, Sizeable.Unit.PIXELS);
image.setHeight(100, Sizeable.Unit.PIXELS);
return image;
}).setCaption("Structure");
Related
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.
I have an html5 canvas and one image object to save this canvas contents
(all drawings: images,draws,..).
i want to play with the order (zindex) of these 2 objects from 1 button as below:
when i want to draw, i call the canvas to the top .
when i want to save i call image to the top(full with datURL). (and rightclick to save as PNG)
These switching operations must be repetitive and reliable.
I used this code (in a function called by the button onclick) but it fails in the second time (canvas have new contents)!
var canvas = document.getElementById('ycanvas1');
// save canvas image as data url (png format by default)
var dataURL = canvas.toDataURL();
// set canvasImg image src to dataURL - it can be saved as an image
document.getElementById('canvasImg').src = dataURL;
NB: i use IE olecontrol embed on a form (working on local) .Is there any security restrictions ?
I guess in the first case:
canvas.style.display="none" // hide the canvas
img.style.display="" //show the image
in the second case:
canvas.style.display="" //show the canvas
img.style.display="none" //hide the image
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
I am using canvas to add image programatically.
for example:
Image image1=new image1();
Image image2=new image1();
var left = new BitmapImage();
left.UriSource = new Uri("images/box.png", UriKind.Relative);
image1.Source = left;
image2.Source = left;
ContentPanelCanvas.Children.Add(image1);
ContentPanelCanvas.Children.Add(image2);
First time enter the loop Add Image to canvas. second time i want to clear canvas.
and again add different images same canvas.
Is this possible wp7.
tell some idea to do this.
thanks
Yes, it is possible:
ContentPanelCanvas.Children.Clear();
This will empty the canvas.
Why not simply change the Source property of the images you already added?
I have existing PDFs to which I need to dynamically add an image/images. The image comes from a file upload. Once I have the file uploaded, how can specify where to place the image on the PDF. One code snippet I found does not work correctly. This needs to work for PDFs with any number of pages. From what I understand, absolute positioning is set from the bottom-left corner of the last page of the PDF. If I need an image to be displayed 30 pixels from the top and 50 pixels from the left of page 1, how can I accomplish this? Or, if I need to display an image 50px from the top/100 px from the left on page 2?
I've tried using the code found at http://rip747.wordpress.com/2009/03/26/add-an-image-dynamically-to-a-pdf-with-cf-and-itext/. I've modified it for my needs below:
<cfscript>
myLeft = 30;
myTop = 50;
myPageNum = 1;
// output buffer to write PDF
fileIO = createObject("java","java.io.FileOutputStream").init(myOutputPath);
// reader to read our PDF
reader = createObject("java","com.lowagie.text.pdf.PdfReader").init(mySourcePath);
// stamper so we can modify our existing PDF
stamper = createObject("java","com.lowagie.text.pdf.PdfStamper").init(reader, fileIO);
// get the content of our existing PDF
content = stamper.getOverContent(reader.getNumberOfPages());
// create an image object so we can add our dynamic image to our PDF
image = createobject("java", "com.lowagie.text.Image");
// initalize our image
img = image.getInstance(imgPath);
x = (reader.getPageSize(1).width() - img.scaledWidth()) - myLeft;
y = (reader.getPageSize(1).height() - img.scaledHeight()) - myTop;
// now we assign the position to our image
img.setAbsolutePosition(javacast("float", x), javacast("float", y));
// add our image to the existing PDF
content.addImage(img);
// flattern our form so our values show
stamper.setFormFlattening(true);
// close the stamper and output our new PDF
stamper.close();
// close the reader
reader.close();
</cfscript>
The above code places my image at the top-right corner of page 2 - 50px form the top/30px from the left.
I know I'm close...just need a little help getting this nailed down for my needs.
I've updated my code. This gets the image to the top left corner of page 2 - correct positioning, but I want it on page 1:
x = myLeft;
y = (reader.getPageSize(1).height()) - img.scaledHeight() - myTop;
I thought maybe I needed to add the height of page 1 to get the image up to page 1, but the image completely disappears when I try either of the options below:
// I figure I'll need something like this to handle multi-page docs
y = (reader.getPageSize(1).height() * reader.getNumberOfPages()) - img.scaledHeight() - myTop;
y = reader.getPageSize(1).height() + reader.getPageSize(1).height() - img.scaledHeight() - myTop;
You're getting your "OverContent" from stamper.getOverContent(reader.getNumberOfPages());. The parameter for getOverContent() is the page number. So your code is getting a PdfContentByte for the last page, not the first.
I found my answer:
The page number has to be set in com.lowagie.text.pdf.PdfStamper.getOverContent():
content = stamper.getOverContent(myPageNum);
Knew it was easy.
are you using CF8+? You can use
<cfpdf action="addWatermark" source="myPDF.pdf" image="myImage.jpg"
position="0,0" rotation="0" showOnPrint="true" opacity="10">