首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从pdfsharp提取的旋转图像

从pdfsharp提取的旋转图像
EN

Stack Overflow用户
提问于 2011-11-23 18:15:45
回答 2查看 2K关注 0票数 0

我可以成功地使用pdfsharp从pdf中提取图像。这些图像是CCITFFaxDecode的。但在创建的tiff图像中,图像正在旋转。你知道可能出了什么问题吗?

这是我使用的代码:

代码语言:javascript
复制
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();
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-25 06:41:21

由于问题仍然标记为w/iTextSharp,因此可以添加一些代码,即使您在这里看起来不像是在使用这个库。从iTextSharp 5开始添加了对PDF解析的支持。

我没有你正在使用的图片类型的测试文件,但是found one here (见附件)。下面是一个使用ASP.NET (HTTP handler .ashx) very simple working示例,它使用了测试文档来帮助您:

代码语言:javascript
复制
<%@ 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;可能已旋转文档中的某些页面。

票数 0
EN

Stack Overflow用户

发布于 2011-11-24 16:43:58

这是完整的代码,它从pdf中提取图像,但旋转它。很抱歉代码太长了。

代码语言:javascript
复制
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();
                }
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8240452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档