Home > Back-end >  Itextpdf blur text in pdf
Itextpdf blur text in pdf

Time:11-25

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.

CodePudding user response:

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