Orientation of PDF when viewing with Ghostscript - c#

My application shows PDFs in a picture box by using Ghostscript. The PDFs I use are scanned images with a text layer. created by the OCR function from Acrobat Pro. This OCR function automatically sets the orientation according to the direction of the text. When the page is displayed in the picture box this information is lost. It just displays every page in portrait mode.
Is there a way for Ghostscript to access this property of the PDF and display it in the correct orientation in the picture box?
public Form1()
{
InitializeComponent();
_viewer = new GhostscriptViewer();
_viewer.DisplaySize += new GhostscriptViewerViewEventHandler(_viewer_DisplaySize);
_viewer.DisplayPage += new GhostscriptViewerViewEventHandler(_viewer_DisplayPage);
NumberOfPagesToExtract = 1;
_viewer.Open("NoFile.pdf", _gsVersion, true);
}
void _viewer_DisplaySize(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Image = e.Image;
}
void _viewer_DisplayPage(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Invalidate();
pictureBox1.Update();
currentPageNumber = _viewer.CurrentPageNumber;
LastPageNumber = _viewer.LastPageNumber;
lblTotalNmbPages.Text = " / " + LastPageNumber.ToString();
txtCurrentPgNmbr.Text = currentPageNumber.ToString();
}
The code to open the file:
private void btnOpenPdfGhostScript_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open PDF file";
ofd.Filter = "PDF, PS, EPS files|*.pdf;*.ps;*.eps";
if (ofd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
btnCLose_Click(this, null);
_viewer.Open(ofd.FileName, _gsVersion, true);
currentFilePath = ofd.FileName;
currentPageNumber = _viewer.CurrentPageNumber;
LastPageNumber = _viewer.LastPageNumber;
lblCurrentFIle.Text = ofd.FileName; //System.IO.Path.GetFileName(ofd.FileName);
if (backgroundWorker1.IsBusy != true) backgroundWorker1.RunWorkerAsync();
}
currentPageNumber = 1;
progressBar1.Value = 0;
}

I'm not sure if I should answer my own questions, but the problem is solved by updating to version 1.1.8. Thanks to HABJAN. Thanks very much.

The first thing to do is to use Ghostscript directly from the command line, rather than in an application. The main reason is that you can then supply a GS command line which other people can use, experiment with and comment on.
I can't see from your code how Ghostscript is invoked.
Ghostscript should, in general, honour the /MediaBox (and optionally /CropBox etc.) as well as the /Rotate attribute of pages.
But without a sample file and command line, I can't give you any suggestions.

Related

Using Tesseract OCR in C#

I have trying to deploy tesseract for reading the clipboard image through the code below in a C# window.form. But, a black commandline window appears and nothing happens.
private void b1_Click(object sender, EventArgs e)
{
if (ofd1.ShowDialog() == DialogResult.OK)
{
var img = new Bitmap(ofd1.FileName);
var ocr = new TessBaseAPI("./tessdata", "eng", OcrEngineMode.DEFAULT);
var page = ocr.SetImage(img);
tb1.Text = page.ToString();
}
}
The error it gives is cannot convert from 'System.Drawing.Bitmap' to 'Leptonica.Pix' hope this can be improved.
Instead of creating a bitmap, try using a Pix object like:
var img = Tesseract.Pix.LoadFromFile(ofd1.FileName)

Open an existing text file in WPF and C#

I have a program that enables a user to search text files in an open file dialog. The user is then able to open an existing text file they choose and edit it. However, my problem is that when they file opens it appears blank. What am I missing?
private void Open_Click(object sender, RoutedEventArgs e)
{
TextBox openText = new TextBox();
var OpenFile = new Microsoft.Win32.OpenFileDialog();
Nullable<bool> Success = OpenFile.ShowDialog();
OpenFile.DefaultExt = ".txt";
OpenFile.Filter = "Text documents (.txt)|*.txt";
if (Success.HasValue && Success.Value)
{
openText.Text = OpenFile.FileName;
}
else
{
//cannot open file
}
}
Replace this:
openText.Text = OpenFile.FileName;
with this:
openText.Text = System.IO.File.ReadAllText(OpenFile.FileName);
Use File.ReadAllText()
openText.Text = File.ReadAllText(OpenFile.FileName);

Saving image from "pictureBox1" to a file with a click on a button in C#

I found this code on here, and I want to save the image I get in my "pictureBox1" with a button like under, how can I implement these together?
I have the picture showing in the pictureBox1, I want to click a button and be able to store the picture on my PC.
private void button1_Click(object sender, EventArgs e)
This is the code to save image:
public static void SaveImageCapture(System.Drawing.Image image)
SaveFileDialog s = new SaveFileDialog();
s.FileName = "Image";// Default file name
s.DefaultExt = ".Jpg";// Default file extension
s.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension
s.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
s.RestoreDirectory = true;
if (s.ShowDialog() == DialogResult.OK)
{
// Save Image
string filename = s.FileName;
(System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.Create))
{
image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
fstream.Close();
I am also not 100% sure i understand what you mean. but here is a 2 lines of codes, showing the simplest way of loading and saving an image into a picturebox.
// 1. Load a picture into the picturebox
PictureBox pic = new PictureBox() { Image = Image.FromFile("SomeFile.jpg") };
// 2. Save it to a file again
pic.Image.Save("SomeFilePath.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
I fixed it by doing this:
private void btnSave_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save(#"C:\Temp\test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
}

Confusion about OpenFileDialog

I am following this tutorial and downloaded source code to practice, and it works. The problem occurs when I rewrite the code: just one image is added instead of all the selected images. What am I doing wrong here?
private void button1_Click(object sender, EventArgs e)
{
ofd.Filter = "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
"All files (*.*)|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
foreach (string name in ofd.FileNames)
{
PictureBox imageControl = new PictureBox();
imageControl.Width = 100;
imageControl.Height = 100;
Image.GetThumbnailImageAbort CallBck = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap myBitmap = new Bitmap(name);
Image img = myBitmap.GetThumbnailImage(97, 97, CallBck, IntPtr.Zero);
imageControl.Image = img;
panel1.Controls.Add(imageControl);
}
}
}
I'll bet they are all being added but they're just all going on top of each other at location (0,0) in the panel (you should step through your code to check this though).
The solution: Either manually specify a location for each new PictureBox, or use a layout control such as a FlowLayoutPanel.

OCR reading using C#

I have a project which is to read character in a captured image but I'm stuck at the button which is to scan image. I ended up tesseract dll in c#, but I don't know how can I code it. I'm a newbie to this programming.
private void Browse_Click(object sender, EventArgs e)
{
//FileInfo fi = new FileInfo(string.Format(#"C:\Documents and Settings\JOrce0201610\My Documents\Visual Studio 2005\Projects\OCR Reader\{0}", imageName));
OpenFileDialog fi = new OpenFileDialog();
fi.InitialDirectory = #"C:\\Documents and Settings\JOrce0201610\My Documents\Visual Studio 2005\Projects\OCR Reader\Card";
fi.Filter = "BMP Image|*.bmp";
fi.FilterIndex = 2;
fi.RestoreDirectory = true;
if (fi.ShowDialog() == DialogResult.OK)
{
//image file path
textBox1.Text = fi.FileName;
//display image in picture box
pictureBox1.Image = new Bitmap(fi.FileName);
}
}
private void Scan_Click(object sender, EventArgs e)
{
Bitmap temp = source.Clone() as Bitmap; //Clone image to keep original image
FiltersSequence seq = new FiltersSequence();
seq.Add(Grayscale.CommonAlgorithms.BT709); //First add GrayScaling filter
seq.Add(new OtsuThreshold()); //Then add binarization(thresholding) filter
temp = seq.Apply(source); // Apply filters on source image
If you are a 'newbie' to programming, OCR is not the best place to start. The best I can suggest is that you use a webservice or existing library that can do this for you.
Microsoft has project Hawaii, Hawaii has an OCR service which is quite easy to use.

Categories

Resources