3

Im trying to add a image from a URL address to my pdf. The code is:

Image image=Image.getInstance("http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif");
image.scaleToFit((float)200.0, (float)49.0);
paragraph.add(image);

But it does not work. What can be wrong?

kamituel
  • 34,606
  • 6
  • 81
  • 98
riahc3
  • 857
  • 4
  • 10
  • 23

3 Answers3

2

This is a known issue when loading .gif from a remote location with iText.

A fix for this would be to download the .gif with Java (not via the getInstance method of iText's Image class) and to use the downloaded bytes in the getInstance method of the Image class.

Edit: I went ahead and fixed remote gif loading in iText, it is included from iText 5.4.1 and later.

Michaël Demey
  • 1,567
  • 10
  • 18
  • Nope, doesnt seem to work either: `URL url = new URL("http://blog.phonehouse.es/wp-content/uploads/2013/01/Google.jpg"); InputStream in = new BufferedInputStream(url.openStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int n = 0; while (-1!=(n=in.read(buf))) { out.write(buf, 0, n); } out.close(); in.close(); byte[] response = out.toByteArray(); Image image=Image.getInstance(response); ` – riahc3 Apr 03 '13 at 08:11
  • I actually went ahead and fixed this in iText: http://sourceforge.net/p/itext/code/5741/ – Michaël Demey Apr 03 '13 at 08:59
  • I have fixed this for iText 5.4.1 (released today), could you confirm with your code? – Michaël Demey Apr 11 '13 at 09:47
0

Adding Image into Itext PDF is not possible through URL . Only way to add image in PDF is download all images in to local directory and apply below code

String photoPath = Environment.getExternalStorageDirectory() + "/abc.png";
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            final Bitmap b = BitmapFactory.decodeFile(photoPath, options);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            Bitmap.createScaledBitmap(b, 10, 10, false);
            b.compress(Bitmap.CompressFormat.PNG, 30, stream);
            Image img = null;
            byte[] byteArray = stream.toByteArray();
            try {
                img = Image.getInstance(byteArray);
            } catch (BadElementException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
Saljith Kj
  • 125
  • 2
  • 4
-3

The way you have used to add images to IText PDF is the way that is used for adding local files, not URLs.

For URLs, this way will solve the problem.

String imageUrl = "http://www.google.com/intl/en_ALL/" 
                  + "images/logos/images_logo_lg.gif";

Image image = Image.getInstance(new URL(imageUrl));

You may then proceed to add this image to some previously open document, using document.add(image).

For further reference, please visit the [Java IText: Image docs].

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
  • An explanation would make this an answer. – Mogsdad Apr 14 '16 at 19:34
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/12021099) – Debosmit Ray Apr 14 '16 at 20:02
  • Even if an answer has only code, *it is still considered an answer.* Don't say it's not an answer, and then try to delete it @DebosmitRay – Zizouz212 Apr 14 '16 at 21:42
  • @Zizouz212 I see what you are sayinh. Based on the 'buckets' available, this seemed to be most appropriate bucket. *This is a very low quality answer*. The question said `... But it does not work. What can be wrong?`. Does this answer the question? **No**. So, it does not provide an answer to the question. – Debosmit Ray Apr 14 '16 at 22:02
  • @DebosmitRay It's a low quality answer, but not one sufficient for deletion. See: http://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue – Zizouz212 Apr 14 '16 at 22:09
  • @Zizouz212 I get what you are saying, but I still think this is not an answer (for me, it is either img 2 and/or 5 in the linked post). More so, because the other answer to this question **solves** the question. I don't know which part you missed here, but the question was about `add a image from a URL address to my pdf`, and asked for what was wrong with the code. Either way, I will edit the answer to fix it. – Debosmit Ray Apr 14 '16 at 23:13