File.Close() casuses "Object reference not set to an instance of an object."

I am receiveing a "Object reference not set to an instance of an object." error when calling the Close() method on the PDF object. Here is my code...

public void InsertImageOnPDF(FileInfo pdfFileInfo, Bitmap barcodeImage)

{

FileStream fs = new FileStream(pdfFileInfo.FullName,FileMode.Open, FileAccess.ReadWrite );

//Instantiate a Pdf object

Pdf pdf1 = new Pdf(fs);

//Create a new section in the Pdf document

Aspose.Pdf.Section sec1 = new Aspose.Pdf.Section(pdf1);

//Add the section in the sections collection of the Pdf document

pdf1.Sections.Add(sec1);

//Create an image object

Aspose.Pdf.Image image1 = Aspose.Pdf.Image.FromSystemImage(barcodeImage);

//Add the image into paragraphs collection of the section

sec1.Paragraphs.Add(image1);

image1.ImageInfo.ImageFileType = ImageFileType.Bmp;

//Set desired the image scale

image1.ImageScale = 0.5F;

pdf1.Close();

}

I am passing in a valid reference to an exsiting PDF file and a Bitmap image generated using the following code that leverages Aspose.Barcode.

public Bitmap GenerateBarcodeImage()

{

//Instantiate BarCodeBuilder object

BarCodeBuilder bb = new BarCodeBuilder();

//Set the Code text for the barcode

bb.CodeText = "1234567";

//Set the symbology type to Code39Standard

bb.SymbologyType = Symbology.DataMatrix;

//Save the image to your system and set its image format to Jpeg

//bb.Save(m_DestDir + "barcode.jpg", ImageFormat.Jpeg);

return bb.BarCodeImage;

}

Can you please tell me know what I am doing wrong here?

Hello Michael,

Thanks for considering Aspose.

Aspose.Pdf is a component which is used to generate PDF documents from scratch, whereas if you need to edit/manipulate any existing PDF document, please try using Aspose.Pdf.Kit.

I'm not certain about your requirement. Can you please share some details regarding the functionality that you are trying to perform.

Just in case, if you are trying to add an image to any existing PDF document, please visit the following link for related information Add Watermark

Hi Michael,

Adding more to Nayyer’s reply, I would like to inform you that you can use AddImage method to add an image in an existing PDF file. You can also read the following article on image manipulation:

Manipulating Images in an Existing PDF File



I hope this helps. If you need any further assistance, please do let us know.
Regards,