【问题标题】:Itextpdf blur text in pdfItextpdf 模糊 pdf 中的文本
【发布时间】:2021-12-27 22:31:28
【问题描述】:

有没有办法 模糊 文本(文本不是随机文本,但它来自数据库)添加 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 中使用画布(如果使用选项二):

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

第二个选项的唯一问题是画布中生成的文本非常非常糟糕,并且在将图像添加到 pdf 时看起来不太好......那么“坏字体”可能会出现问题吗?

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

【问题讨论】:

  • 确实,如果您希望文本字形具有特定外观,请使用字形具有该外观的字体。
  • 我需要使用特定的字体(在我的例子中是 Arial 窄粗体),所以要获得我想要的更改字体不是一种选择。我尝试使用 android canvas (所以从画布创建位图并将其放入 pdf (如果字体质量不会那么差,这将非常有用))但是字体和一切都太糟糕了,所以字体的质量真的是,真的很糟糕,看起来也不好看。
  • 我更新了我正在解决的问题...

标签: java android pdf itext blur


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多