logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
eagleview  
#1 Posted : Monday, February 1, 2016 1:43:06 PM(UTC)
eagleview

Rank: Member

Groups: Registered
Joined: 1/28/2016(UTC)
Posts: 17
United States

Thanks: 3 times
Hi,

I purchased the full version and have the package installed in a VS project. I've created the instance with my key and it runs with no errors. I'm not trying to OCR a PDF that is an Image PDF (no searchable text). The examples involve JPG, Tiffs and other pure image files. How do I use the product with an unsearchable image type PDF? Thanks much for any help.
eagleview  
#2 Posted : Monday, February 1, 2016 4:12:27 PM(UTC)
eagleview

Rank: Member

Groups: Registered
Joined: 1/28/2016(UTC)
Posts: 17
United States

Thanks: 3 times
The below VS C# code is not compiling. I'm really in a spot an would like any help that anyone can provide.

The code only works when an image file (e.g. test.jpg) is used. I have code that returns the page_object which is an image. I can even
click through the var and see the information in the Visual Studio IDE. But when I attempt to use image_object in the GetTextFromImage, VS gives errors
like:

Error CS1061 'PdfPageObject' does not contain a definition for 'GetImage' and no extension method 'GetImage' accepting a first argument of type 'PdfPageObject' could be found (are you missing a using directive or an assembly reference?)

Error CS1503 Argument 1: cannot convert from 'Patagames.Pdf.Net.PdfImageObject' to 'string'

ANY help is greatly appreciated as I've spent a few hundred dollars on paid versions of Patagames PDFium and Tesseract and cannot show my boss a working product.

I do not see why it is complaining about converting the ImageObject to a string when the overloaded function allows for it to be of type image, not just a string for a filename. Any idea what I am missing? Thanks!


if (page_object.ObjectType == PageObjectTypes.PDFPAGE_IMAGE)
{
api.Init(Languages.English);

PdfImageObject image_object;

image_object = (PdfImageObject) page_object.GetImage();

string plainText = api.GetTextFromImage(image_object);
}
Paul Rayman  
#3 Posted : Monday, February 1, 2016 5:19:00 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
You need to use Pdfium.Net SDK to render pages into images and then use this images with Tesseract

Code:

public void RenderPage()
{
    //Initialize the SDK library
    //You have to call this function before you can call any PDF processing functions.
    PdfCommon.Initialize();

    //Open and load a PDF document from a file.
    using (var doc = PdfDocument.Load(@"c:\test001.pdf"))
    {
        int i = 0;
        //Iterate all pages;
        foreach (var page in doc.Pages)
        {
            //Gets page width and height measured in points. One point is 1/72 inch (around 0.3528 mm)
            int width = (int)page.Width;
            int height = (int)page.Height;

            //Create a bitmap
            using (var bmp = new PdfBitmap(width, height, true))
            {
                //Fill background
                bmp.FillRect(0, 0, width, height, Color.White);
                //Render contents in a page to a drawing surface specified by a coordinate pair, a width, and a height.
                page.Render(bmp, 0, 0, width, height,
                    Patagames.Pdf.Enums.PageRotate.Normal,
                    Patagames.Pdf.Enums.RenderFlags.FPDF_LCD_TEXT);
                //Get .Net image and save it into file
                bmp.Image.Save(string.Format(@"c:\test001_pdf_page_{0}.png", i++), ImageFormat.Png);
            }
        }
    }

}

Edited by user Thursday, March 31, 2016 2:26:06 AM(UTC)  | Reason: Not specified

eagleview  
#4 Posted : Monday, February 1, 2016 5:43:35 PM(UTC)
eagleview

Rank: Member

Groups: Registered
Joined: 1/28/2016(UTC)
Posts: 17
United States

Thanks: 3 times
Thank you for the info. But I'm confused. The PDF I need to OCR is already saved. I've gone thru with PDFViewer and extracted the image object. One of Tesseract's GetTextFromImage(bitmap). When I view the object in the debugger, it shows that the page object is of type Image and that it contains bitmap data. Can I not use this method without saving out the data again to another file? This is not making any sense that I would have an image pdf and then have to go thru each page and create another image file of type PNG or JPG in order to OCR the original file. Am I mistaken in how GetTextFromImage works?
Paul Rayman  
#5 Posted : Tuesday, February 2, 2016 2:11:12 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Originally Posted by: eagleview Go to Quoted Post
Thank you for the info. But I'm confused. The PDF I need to OCR is already saved.


Please look at this example:
Code:

PdfCommon.Initialize();
var ocr = OcrApi.Create();
ocr.Init(Languages.English);

double scaleFactor = 3;

using (var doc = PdfDocument.Load(@"d:\1\1.pdf"))
{
	foreach(var page in doc.Pages)
	{
		int width = (int)(page.Width * scaleFactor);
		int height = (int)(page.Height * scaleFactor);
		using (var bitmap = new PdfBitmap(width, height, true))
		{
			//Fill background
			bmp.FillRect(0, 0, width, height, Color.White);
			//Render page into image
			page.Render(bitmap, 0, 0, width, height, PageRotate.Normal,  RenderFlags.FPDF_LCD_TEXT);
			//OCR image
			string text = ocr.GetTextFromImage(bitmap.Image as Bitmap);
		}
	}
}

Edited by user Monday, May 22, 2017 6:07:00 AM(UTC)  | Reason: Not specified

thanks 1 user thanked Paul Rayman for this useful post.
eagleview on 2/2/2016(UTC)
eagleview  
#6 Posted : Tuesday, February 2, 2016 5:40:58 AM(UTC)
eagleview

Rank: Member

Groups: Registered
Joined: 1/28/2016(UTC)
Posts: 17
United States

Thanks: 3 times
THANKS! It works PERFECTLY!! I now understand where my confusion resulted: I was trying to work on a PDFViewers page object (image type) directly using as a bitmap and I did not understand that it needed to be rendered. In my earlier version I was trying to OCR page_image_object.Bitmap and page_image_object.Bitmap.Image directly, without rendering.
Thank you very much for the help!!!
Paul Rayman  
#7 Posted : Tuesday, February 2, 2016 7:07:12 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
You welcome
eagleview  
#8 Posted : Tuesday, February 2, 2016 1:57:53 PM(UTC)
eagleview

Rank: Member

Groups: Registered
Joined: 1/28/2016(UTC)
Posts: 17
United States

Thanks: 3 times
Hi Paul,

Making excellent progress. I have two very quick questions, though, on the example code below. Your suggested text uses a scaling factor and I was curious as to what the use of it is. In the PDFium User info there is an example of render but without scaling the height and width. The other question is the term PDF User Space. Does user space mean Points (1/72)? I've tried a number of different PDFs and all have a width of 612 and height of 792 no matter where the PDF comes from...I've also changed the PDFViewer and ScrollViewer sizes in the XMAL file (designer) and still I have same size of 612 and 792. My goal is to figure out the different coordinate systems in the PDF & WPF controls. I'm figuring that the scaling factor is for the 3 bytes per pixel (but not sure). Thanks!


int width = (int)(page.Width * scaleFactor);
int height = (int)(page.Height * scaleFactor);
using (var bitmap = new PdfBitmap(width, height, true))
{
page.Render(bitmap, 0, 0, width, height, PageRotate.Normal, RenderFlags.FPDF_LCD_TEXT);
string text = ocr.GetTextFromImage(bitmap.Image as Bitmap);
}

Edited by moderator Thursday, March 31, 2016 2:27:28 AM(UTC)  | Reason: Added info about height and width

Paul Rayman  
#9 Posted : Friday, February 5, 2016 3:16:58 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Tesseract have a problems with recognizing text the height of line of which is less than 20 pixels. So increasing the image size we can achieve the best results recognizers. ScaleFactor used for this purpose.
eagleview  
#10 Posted : Friday, February 5, 2016 7:50:49 AM(UTC)
eagleview

Rank: Member

Groups: Registered
Joined: 1/28/2016(UTC)
Posts: 17
United States

Thanks: 3 times
Understand - thanks, Paul.
Paul Rayman  
#11 Posted : Friday, February 5, 2016 8:03:31 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
you welcome
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.