Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I try this Itext sharp and work

  iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("C:\\Program files\\BSS\\memorandum.jpg");

logo.ScaleAbsolute(550, 110);
logo.SetAbsolutePosition(25, 715);
pdfDoc.Add(logo);


I need from db to document

And When try solution down have error

+		$exception	{"Could not find file 'C:\\Users\\Bibic Goran\\source\\repos\\BSS\\BSS\\bin\\Debug\\System.Byte[]'."}	System.Net.WebException


What I have tried:

string Memorandum = "";
           using (SqlConnection openCon14 = new SqlConnection(Con))
           {
               SqlDataReader reader14;
               SqlCommand cmd14 = new SqlCommand();
               cmd14.CommandText = "select memorandum from podaci_o_korisniku";
               cmd14.Connection = openCon14;
               openCon14.Open();
               reader14 = cmd14.ExecuteReader();
               while (reader14.Read())
               {
                  Memorandum = reader14[0].ToString();
               }
               openCon14.Close();
           }


iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Memorandum);
Posted
Updated 5-Nov-19 4:57am
Comments
F-ES Sitecore 5-Nov-19 10:51am    
The easiest way to do this will probably be to create an Image object from your reader (google "c# create Bitmap image from byte array" and you'll probably find examples. Then pass that Bitmap\Image object to GetInstance

https://www.mikesdotnetting.com/article/87/itextsharp-working-with-images

or just create a stream reader to convert your byte array into a stream and use that with GetInstance directly. Again google "c# convert byte array to stream" for examples.

The field reader14[0] is returning a byte array.

You then convert that byte array to a string, which results in the string "System.Byte[]".

You then pass that string to the GetInstance overload which expects a file path. Since it's not a rooted path, the method treats it as a relative path, and tries to find the file:
C:\Users\Bibic Goran\source\repos\BSS\BSS\bin\Debug\System.Byte[]
That file doesn't exist, so you get an exception.

Change your code to load the image from a byte array:
C#
byte[] Memorandum = null;

using (SqlConnection openCon14 = new SqlConnection(Con))
using (SqlCommand cmd14 = new SqlCommand())
{
    cmd14.CommandText = "select memorandum from podaci_o_korisniku";
    cmd14.Connection = openCon14;
    openCon14.Open();

    using (SqlDataReader reader14 = cmd14.ExecuteReader())
    {
        if (reader14.Read())
        {
            Memorandum = (byte[])reader14[0];
        }
    }
}

iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Memorandum);
 
Share this answer
 
Comments
Goran Bibic 5-Nov-19 10:51am    
Thank you
Goran Bibic 5-Nov-19 10:58am    
This work fine...just addititional question
Iff iss null?

if (reader14[0] != null && reader14[0] != DBNull.Value)
{
Memorandum = (byte[])reader14[0];
}
else
{
Memorandum = ????
}
Richard Deeming 5-Nov-19 11:01am    
You've already initialized the variable to null, so you don't need that else block.

Obviously, you'll need to check whether Memorandum is null before you call Image.GetInstance, and not show the logo in that case.
Goran Bibic 5-Nov-19 11:04am    
No problem. If is null, show document without image. Now have errof if is null
Somesting like

Memorandum=empty;
Or like that
Richard Deeming 5-Nov-19 11:09am    
The variable is a byte array. You can either set it to null, or set it to an empty array - Array.Empty<byte>().
if (!reader14.IsDBNull(0))
{
    Memorandum = (byte[])reader14[0];
}
else
{
    Memorandum = null;
}
You may want to take a look at documentation and samples. It appears that the method you are trying to use is meant for retrieving the information from a file; and that you are passing in the information directly.
What you may want to try is creating a System.Drawing.Image object and then passing that object into iTextSharp

Reference:
iTextSharp - Working with images[^]
 
Share this answer
 
Comments
Richard Deeming 5-Nov-19 11:02am    
According to this Stack Overflow thread[^], iTextSharp has overloads which accept a byte array or a stream as well as a file path. :)
MadMyche 5-Nov-19 11:08am    
Saw that, but the error he gets is suggesting the method he is using is looking for a (string)location as opposed to a byte array
Richard Deeming 5-Nov-19 11:09am    
Only because he was passing in a string instead of a byte array. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900