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.
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
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:
Now I used AzureComputerVision to get the text of image. The analyaz text of computer will look like below
Snippet:
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
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?
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
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
|
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. 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: |