In debugging the code stuck at the place where image put in pdf

we have already using this,
Please check line no . 169
please reply at your earliest because we have compact timeline to implement it in our system and deploy
Thanks in advance

@Atifats
I will look into the code you attached.
Please wait.

@Atifats
I have brought the code you attached to a running state (as a separate fragment in a console application).

         try
         {
             string extractedText = "";
             string strImageName = dataDir + "arabicimage.png";

             int iFileLength = (int)(new FileInfo(strImageName).Length);
             var inputBuffer = new byte[iFileLength];
             System.IO.Stream inputStream = new FileStream(strImageName, FileMode.Open);

             inputStream.Read(inputBuffer, 0, iFileLength);

             using (var pdfStream = new MemoryStream(inputBuffer))
             {
                 var pdfDocument = new Aspose.Pdf.Document(pdfStream);
                 string outputPath = dataDir + "output.pdf";
                 string imagePath = strImageName;// Server.MapPath("../../FileUpload/PDF/jamalDahbaly.png"); // Path to your image file
                 try
                 {
                     using (var originalImageStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
                     {
                         using (var compressedStream = new MemoryStream())
                         {
                             // Compress and resize the image
                             using (var bitmap = new Bitmap(originalImageStream))
                             {
                                 var resizedBitmap = new Bitmap(bitmap, new Size(100, 50)); // Adjust size as needed
                                 resizedBitmap.Save(compressedStream, ImageFormat.Png); // Save resized image to stream
                             }

                             compressedStream.Position = 0; // Reset stream position

                             Aspose.Pdf.Page page = pdfDocument.Pages[1];

                             var table = new Aspose.Pdf.Table
                             {
                                 ColumnWidths = "90",
                                 DefaultCellPadding = new Aspose.Pdf.MarginInfo(0, 0, 0, 0)
                             };

                             Aspose.Pdf.Row row = table.Rows.Add();
                             var pdfImage = new Aspose.Pdf.Image
                             {
                                 ImageStream = compressedStream, // Use the compressed stream
                                 FixWidth = 100,
                                 FixHeight = 50
                             };

                             row.Cells.Add(new Aspose.Pdf.Cell());
                             row.Cells[0].Paragraphs.Add(pdfImage);

                             page.Paragraphs.Add(table);

                             table.Margin.Left = 10;
                             table.Margin.Top = 920;
                             pdfDocument.OptimizeResources(new Aspose.Pdf.Optimization.OptimizationOptions()
                             {
                                 CompressImages = true,
                                 ImageQuality = 50 // Reduce quality to speed up processing
                             });
                             try
                             {
                                 using (var outputStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
                                 {
                                     pdfDocument.Save(outputStream);
                                 }

                                 inputBuffer = File.ReadAllBytes(outputPath);
                             }
                             catch (Exception ex)
                             {
                                 Console.WriteLine("An error occurred: {" + ex.Message + "}");
                             }
                         }
                     }
                 }
                 catch (Exception ex)
                 {
                     // Log the error
                     Console.WriteLine("An error occurred: {" + ex.Message + "}");
                     Console.WriteLine("Stack Trace: {" + ex.StackTrace + "}");

                     // Optionally show the error to the user (use MessageBox for desktop apps or logging for web apps)
                     // MessageBox.Show($"An error occurred: {ex.Message}");

                     // Handle any cleanup or fallback
                 }

             }
         }
         catch (Exception ex)
         {

         }

However, on the line that I wrote about in the previous message, an exception occurs (I also wrote the reason) - and I do not understand how this can work for you.
Please look at the code - perhaps I have corrected something incorrectly.

We are not using the file path for the PDF in our code; instead, we are using HttpPostedFile for file upload. Please provide a solution for working with HttpPostedFile.

@Atifats
Describe in words what you want to do?

we have store pdf file in database ,and we are using that pdf .

@Atifats
At the input you have an image, you want to deal with its data read into the stream.
You compress it, bring it to the desired size and insert the image into a certain place using Aspose.Pdf.Table.
You save the resulting pdf and at this point you have a hang.
Did I understand your intentions correctly?

We are doing the following steps:

step 1 we are uploading pdf file and saving into database
step 2 we are fetching the file from database
step 3 then we fetch in Bytes[] and attach barcode image.

its hanging for unlimited time at this function Document.save

Your Prompt Reply is required.

@Atifats
I reproduced the problem. However, I still slightly corrected the code.

static private void AddImageToPdf(Document pdfDocument, string imagePath)
{
    string outputPath = dataDir + "pictToPdf_out.pdf";

    using (var originalImageStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
    {
        using (var compressedStream = new MemoryStream())
        {
            // Compress and resize the image
            using (var bitmap = new Bitmap(originalImageStream))
            {
                var resizedBitmap = new Bitmap(bitmap, new Size(100, 50)); // Adjust size as needed
                resizedBitmap.Save(compressedStream, ImageFormat.Png); // Save resized image to stream                        
            }

            compressedStream.Position = 0; // Reset stream position

            var table = new Aspose.Pdf.Table
            {
                ColumnWidths = "90",
                DefaultCellPadding = new Aspose.Pdf.MarginInfo(0, 0, 0, 0)
            };

            Aspose.Pdf.Row row = table.Rows.Add();
            var pdfImage = new Aspose.Pdf.Image
            {
                ImageStream = compressedStream, // Use the compressed stream
                FixWidth = 100,
                FixHeight = 50
            };

            row.Cells.Add(new Aspose.Pdf.Cell());
            row.Cells[0].Paragraphs.Add(pdfImage);

            Page page = pdfDocument.Pages[1];
            page.Paragraphs.Add(table);

            table.Margin.Left = 10;
            table.Margin.Top = 220;  // <----  Value changed, with 920 hangs

            pdfDocument.OptimizeResources(new Aspose.Pdf.Optimization.OptimizationOptions()
            {
                CompressImages = true,
                ImageQuality = 50 // Reduce quality to speed up processing
            });
            try
            {
                using (var outputStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
                {
                    pdfDocument.Save(outputStream);
                }
                //inputBuffer = File.ReadAllBytes(outputPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: {" + ex.Message + "}");
            }
        }
    }
}

The reason in your case is the line:

            page.Paragraphs.Add(table);

            table.Margin.Left = 10;
            table.Margin.Top = 220;  // <----  Value changed, with 920 hangs

            pdfDocument.OptimizeResources(new Aspose.Pdf.Optimization.OptimizationOptions()

If you change its value, the freeze does not occur.

I will create a task for the development team to fix this error.

@Atifats
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-59093

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Please check this video ,your code is not working.

Dear Support,

Kindly provide us prompt support to resolve this issue we are already delayed for deployment.

@Atifats , we’ll investigate this issue and provide you answer shortly

Its been more than a week we still not get any solution

@Atifats
Please attach a pdf document with which this happens. I checked for a newly created empty document.
The problem occurs when the height of the document pages is not enough to insert the element. The final formation of the document is performed when calling the Save() method - that is why it hangs.

We upload a PDF file and save it to the database. Later, the file is retrieved from the database, converted into a byte array, and a barcode image is attached to it. However, the process hangs indefinitely during the Document.Save operation.
pdf .document
arab sea (4).pdf (56.1 KB)

@Atifats
I checked with the document you attached - no freezing occurs.

I provide the code together with the calling code

// Imitation of "the file is retrieved from the database, converted into a byte array"
byte[] pdfBytes = File.ReadAllBytes(dataDir + "arab sea (4).pdf");
using var ms = new MemoryStream(pdfBytes);

// "a barcode image is attached to it"
using (var doc = new Document(ms))
{
    AddImageToPdf(doc, dataDir + "arabicimage.png", dataDir + "pictToPdf_out.pdf");
}

static private void AddImageToPdf(Document pdfDocument, string imagePath, string outputPath)
{
    using (var originalImageStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
    {
        using (var compressedStream = new MemoryStream())
        {
            // Compress and resize the image
            using (var bitmap = new Bitmap(originalImageStream))
            {
                var resizedBitmap = new Bitmap(bitmap, new Size(100, 50)); // Adjust size as needed
                resizedBitmap.Save(compressedStream, ImageFormat.Png); // Save resized image to stream                        
            }

            compressedStream.Position = 0; // Reset stream position

            var table = new Aspose.Pdf.Table
            {
                ColumnWidths = "90",
                DefaultCellPadding = new Aspose.Pdf.MarginInfo(0, 0, 0, 0)
            };

            Aspose.Pdf.Row row = table.Rows.Add();
            var pdfImage = new Aspose.Pdf.Image
            {
                ImageStream = compressedStream, // Use the compressed stream
                FixWidth = 100,
                FixHeight = 50
            };

            row.Cells.Add(new Aspose.Pdf.Cell());
            row.Cells[0].Paragraphs.Add(pdfImage);

            Page page = pdfDocument.Pages[1];
            page.Paragraphs.Add(table);

            table.Margin.Left = 10;
            table.Margin.Top = 220;  // <----  Value changed, with 920 hangs

            pdfDocument.OptimizeResources(new Aspose.Pdf.Optimization.OptimizationOptions()
            {
                CompressImages = true,
                ImageQuality = 50 // Reduce quality to speed up processing
            });
            try
            {
                using (var outputStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
                {
                    pdfDocument.Save(outputStream);
                }
                //inputBuffer = File.ReadAllBytes(outputPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: {" + ex.Message + "}");
            }
        }
    }
}

(video, unfortunately I couldn’t download it)

Will Aspose work offline?

@Atifats
If you mean whether it is possible to use the library without internet access, then with a usually (not metered) license - yes. (Of course, the library itself must be downloaded first).

The issues you have found earlier (filed as PDFNET-59093) have been fixed in Aspose.PDF for .NET 25.2.