itextpdf 模糊 pdf 中的文本

Itextpdf blur text in pdf

有没有办法将 模糊 文本(文本不是随机文本,但它来自数据库)添加 showTextAligned(PdfContentByte.ALIGN_LEFT,"Random text",297,606,rotation)PdfContentByte ?或任何其他模糊文本的方式(它可以是图像,但图像必须从输入文本生成,而不是您添加到项目中的图像)并将其添加到 pdf 中?我正在使用 itextpdf:5.5.12(在 android)

我想到的小例子:

    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);

在android中使用canvas(如果使用选项二):

//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);

第二个选项的唯一问题是 canvas 中生成的文本非常非常糟糕,并且在以 pdf 格式添加图像时看起来不太好...所以哪里可能有问题“字体错误”?

非常感谢任何帮助或建议。

解法: 选择第二个选项。 增加位图大小(设置 Bitmap.Config.ARGB_8888),将字体大小增加到大于 7 或 8 的大小,然后对其应用模糊(在我的例子中,我将字体大小增加到 24),保存图像作为位图,然后将图像添加到 PdfContentByte 或直接将图像添加到文档(将 scaleAbsolute 应用于图像和 absolutePosition)。