-1

I'm using IKVM and PDFBox to create accessible PDF using C#. To add image to the document, I need to convert System.Drawing.Bitmap to java.awt.image.BufferedImage. I'm trying it with below code segment.

Code:

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(imgLoc);
java.awt.image.BufferedImage bufImg = .... //TO DO
PDJpeg img = new PDJpeg(document,bufImg);
contentStream.drawImage(img,50,50);

Can anyone please help me to add image to PDFdoc using PDFBOX in C#?

Thank You

RBK
  • 558
  • 5
  • 16
Briyatis
  • 57
  • 2
  • 14
  • `BufferedImage.getBitmap()` should return a `System.Drawing.Bitmap`, if the BufferedImage is created with `BufferedImage.TYPE_INT_RGB` or `BufferedImage.TYPE_INT_ARGB`. I think `ikvm.awt` internally uses `((BufferedImage)img).getBitmap();` (where `img` is `java.awt.Image`) to return a Bitmap. – Jimi May 09 '18 at 07:37

1 Answers1

1

I resolved the issue as below.Used PDPixelMap instead of Bitmap.

//png image
java.io.File file = new java.io.File(fileLoc);
java.awt.image.BufferedImage bufImg = javax.imageio.ImageIO.read(file);
PDXObject image = new PDPixelMap(doc,bufImg);
contentStream.drawXObject(image ,50,50, image.getWidth().image.getHeight());
Briyatis
  • 57
  • 2
  • 14
  • The decision whether to use PDJpeg or PDPixelMap depends on the source format. If it is a jpeg file, then it is better to use `PDJpeg(PDDocument doc, InputStream is)` because this allows to store it directly in jpeg format, i.e. without uncompressing first. If it is a png, then use PDPixelmap as you did here, i.e. your decision is good :-) – Tilman Hausherr May 09 '18 at 12:39