Is there any possibility to do overlay on pdf file that contains image?

Hi, I have created one console app using .net core and trying to do overlay on pdf file that contains text inside  image.
Please find sample pdf below.
Sample Pdf

So is there any possiblity to peform overlay on top of the image?
Any suggestion/sample code would be really great.



5 Replies

AG Anantha Gokula Raman Jeyaraman Syncfusion Team May 10, 2024 03:20 PM UTC

Hi,

Kindly use the following KB documentation to overlay on PDF files, How to superimpose PDF pages using C# and VB.NET? | Syncfusion

Regards,

Anantha Gokula Raman J



MN Mohd Nasir replied to Anantha Gokula Raman Jeyaraman May 22, 2024 01:51 PM UTC

My poblem is bit different.
Lets say I have one pdf document which contains images in that pdf we cant seatch text right that is pdf behavior.

Snippet:
Image_8009_1716287801677

Now I used AzureComputerVision to get the text of image. The analyaz text of computer will look like below
Snippet:
Image_5399_1716287932480

Now I have to get  this text from this json and append on top of the my pdf character so that pdf ll look similar but we are able to search the text now.
Sample below
Image_1492_1716288177317

I tried to achive this functionality but its not overlaying on top of the character could you please help me on this.
Sample Code below.

//Method 1

private async Task<string> ProcessForTextOverlayAsync1(PdfModel pdfModel, CancellationTokenSource cancellationTokenSource)
{
    pdfModel.LasteOverlayPage = 0;
    string pdfDocName = $"{pdfModel.DocumentId}-{pdfModel.DocumentDetailsId}";


    byte[] sourceFile = System.IO.File.ReadAllBytes($"D:/Files/SyncFusionTest/PdfImage1Page_test.pdf");
    Stream stream = new MemoryStream(sourceFile);


    using (PdfLoadedDocument pdfDoc = new(stream))
    {


        foreach (string blobName in pdfModel.AnalyzeResultBlobNames)
        {
            await ProcessForOverlayAsync(pdfModel, pdfDoc, blobName);
        }


        await Save(pdfDoc, pdfDocName, cancellationTokenSource.Token);
    }
    return "";
}


//Method 2

public async Task ProcessForOverlayAsync(PdfModel pdfModel, PdfLoadedDocument pdfDoc, string blobName)
{
    try
    {
        ReadOperationResult readResult = await DownloadFromStorageAsync<ReadOperationResult>("");


        int pageCount = pdfDoc.Pages.Count;
        foreach (ReadResult page in readResult.AnalyzeResult.ReadResults)
        {
            PdfPageBase pdfPage = pdfDoc.Pages[pdfModel.LasteOverlayPage + page.Page - 1];
            if (pdfPage != null)
            {
                double pageHeight = pdfPage.Size.Height; // Get the height of the page
                page.Height = GetPageHeight(pdfModel.HasDefaultTextEnabled, page.Height, pageHeight);
                PdfGraphics graphics = pdfPage.Graphics;


                foreach (Line line in page.Lines)
                {
                    // Choose a suitable font (e.g., PdfStandardFont) or load a custom font from a file
                    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); // Example with a standard font
                                                                                     // If you have custom fonts, load them from file like this:
                                                                                     // PdfFont font = new PdfTrueTypeFont(new Font("Arial", 12), true);


                    // Use RGB color for PdfSolidBrush
                    PdfBrush brush = null;
                    if (pdfModel.HasDefaultTextEnabled)
                    {
                        brush = new PdfSolidBrush(new PdfColor(0, 0, 255)); // RGB color
                    }
                    else
                    {
                        brush = new PdfSolidBrush(new PdfColor(0, 0, 0)); // Black color
                    }


                    // Calculate the starting point to position text above existing text
                    PointF startPoint = new PointF((float)line.BoundingBox[0], (float)(line.BoundingBox[1])); // Adjust Y-coordinate


                    graphics.DrawString(line.Text, font, brush, startPoint);
                }
            }
        }
        pdfModel.LasteOverlayPage += readResult.AnalyzeResult.ReadResults.Count;
        response.Status = "Success";
    }
    catch (Exception ex)
    {
        throw;
    }
}

Please help me on this?


JT Jeyalakshmi Thangamarippandian Syncfusion Team May 22, 2024 02:17 PM UTC

Hi Mohd Nasir,

Upon further analysis of your code, we suggest setting the brush as transparent and using SetTransparency in pdf graphics. However, you can utilize the OCR process to convert the PDF document into selectable text..

Please refer to the UG documentation for further details:

Perform OCR on PDF and image files | Syncfusion

 

if (pdfPage != null)

 

{

   PdfGraphics graphics = pdfPage.Graphics;

   // Choose a suitable font (e.g., PdfStandardFont) or load a custom font from a file

 

   PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); // Example with a standard font

  // Use RGB color for PdfSolidBrush

 

PdfBrush brush = PdfBrushes.Transparent;

       // Calculate the starting point to position text above existing text

 

       PointF startPoint = new PointF(50, 50); // Adjust Y-coordinate

graphics.SetTransparency(0);

       graphics.DrawString("Text", font, brush, startPoint);}

 

Kindly try this on your end and let us know if you need any further assistance.


Regards,

Jeyalakshmi T



MN Mohd Nasir replied to Jeyalakshmi Thangamarippandian May 24, 2024 05:50 AM UTC

Thanks for the response. 

So on the code what you have given, is there any way to find out the co-ordinate of the text becuase i want to write it on top of the character.

One more thing on OCR process. I tried to use PerformOCR() method but it is giving accurate result, may I know the accuracy of OCR on syncfusion, I just create one request on this as well, please have a look below
https://www.syncfusion.com/forums/188367/what-is-the-accuracy-for-performing-ocr



KS Karmegam Seerangan Syncfusion Team May 27, 2024 12:23 PM UTC

is there any way to find out the co-ordinate of the text becuase i want to write it on top of the character.

 

 

Thank you for reaching out to Syncfusion support.
We have support to get the OCRed text and its bounds from a scanned PDF document by using the 
OCRLayoutResult Class. Please refer to the below documentation and sample.

Getting Layout results from the OCR processor

Sample:

Get-the-OCR'ed-text-and-its-bounds-from-an-input-PDF
 

Kindly try the above-provided solution and let us know the result.

may I know the accuracy of OCR on syncfusion

As of now, we don’t have support to get the accuracy of the OCR'ed output text. We have already logged this requirement as a feature request “Add support for getting the accuracy of the OCR output result“ in our library. At present, we do not have any plans to implement this feature.

 

Please use the below feedback link to track the status of the feature:

Add support for getting the accuracy of the OCR output result in ASP.NET Core | Feedback Portal (syncfusion.com)


Loader.
Up arrow icon