I have the following code. I am getting error trying many solutions but none of them works
C#
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 JoshiRank #377 · 6.7K reputationPosted Jun 11, 2024, 1:38 AM EDT
Hello
Try to Reinstall the required packages using the below command.
nuget updateThanks
Vishal Joshi
Jayraj ChhayaRank #304 · 8.2K reputationPosted May 30, 2024, 2:42 AM EDT
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:if (readerInvoice != null) { using (PdfStamper stamper = new PdfStamper(readerInvoice, ms1, '\0', true)) { // Rest of the code } }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:
using iTextSharp.text; using iTextSharp.text.pdf;If the issue persists, please provide more information about the specific error message and any additional code or stack trace that may be relevant.
Garima BansalRank #1,555 · 1K reputationPosted May 30, 2024, 7:43 AM EDT
Jayraj Chhaya Sir,
i checked the
readerInvoiceobject is not null, somehow code works but still getting the error.Jayraj ChhayaRank #304 · 8.2K reputationPosted May 30, 2024, 10:02 AM EDT
Hi Garima Bansal,
Please share screen shot or error message so I can help you and give some suggestion accrodingly.
Garima BansalRank #1,555 · 1K reputationPosted May 31, 2024, 12:49 AM EDT
Jayraj Chhaya Sir,
here is the screeshot where i am getting error
Jignesh KumarRank #91 · 22.9K reputationPosted May 31, 2024, 3:11 AM EDT
Hello Garima,
You can skip pdf version or you can assign as below,
using (PdfStamper stamper = new PdfStamper(readerInvoice, ms1, readerInvoice.PdfVersion, true)) { for (int page = 1; page <= n; page++) { PdfContentByte over = stamper.GetOverContent(page); over.AddImage(img); } stamper.Close(); }If you wish to skip version then use below,
using (PdfStamper stamper = new PdfStamper(readerInvoice, ms1)) { for (int page = 1; page <= n; page++) { PdfContentByte over = stamper.GetOverContent(page); over.AddImage(img); } stamper.Close(); }Amit MohantyRank #112 · 20K reputationPosted May 31, 2024, 6:19 AM EDT
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.
PdfReader readerInvoice = new PdfReader(image);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.
if (model.Pdfdoc != null) { byte[] 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); iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/Watermark.png")); img.SetAbsolutePosition(0, 80f); img.ScaleAbsolute(600f, 600f); using (MemoryStream pdfStream = new MemoryStream(image)) { PdfReader readerInvoice = new PdfReader(pdfStream); int n = readerInvoice.NumberOfPages; using (MemoryStream ms1 = new MemoryStream()) { using (PdfStamper stamper = new PdfStamper(readerInvoice, ms1, '\0', true)) { for (int page = 1; page <= n; page++) { PdfContentByte over = stamper.GetOverContent(page); over.AddImage(img); } stamper.Close(); } byte[] bytesInStream = ms1.ToArray(); ms1.Close(); image = bytesInStream; } if (model.filetype.Equals("application/pdf")) { model.Pdf = image; } } }