Image 从pdfsharp中提取旋转图像
我能够成功地使用pdfsharp从pdf中提取图像。该图像为CCITFAX解码图像。但在创建的tiff图像中,图像正在旋转。知道哪里出了问题吗 这是我使用的代码:Image 从pdfsharp中提取旋转图像,image,itextsharp,pdfsharp,Image,Itextsharp,Pdfsharp,我能够成功地使用pdfsharp从pdf中提取图像。该图像为CCITFAX解码图像。但在创建的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.S
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();
由这是完整的代码,这是提取图像从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();
}
}
}
}
}
PdfDocument document=PdfReader.Open(“D:\\Sample.pdf”);
PdfDictionary resources=document.pages.Elements.GetDictionary(“/resources”);
PdfDictionary xObjects=resources.Elements.GetDictionary(“/XObject”);
if(xObject!=null)
{
ICollection items=xObjects.Elements.Values;
//迭代对外部对象的引用
foreach(PdfItem项目中的项目)
{
PdfReference reference=作为PdfReference的项目;
如果(引用!=null)
{
PdfDictionary xObject=引用。值为PdfDictionary;
//外部物体是图像吗?
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);
字节[]数据=xObject.Stream.Value;
Tiff Tiff=bitmracle.LibTiff.Classic.Tiff.Open(“D:\\sample.tif”,“w”);
tiff.设置字段(TiffTag.IMAGEWIDTH,(uint)(宽度));
tiff.设置字段(TiffTag.IMAGELENGTH,(uint)(高度));
设置字段(TiffTag.COMPRESSION,(uint)bitmracle.LibTiff.Classic.COMPRESSION.CCITTFAX4);
tiff.SetField(TiffTag.BITSPERSAMPLE,(uint)(bpp));
tiff.设置字段(TiffTag.STRIPOFFSETS,187);
tiff.WriteRawStrip(0,data,data.Length);
tiff.Close();
}
}
}
}
}
由于问题仍然被标记为w/iTextSharp,因此可能需要添加一些代码,即使看起来您并没有在这里使用库。从iText[Sharp]5开始添加了PDF解析支持
没有您正在使用的图像类型的测试PDF,但是(请参阅附件)。下面是ASP.NET
(HTTP 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());
}
}
}
}
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用System.Web;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
使用iTextSharp.text.pdf.parser;
使用Dotnet=System.Drawing.Image;
使用系统、绘图、成像;
公共类CCITTFANDECODE摘录:IHttpHandler{
公共void ProcessRequest(HttpContext上下文){
HttpServerUtility服务器=context.Server;
HttpResponse Response=context.Response;
字符串文件=Server.MapPath(“~/app_data/CCITTFaxDecode.pdf”);
PdfReader reader=新PdfReader(文件);
PdfReaderContentParser=新的PdfReaderContentParser(读取器);
MyImageRenderListener=新建MyImageRenderListener();
对于(int i=1;i无PDF,无TIFF,关于提取代码-我们如何知道出了什么问题?可能图像是通过旋转变换在PDF中绘制的?或者PDF页面是旋转的?可能没有任何问题,一切都是经过设计的。哦,你的意思是,如果图像是通过旋转变换在PDF上绘制的,那么提取的图像输出也会旋转?图像的旋转是否与pdf和tiff图像的坐标系有关?