Is there a way to blur text (text is not random text but it comes from db) added with showTextAligned(PdfContentByte.ALIGN_LEFT,"Random text",297,606,rotation) to PdfContentByte ? Or any other way to blur text (it can be image but the image must be generated from input text and it's not image that you added to your project) and added it to the pdf ? I'm using itextpdf:5.5.12 (on android)
Small example what i have in mind:
PdfWriter pdfWriter=PdfWriter.getInstance(document, new FileOutputStream(file));
PdfContentByte cb = pdfWriter.getDirectContent();
cb.saveState();
cb.beginText();
cb.setColorFill(baseCol);
cb.setFontAndSize(myBaseFontArialNarrowBold,7);
//some function that blurs the text ??
//here
cb.showTextAligned(PdfContentByte.ALIGN_LEFT,"Random text that we want to blur",297,638,rotation);
cb.endText();
cb.restoreState();
//another option would be to add generated image from canvas
Bitmap btmp=bBlur.getBitmap();
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
btmp.compress(Bitmap.CompressFormat.PNG, 100, stream2);
byte[] byteArray2 = stream2.toByteArray();
Image img2 = Image.getInstance(byteArray2);
cb.addImage(img,250,0,0,250,290,398);
Using canvas in android (if going with option two):
//arial narrow bold
paint.setTypeface(typeface);
paint.setColor(Color.argb(190,0,0,0));
paint.setTextAlign(Paint.Align.LEFT);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setSubpixelText(true);
paint.setLinearText(true);
//paint.setMaskFilter(new BlurMaskFilter(1, BlurMaskFilter.Blur.NORMAL));
paint.setTextSize(8);
canvas.rotate(-0.9876f);
canvas.drawText(textAr[0],6.8f,11.78f,paint);
paint.setTextSize(7);
canvas.drawText(textAr[1],6.8f,20,paint);
canvas.drawText(textAr[2],6.8f,45,paint);
canvas.setBitmap(bitmap);
The only problem with the second option is that generated text in canvas is very very bad and it doesn't look nice when image is added in pdf... So where could be a problem with "bad font"?
Any help or advice is very much appreciated.
Solution:
Going with second option.
Increase bitmap size (set Bitmap.Config.ARGB_8888), increase font size to something bigger then size 7 or 8 and then apply blur to it (in my case i increased the font size to 24), save image as bitmap and then add image to PdfContentByte or add image directly to document (apply scaleAbsolute to image and absolutePosition).
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 know that it is possible to set the color of the run by: How can I set background colour of a run (a word in line or a paragraph) in a docx file by using Apache POI?
The relevant code is for example (see the answer in the referenced question):
XWPFRun run=paragraph.createRun();
run.setText("background color");
CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
cTShd.setVal(STShd.CLEAR);
cTShd.setColor("auto");
cTShd.setFill("00FFFF");
I would like to use an image instead as a pattern, is it possible? I know that the argument used for the fill argument is an object, so is it possible to use a picture reference or something like that?
I also know how to add directly a picture in a docx document, for example:
Image image = // my image to use
float widthEMU = // the width of the image in EMU
float heightEMU = // the height of the image in EMU
BufferedImage bimage = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(_image, 0, 0, null);
bGr.dispose();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bimage, "png", os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
XWPFPicture pic = run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, "Picture", widthEMU, heightEMU);
i need to write a program which takes an image, resizes and rotates it and then saves it out. The first 2 point are done, but now I have a problem. Every time I convert a grayscale image it becomes a monochrome image.
I load the target image with the following command:
BufferedImage sourceimg = ImageIO.read(input);
And after I scaled and rotated it I save it out with the following command:
BufferedImage newimg = new BufferedImage(sourceimg.getHeight(), sourceimg.getWidth(), sourceimg.getType());
op.filter(sourceimg, newimg);
sourceimg = newimg;
ImageIO.write(sourceimg, "png", outputFile);
This works fine for every image except grayscale images. I have already tried a workaround by setting the type of every image to ARGB but there has to be another way. Is there a way to get the IndexColorModel of an given image?
The problem ist now solved, i just had to change:
BufferedImage newimg = new BufferedImage(sourceimg.getHeight(), sourceimg.getWidth(), sourceimg.getType());
to:
BufferedImage newimg = new BufferedImage(sourceimg.getWidth(), sourceimg.getHeight(), BufferedImage.TYPE_INT_ARGB);
There are other solutions, especially if you like to keep the original image type (ie. keep palette of IndexColorModel images etc.).
In fact, the easiest is to just do (assuming op is a standard BufferedImageOp):
BufferedImage newimg = op.filter(sourceimg, null);
Here a new, compatible image will be created for you, and will be of correct size to keep the result of the operation.
Another option, which will preserve the image type and color model, is slightly more verbose:
ColorModel sourceCM = sourceimg.getColorModel(); // Will be the IndexColorModel in your case
// I'm assuming you are deliberately switching width/height to rotate 90 deg
WritableRaster raster = sourceCM.createCompatibleWritableRaster(sourceimg.getHeight(), sourceimg.getWidth());
BufferedImage newimg = new BufferedImage(sourceCM, raster, sourceCM.isAlphaPremultiplied(), null);
op.filter(sourceimg, newimg);
I'm trying to rotate an image using thumbnailator library. It works fine except I don't see any way to set background color of the new image.
Thumbnails.of(image).rotate(45).toByteArray()
If I rotate an image 45 degree it makes corners black. If I need to set some other color, how do I do this?
Example of a rotated image with black background:
You can transform to an "intermediate" image and then apply your background color.
This is mostly untested.
Color newBackgroundColor = java.awt.Color.WHITE;
// apply your transformation, save as new BufferedImage
BufferedImage transImage = Thumbnails.of(image)
.size(100, 60)
.rotate(45)
.outputQuality(0.8D)
.outputFormat("jpeg")
.asBufferedImage();
// Converts image to plain RGB format
BufferedImage newCompleteImage = new BufferedImage(transImage.getWidth(), transImage.getHeight(),
BufferedImage.TYPE_INT_ARGB);
newCompleteImage.createGraphics().drawImage(transImage, 0, 0, newBackgroundColor, null);
// write the transformed image with the desired background color
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(newCompleteImage, "jpeg", baos);
http://www.robinswebsitestudio.com/ButtonSamples/buttons.html
The following code does write to a premade image. The resulting image, though, has significant problems.
the original png file is a 3D button with Index Transparency and a matte so that the gif file would
blend in with the page background color
the resulting gif file is flat, the matte is gone, and the text doesn’t look smooth. The resulting
file is therefore is not something you would get from Adobe Photoshop or Fireworks and doesn’t look
professional.
If anyone has suggestions, I’m all ears. Is it impossible to get a high quality 3D button using Java?
String pathToWeb = getServletContext().getRealPath(File.separator);
File f = new File(pathToWeb + "activities.png");
BufferedImage bufferedImage = ImageIO.read(f);
// Create a graphics which can be used to draw into the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setFont(new Font( "Arial", Font.BOLD, 15 ));
//create a string with black
g2d.setColor(Color.black);
g2d.drawString("Activities", 40, 40);
// Disposes of this graphics context and releases any system resources that it is using.
g2d.dispose();
// Save as gif
File file = new File("myactivities.gif");
ImageIO.write(bufferedImage, "gif", file);