I have the following code. I am getting error trying many solutions but none of them works
if (model.Pdfdoc != null)
{
image = new byte[model.Pdfdoc.ContentLength];
model.Pdfdoc.InputStream.Read(image, 0, image.Length);
System.UInt32 mimetype;
FindMimeFromData(0, null, image, 256, null, 0, out mimetype, 0);
System.IntPtr mimeTypePtr = new IntPtr(mimetype);
model.filetype = Marshal.PtrToStringUni(mimeTypePtr);
Marshal.FreeCoTaskMem(mimeTypePtr);
//WATER MARK IMAGE
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/Watermark.png"));
img.SetAbsolutePosition(0, 80f);
img.ScaleAbsolute(600f, 600f);
PdfReader readerInvoice = new PdfReader(image);
int n = readerInvoice.NumberOfPages;
MemoryStream msPIA = new MemoryStream();
using (MemoryStream ms1 = new MemoryStream())
{
// Create the stamper
//using (PdfStamper stamper = new PdfStamper(readerInvoice, ms1))
using (PdfStamper stamper = new PdfStamper(readerInvoice, ms1, '\0', true)) error in this line
{
for (int page = 1; page <= n; page++)
{
PdfContentByte over = stamper.GetOverContent(page);
over.AddImage(img);
}
stamper.Close();
}
msPIA = ms1;
}
byte[] bytesInStream = msPIA.ToArray(); // simpler way of converting to array
msPIA.Close();
image = bytesInStream;
if (model.filetype.Equals("application/pdf"))
{
model.Pdf = image;
}
}
Vishal JoshiPosted Jun 11, 2024, 5:38 AM
Hello
Try to Reinstall the required packages using the below command.
Thanks
Vishal Joshi
Amit MohantyPosted May 31, 2024, 10:19 AM
Ensure that PdfReader is correctly initialized. You're passing image to it, which is a byte array, but PdfReader typically expects a file path or a stream.
This approach might not work as expected because the PdfReader constructor expects a file path, a byte array, or a stream, but it doesn't directly handle the content of a PDF file stored in a byte array.
Jignesh KumarPosted May 31, 2024, 7:11 AM
Hello Garima,
You can skip pdf version or you can assign as below,
If you wish to skip version then use below,
Garima BansalPosted May 31, 2024, 4:49 AM
Jayraj Chhaya Sir,
here is the screeshot where i am getting error
Jayraj ChhayaPosted May 30, 2024, 2:02 PM
Hi Garima Bansal,
Please share screen shot or error message so I can help you and give some suggestion accrodingly.
Garima BansalPosted May 30, 2024, 11:43 AM
Jayraj Chhaya Sir,
i checked the
readerInvoiceobject is not null, somehow code works but still getting the error.Jayraj ChhayaPosted May 30, 2024, 6:42 AM
The "Object reference not set to an instance of an object" error typically occurs when you are trying to access a member or property of an object that is null. In the provided code snippet, the error is occurring on the line
using (PdfStamper stamper = new PdfStamper(readerInvoice, ms1, '\0', true)).To troubleshoot this issue, you should check if the
readerInvoiceobject is null before using it to create a newPdfStamperinstance. You can add a null check like this:By adding this null check, you ensure that the
PdfStamperis only created when thereaderInvoiceobject is not null, preventing the "Object reference not set to an instance of an object" error.Additionally, make sure that you have imported the necessary namespaces for iTextSharp:
If the issue persists, please provide more information about the specific error message and any additional code or stack trace that may be relevant.