从pdfsharp中提取的旋转图像

时间:2011-11-23 10:15:45

标签: image itextsharp pdfsharp

我已成功使用pdfsharp从pdf中提取图像。图像是CCITFFaxDecode。但是在创建的tiff图像中,图像会旋转。知道可能出现什么问题吗?

这是我使用的代码:

byte[] data = xObject.Stream.Value;
Tiff tiff = BitMiracle.LibTiff.Classic.Tiff.Open("D:\\clip_TIFF.tif", "w");
tiff.SetField(TiffTag.IMAGEWIDTH, (uint)(width));
tiff.SetField(TiffTag.IMAGELENGTH, (uint)(height));
tiff.SetField(TiffTag.COMPRESSION, (uint)BitMiracle.LibTiff.Classic.Compression.CCITTFAX4);
tiff.SetField(TiffTag.BITSPERSAMPLE, (uint)(bpp));
tiff.WriteRawStrip(0,data,data.Length);
tiff.Close();

2 个答案:

答案 0 :(得分:0)

通过这是完整的代码,它从pdf中提取图像,但旋转它。抱歉代码的长度。

PdfDocument document = PdfReader.Open("D:\\Sample.pdf");
PdfDictionary resources =document.pages.Elements.GetDictionary("/Resources");
PdfDictionary xObjects = resources.Elements.GetDictionary("/XObject");
if (xObjects != null)
{
    ICollection<PdfItem> items = xObjects.Elements.Values;
    // Iterate references to external objects
    foreach (PdfItem item in items)
    {
        PdfReference reference = item as PdfReference;
        if (reference != null)
        {
            PdfDictionary xObject = reference.Value as PdfDictionary;
            // Is external object an image?

            if (xObject != null && xObject.Elements.GetString("/Subtype") == "/Image")
            {
                string filter = xObject.Elements.GetName("/Filter");

                if (filter.Equals("/CCITTFaxDecode"))
                {
                    int width = xObject.Elements.GetInteger(PdfImage.Keys.Width);
                    int height = xObject.Elements.GetInteger(PdfImage.Keys.Height);
                    int bpp = xObject.Elements.GetInteger(PdfImage.Keys.BitsPerComponent);

                    byte[] data = xObject.Stream.Value;
                    Tiff tiff = BitMiracle.LibTiff.Classic.Tiff.Open("D:\\sample.tif", "w");
                    tiff.SetField(TiffTag.IMAGEWIDTH, (uint)(width));
                    tiff.SetField(TiffTag.IMAGELENGTH, (uint)(height));
                    tiff.SetField(TiffTag.COMPRESSION, (uint)BitMiracle.LibTiff.Classic.Compression.CCITTFAX4);
                    tiff.SetField(TiffTag.BITSPERSAMPLE, (uint)(bpp));
                    tiff.SetField(TiffTag.STRIPOFFSETS, 187);

                    tiff.WriteRawStrip(0,data,data.Length);
                    tiff.Close();
                }
            }
        }
    }
}

答案 1 :(得分:0)

由于问题仍然被标记为w / iTextSharp可能会添加一些代码,即使它看起来不像你在这里使用库。从iText [Sharp] 5开始添加PDF解析支持。

没有包含您正在使用的图片类型的测试PDF,但是found one here(请参阅附件)。这是ASP.NETHTTP handler .ashx)中使用该测试PDF文档的非常简单工作示例,可以帮助您:

<%@ WebHandler Language="C#" Class="CCITTFaxDecodeExtract" %>
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using Dotnet = System.Drawing.Image;
using System.Drawing.Imaging;

public class CCITTFaxDecodeExtract : IHttpHandler {
  public void ProcessRequest (HttpContext context) {
    HttpServerUtility Server = context.Server;
    HttpResponse Response = context.Response;
    string file = Server.MapPath("~/app_data/CCITTFaxDecode.pdf");
    PdfReader reader = new PdfReader(file);
    PdfReaderContentParser parser = new PdfReaderContentParser(reader);
    MyImageRenderListener listener = new MyImageRenderListener();
    for (int i = 1; i <= reader.NumberOfPages; i++) {
      parser.ProcessContent(i, listener);
    } 
    for (int i = 0; i < listener.Images.Count; ++i) {
      string path = Server.MapPath("~/app_data/" + 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; } }
/*
 * see: TextRenderInfo & RenderListener classes here:
 * http://api.itextpdf.com/itext/
 * 
 * and Google "itextsharp extract images"
 */
  public class MyImageRenderListener : IRenderListener {
    public void RenderText(TextRenderInfo renderInfo) { }
    public void BeginTextBlock() { }
    public void EndTextBlock() { }

    public List<byte[]> Images = new List<byte[]>();
    public List<string> ImageNames = new List<string>();
    public void RenderImage(ImageRenderInfo renderInfo) {
      PdfImageObject image = renderInfo.GetImage();
      PdfName filter = image.Get(PdfName.FILTER) as PdfName;
      if (filter == null) {
        PdfArray pa = (PdfArray) image.Get(PdfName.FILTER);
        for (int i = 0; i < pa.Size; ++i) {
          filter = (PdfName) pa[i];
        }
      }
      if (PdfName.CCITTFAXDECODE.Equals(filter)) {
        using (Dotnet dotnetImg = image.GetDrawingImage()) {
          if (dotnetImg != null) {
            ImageNames.Add(string.Format(
              "{0}.tiff", renderInfo.GetRef().Number)
            );
            using (MemoryStream ms = new MemoryStream()) {
              dotnetImg.Save(
              ms, ImageFormat.Tiff);
              Images.Add(ms.ToArray());
            }
          }
        }
      }
    }
  }
}

如果正在旋转图像,see this thread on the iText mailing list;也许PDF文档中的某些页面已被轮换。