Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're able to use the latest version (5.1.3), the API to extract <code>FlateDecode</code> and other image types has been simplified using the <code>iTextSharp.text.pdf.parser</code> namespace. Basically you use a <a href="http://api.itextpdf.com/itext/com/itextpdf/text/pdf/parser/PdfReaderContentParser.html" rel="nofollow">PdfReaderContentParser</a> to help you parse the PDF document, then you implement the <a href="http://api.itextpdf.com/itext/com/itextpdf/text/pdf/parser/RenderListener.html" rel="nofollow">IRenderListener</a> interface specifically (in this case) to deal with images. Here's a working example HTTP handler:</p> <pre><code>&lt;%@ WebHandler Language="C#" Class="bmpExtract" %&gt; using System; using System.Collections.Generic; using System.IO; using System.Web; using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.pdf.parser; public class bmpExtract : IHttpHandler { public void ProcessRequest (HttpContext context) { HttpServerUtility Server = context.Server; HttpResponse Response = context.Response; PdfReader reader = new PdfReader(Server.MapPath("./bmp.pdf")); PdfReaderContentParser parser = new PdfReaderContentParser(reader); MyImageRenderListener listener = new MyImageRenderListener(); for (int i = 1; i &lt;= reader.NumberOfPages; i++) { parser.ProcessContent(i, listener); } for (int i = 0; i &lt; listener.Images.Count; ++i) { string path = Server.MapPath("./" + listener.ImageNames[i]); using (FileStream fs = new FileStream( path, FileMode.Create, FileAccess.Write )) { fs.Write(listener.Images[i], 0, listener.Images[i].Length); } } } public bool IsReusable { get { return false; } } public class MyImageRenderListener : IRenderListener { public void RenderText(TextRenderInfo renderInfo) { } public void BeginTextBlock() { } public void EndTextBlock() { } public List&lt;byte[]&gt; Images = new List&lt;byte[]&gt;(); public List&lt;string&gt; ImageNames = new List&lt;string&gt;(); public void RenderImage(ImageRenderInfo renderInfo) { PdfImageObject image = null; try { image = renderInfo.GetImage(); if (image == null) return; ImageNames.Add(string.Format( "Image{0}.{1}", renderInfo.GetRef().Number, image.GetFileType() )); using (MemoryStream ms = new MemoryStream(image.GetImageAsBytes())) { Images.Add(ms.ToArray()); } } catch (IOException ie) { /* * pass-through; image type not supported by iText[Sharp]; e.g. jbig2 */ } } } } </code></pre> <p>The iText[Sharp] development team is still working on the implementation, so I can't say for sure if it will work in <em>your</em> case. But it does work on <a href="http://goo.gl/IYPDH" rel="nofollow">this simple example PDF</a>. (used above and with a couple of other PDFs I tried with bitmap images)</p> <p><strong>EDIT</strong>: I've been experimenting with the new API too and made a mistake in the original code example above. Should have initialized the <code>PdfImageObject</code> to null <strong>outside</strong> the <code>try..catch</code> block. Correction made above.</p> <p>Also, when I use the above code on an unsupported image type, (e.g. jbig2) I get a different Exception - "The color depth XX is not supported", where "XX" is a number. And iTextSharp <strong>does</strong> support <code>FlateDecode</code> in all the examples I've tried. (but that's not helping you in <em>this</em> case, I know)</p> <p>Is the PDF produced by third-party software? (non-Adobe) From what I've read in the book, some third-party vendors produce PDFs that aren't completely up to spec, and iText[Sharp] can't deal with some of these PDFs, while Adobe products can. IIRC I've seen cases specific to some PDFs generated by Crystal Reports on the iText mailing list that caused problems, <a href="http://www.mail-archive.com/itext-questions@lists.sourceforge.net/msg41209.html" rel="nofollow">here's one thread</a>.</p> <p>Is there any way you can generate a test PDF with the software you're using with some non-sensitive <code>FlateDecode</code> image(s)? Then maybe someone here could help a little better.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload