[英]iText: how to put transparent images into a pdf without making the transparent parts BLACK?
I'm using iText 5.5.9 inside my Android Studio project to make a PDF file. 我在Android Studio项目中使用iText 5.5.9制作PDF文件。
When I add an image, the transparent parts turn to black inside the PDF. 添加图像时,PDF内的透明部分变为黑色。
How can I avoid that? 我该如何避免呢?
Here is the part of my code which illustrates the problem: 这是我的代码中说明问题的部分:
// add card
Resources res=getResources();
Drawable drawable=res.getDrawable(R.drawable.card);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();
Image img=Image.getInstance(bitMapData);
img.scaleAbsolute(300f,156.3f);
img.setSmask(false);
doc.add(img);
If you want to add a transparent circle image with broader (ie stroke) in iText , you can add a circle with stroke. 如果要在iText中添加具有更宽(即笔触)的透明圆形图像,则可以添加具有笔触的圆形。
Use the code below (and change it to suit your needs) : 使用下面的代码(并更改为适合您的需要):
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("myCirclepdffile.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.circle(250.0f, 500.0f, 200.0f);
cb.stroke();
} catch (Exception e) {
System.err.println(e.getMessage());
}
Transparent images aren't supported in PDF (such as PNGs or GIFs with a color that acts as if it were transparent), but they are supported in iText. PDF不支持透明图像(例如PNG或GIF的颜色看起来像是透明的),但iText支持透明图像。
Transparency in images in PDF is achieved by adding the opaque image along with an image mask that mimics the transparency. 通过添加不透明图像以及模仿透明度的图像蒙版,可以实现PDF中图像的透明度。 For every transparent image added to a PDF, iText adds two images: the opaque image and the mask.
对于添加到PDF的每个透明图像,iText都会添加两个图像:不透明图像和蒙版。
This is true for PNG files and GIF files that support transparency. 对于支持透明度的PNG文件和GIF文件,这是正确的。 Unfortunetely, you are using the JPEG format, and JPEG doesn't support transparency.
不幸的是,您使用的是JPEG格式,而JPEG不支持透明性。 See Transparent background in JPEG image
请参见JPEG图像中的透明背景
Use PNG or GIF instead of JPEG. 使用PNG或GIF代替JPEG。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.