Let's review the relationship between "docx" and "pictures":
As I understand it, *.docx stores original pictures (pictures at the moment when you copy/paste them into Word). And every time when you use that picture, Word makes a "link" to original picture.
But if you make some changes to that picture (for example resize, crop or change color) Word remembers your changes, modifying the "link" (add some special tags). That's great, because you will never lose quality of your picture!
Let's get a picture from our *.docx file. To do that I use this code snippet:
XWPFDocument wordDoc = new XWPFDocument( pathToFile );
for (XWPFParagraph p : wordDoc.getParagraphs()) {
for (XWPFRun run : p.getRuns()) {
for (XWPFPicture pic : run.getEmbeddedPictures()) {
byte [] img = pic.getPictureData().getData()
File outputfile = new File ( pathToOutputFile );
BufferedImage image = ImageIO.read(new ByteArrayInputStream(img));
ImageIO.write(image , "png", outputfile);
}
}
}
But this way I get the original pictures from *.docx. If, for example, you cropped out a section from your picture and gave me the rest, then I always find the whole image in outputfile. That's not good.
Does anyone know how to get the picture with all changes that someone made to it in Word?
Related
I was trying to find out how to set the text position and rotation in a pdf with the "itextpdf" dependency. My current code looks like that:
//Create the pdf document
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
PdfContentByte pdfCB = new PdfContentByte(writer);
//Create the font which will be used
Font font = FontFactory.getFont(FontFactory.COURIER, 16, BaseColor.BLACK);
//Open the document
document.open();
//Get the image which was previously created (Not important for this)
Image iTextImage = Image.getInstance(pdfCB, image, 1);
//Set the position of the image (How do I do that with the text?)
iTextImage.setAbsolutePosition(0, 0);
//Create a text chunk which I want to set the position of and rotate
Chunk chunk = new Chunk("Hello World!", font);
//Add both the image and text to the pdf and save it
document.add(iTextImage);
document.add(chunk);
document.close();
//Notify the user that it was a success
System.out.println("Image added successfully and PDF file created!");
Now I was wondering how to set the position and rotation of the text. I want to create PDF's for a project which will later be printed. Now I don't want to add like a hundred whitespace's each time there is a blank spot in the page like the table of contents and then a footer at the bottom. I don't want to write the table of contents and then do \n 20 times to reach the bottom, so I can add my footer text, like page number, author, project ID, date, company logo, etc.
So how do I best position the text here? Also, I would like to rotate it, since some of the text has to be turned by 90 degrees for stuff like labels for tables and so on.
Any help would be appreciated, thank you.
PS: I already tried to set the absolute position, which just makes the text disappear.
I need to place an image in a specific location in docx file. I can place an image at any paragraph and even create a new paragraph, but aligning breaks.
I tried to put a placeholder in the document and put my image there. Here is my code:
try (XWPFDocument doc = new XWPFDocument(
Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream(filePath)));
) {
List<XWPFParagraph> xwpfParagraphList = doc.getParagraphs();
for (XWPFParagraph xwpfParagraph : xwpfParagraphList) {
if (Objects.equals(xwpfParagraph.getText().trim(), "QR")) {
InputStream is = new ByteArrayInputStream(qr);
XWPFRun qrRun = xwpfParagraph.createRun();
qrRun.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, "qr", Units.toEMU(100), Units.toEMU(120));
is.close();
}
...
and the output docx looks like this:
enter image description here
How to place an image at the place, where the QR placeholder is located?
xstButton.setIcon(new ImageIcon("D://icon-tender-check-press.png"));
I am using this line of code to display an image on a Java button.
I cannot see the desired image on the button, need help!!!
You could try it like this:
Image image = ImageIO.read(getClass().getResource("D://icon-tender-check-press.png"));
button.setIcon(new ImageIcon(image));
But i would suggest to create a Folder in your project to store images:
Image image = ImageIO.read(getClass().getResource("images/icon-tender-check-press.png"));
button.setIcon(new ImageIcon(image));
Although i am not exactly sure what your question is
Probably file "D://icon-tender-check-press.png" doesn't exists or is not a valid image.
Check it exists first.
File f = new File("D://icon-tender-check-press.png");
if(f.exists() && !f.isDirectory()) {
System.out.println("File exists");
}
I try to get a image (jpg format) from url to put in my pdf using itext 5.0.5 with this simple code below:
Image imageToShow = null;
imageToShow = Image.getInstance(new URL("any image url here"));
imageToShow.scaleAbsolute(size[0], size[1]);
I get the image but the value from absoluteX and absoluteY is always a 'NaN' value and this problem prevents me to change this values (third line), what I'm doing wrong?
If you are in a situation where you can switch to another iText version, try out iText7.
The equivalent code would be:
// src url
String somePath = "https://www.pdfa.org/wp-content/uploads/2016/08/ITSC-Logo-Horizontal-RGB-300dpi.png";
// fetch image data
ImageData imageData = ImageDataFactory.create(new URL(somePath));
// turn into image object
Image image = new Image(imageData);
// perform scaling operation
image = image.scaleAbsolute(120f, 120f);
// debug
System.out.println(image.getImageWidth() + "x" + image.getImageHeight());
What I want to do is read an image from FileChooser and write it to file. I had to store the image in a javafx.scene.image.Image so that I can display it and clip it inside a circle. I have a little problem with trying to write the image that I got from javafx.scene.image.Image to file. The conversion process is not fluid, converts from CMYK to RGB (therefore turning my picture to some pink thing.
Please, I have checked a lot of other sources, and no one has been able to give me a notable solution
FileChooser fileChooser = new FileChooser();
File selectedFile = fileChooser.showOpenDialog(parent);
// get Image from selectedFile
Image userImg = = new Image( selectedFile.toURI().toURL().toString() );
if ( userImg != null ) {
String format = "jpg";
String filename = "d:\\pictureName."+ format;
File file = new File(filename);
// convert Image to BufferedImage
BufferedImage bufferedImage = SwingFXUtils.fromFXImage( userImg, null);
try {
// this is where i want to convert the color mode
// from cmyk to rgb, before i write it to file
ImageIO.write( bufferedImage, format, file );
} catch (IOException e) {
System.out.println("Exception :: "+ e.getMessage() );
}
}
Why do you think that there is some CMYK to RGB conversion happening? I suspect the reason for your "pink thing" is something different. The easiest way to find out is to change your output format from jpeg to png and see whether it makes a difference.
I think you are again one of the many people who are hit by this bug https://bugs.openjdk.java.net/browse/JDK-8119048 which is not considered important enough to be fixed. If you read the comments in there you will find a work-arround. Basically the idea is to copy the image after the conversion into a new image without alpha channel.
I'd really like to know how many more people have to waste their time until this bug finally gets enough attention to be fixed.