C#:使用iTextSharp

时间:2015-09-08 14:43:58

标签: c# image itextsharp

您好我试图从包含某些数据的图像的扫描文档或pdf文件中提取数据。对于PdF文件,我已成功读取数据,但是从包含图片的图像或pdf中,我失败了。 我已将图像文件转换为pdf但没有结果。 这是我的小代码:

 public bool ConvertImageToPdf(string ImageIn, string PDFOut)
        {

            try
            {
                iTextSharp.text.Document doc1 = new iTextSharp.text.Document();
                byte[] b = File.ReadAllBytes(ImageIn);
                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(b);
                using (FileStream fs = new FileStream(PDFOut, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    using (Document doc = new Document(image))
                    {
                        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                        {

                            Paragraph paragraph = new Paragraph("");
                            doc.Open();

                            image.SetAbsolutePosition(0, 0);
                            writer.DirectContent.AddImage(image);
                            doc.Add(paragraph); // add paragraph to the document  
                            doc.Add(image); //add an image to the created pdf document
                            doc.Close();
                            return true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }



        }

为了提取数据,我使用这段代码:

   for (int page = 1; page <= reader.NumberOfPages; page++)
                {
                    outFile.Write(ExtractTextFromPDFBytes(reader.GetPageContent(page)) + " ");
}

ExtractTextFromPDFBytes(byte []输入)在codeProject sample

上类似

0 个答案:

没有答案