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

Notification

Icon
Error

Options
Go to last post Go to first unread
Paul Rayman  
#1 Posted : 9 years ago
Paul Rayman

Rank: Administration

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

Thanks: 8 times
Was thanked: 130 time(s) in 127 post(s)
Question:
How to create a PDF from multiple scanned images?

Answer:
Please look at code below. The detailed explanations of this code can be found here: How to Create a PDF from Multiple Scanned Pages in Your C# Code

Code:

int pageIndex = 0;
PdfCommon.Initialize();
using (var doc = PdfDocument.CreateNew())
{
    var files = System.IO.Directory.GetFiles(@"SourceImages", "*.*", System.IO.SearchOption.AllDirectories);
    foreach (var file in files)
    {
        using (var image = Bitmap.FromFile(file, true) as Bitmap)
        {
		var pdfBitmap = CreateBitmap(image);
		var imageObject = PdfImageObject.Create(doc);
		imageObject.SetBitmap(pdfBitmap);
	 
		var size = CalculateSize(pdfBitmap.Width, pdfBitmap.Height, image.HorizontalResolution, image.VerticalResolution);

		doc.Pages.InsertPageAt(pageIndex, size);
		doc.Pages[pageIndex].PageObjects.InsertObject(imageObject);
 
		imageObject.SetMatrix(size.Width, 0, 0, size.Height, 0, 0);
 
		doc.Pages[pageIndex].GenerateContent();
		pageIndex++;
        }
    }
    doc.Save(@"saved.pdf", SaveFlags.NoIncremental);
}


Code:

private static PdfBitmap CreateBitmap(Bitmap image)
{
    BitmapFormats pdfFormat;
    int[] palette;
    GetPdfFormat(image, out pdfFormat, out palette);
 
    var lockInfo = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);
    var pdfBitmap = new PdfBitmap(image.Width, image.Height, pdfFormat, lockInfo.Scan0, lockInfo.Stride);
    image.UnlockBits(lockInfo);
 
    if (palette != null)
	pdfBitmap.Palette = palette;
    return pdfBitmap;
}
 
public static void GetPdfFormat(Bitmap image, out BitmapFormats pdfFormat, out int[] palette)
{
    palette = null;
    switch (image.PixelFormat)
    {
	case PixelFormat.Format1bppIndexed:
	    pdfFormat = BitmapFormats.FXDIB_1bppRgb;
	    palette = GetPalette(image);
	    break;
	case PixelFormat.Format8bppIndexed:
	    pdfFormat = BitmapFormats.FXDIB_8bppRgb;
	    palette = GetPalette(image);
	    break;
	case PixelFormat.Format24bppRgb:
	    pdfFormat = BitmapFormats.FXDIB_Rgb;
	    break;
	case PixelFormat.Format32bppArgb:
	case PixelFormat.Format32bppPArgb:
	    pdfFormat = BitmapFormats.FXDIB_Argb;
	    break;
	case PixelFormat.Format32bppRgb:
	    pdfFormat = BitmapFormats.FXDIB_Rgb32;
	    break;
	default:
	    throw new Exception("Unsupported Image Format");
	}
    }
 
public static int[] GetPalette(Bitmap image)
{
    var ret = new int[image.Palette.Entries.Length];
    for (int i = 0; i < ret.Length; i++)
	ret[i] = ((Color)image.Palette.Entries.GetValue(i)).ToArgb();
    return ret;
}
 
private static SizeF CalculateSize(int width, int height, float dpiX, float dpiY)
{
    return new SizeF()
    {
	Width = width * 72 / dpiX,
	Height = height * 72 / dpiY
    };
}

Edited by user 9 years ago  | Reason: Not specified

Akshara  
#2 Posted : 8 years ago
Akshara

Rank: Member

Groups: Registered
Joined: 2/7/2017(UTC)
Posts: 12
India
Location: Pune

hey hi,

I wanted to ask one Question, When we Create a PDF using the above code we are not able to use that PDF generated for OCR purpose.
Can you please tell some solution to make that PDF to be used for OCR purpose.
I have tried your solution for converting the generated PDF to Searchable PDF but I am not able to convert it.I am getting an error "Unexpected error code".
I have used the following code to convert the the PDF (generated using above code) into searchable PDF-

Code:

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

                using (var renderer = OcrPdfRenderer.Create(@"d:\0\output_pdf_file", "tessdata\\"))
                {
                    renderer.BeginDocument("document title");

                    using (var doc = PdfDocument.Load(Path))
                    {
                        foreach (var page in doc.Pages)
                        {
                            int width = (int)(page.Width / 72.0 * 96);
                            int height = (int)(page.Height / 72.0 * 96);
                            using (var bitmap = new PdfBitmap(width, height, true))
                            {
                                bitmap.FillRect(0, 0, width, height, Color.White);
                                page.Render(bitmap, 0, 0, width, height, PageRotate.Normal, RenderFlags.FPDF_LCD_TEXT);
                                using (var pix = OcrPix.FromBitmap(bitmap.Image as Bitmap))
                                {
                                    ocr.ProcessPage(pix, null, 0, renderer);
                                }
                            }
                        }
                    }
                    renderer.EndDocument();
                }
            }



Please do reply, it is really very argent.

Thanks in advance.

Edited by moderator 8 years ago  | Reason: Not specified

Paul Rayman  
#3 Posted : 8 years ago
Paul Rayman

Rank: Administration

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

Thanks: 8 times
Was thanked: 130 time(s) in 127 post(s)
Could you please tell what exactly instruction generate the exception?

Edited by user 8 years ago  | Reason: Not specified

Akshara  
#4 Posted : 8 years ago
Akshara

Rank: Member

Groups: Registered
Joined: 2/7/2017(UTC)
Posts: 12
India
Location: Pune

The bellow line gives me an error "Unexpected error code"



ocr.ProcessPage(pix, null, 0, renderer);
Paul Rayman  
#5 Posted : 8 years ago
Paul Rayman

Rank: Administration

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

Thanks: 8 times
Was thanked: 130 time(s) in 127 post(s)
Please send your solution to support@patagames.com
Support team will check it.
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.