iTextSharp使用GetDrawingImage()扭转CCITTFaxDecode提取的数据

时间:2014-01-28 22:35:17

标签: pdf itextsharp

在某些图片上,当我打电话时:

PdfImageObject pimg = new PdfImageObject(stream);
Image bmp = pimg.GetDrawingImage();

返回的图像被扭曲。我以前见过这个,它通常与字节对齐有关,但我不知道如何解决这个问题。

此对象的/DecodeParms/EndOfLine true /K 0 /Columns 3300.

我已尝试将GetStreamBytesRaw()BitMiracle.LibTiff一起使用,虽然图像已旋转,但我可以正确格式化数据。如果可能的话,我希望GetDrawingImage()能够正确解码数据,假设这是问题。

如果需要,我可以通过电子邮件提供PDF。

谢谢, 达伦

1 个答案:

答案 0 :(得分:0)

对于遇到这种情况的其他人来说,这是我的解决方案。这个难题的关键是理解/ K 0是G3,/ K -1(或任何小于0)是G4 / K 1(或任何大于0的东西)是G3-2D。

当您尝试使G3压缩数据适合G4图像时,会发生扭曲,而这似乎是iTextSharp可能正在做的事情。我知道它肯定不适用于我在项目中实现iTextSharp的方式。我承认我无法破译iTextSharp所做的所有解码工作,所以它也可能是我所缺少的。

EndOfLine在这个难题中没有任何部分,但我仍然认为将换行符放在二进制数据中是一种奇怪的做法。

99%的代码来自BitMiracle.LibTiff.Net - 谢谢。

int nK = 0;// Default to 0 like the PDF Spec
PdfObject oDecodeParms = stream.Get(PdfName.DECODEPARMS);
if (oDecodeParms is PdfDictionary)
{
    PdfObject oK0 = ((PdfDictionary)oDecodeParms).Get(PdfName.K);
    if (oK0 != null)
        nK = ((PdfNumber)oK0).IntValue;
}

using (MemoryStream ms = new MemoryStream())
{
    using (Tiff tiff = Tiff.ClientOpen("custom", "w", ms, new TiffStream()))
    {
        tiff.SetField(TiffTag.IMAGEWIDTH, width);
        tiff.SetField(TiffTag.IMAGELENGTH, height);
        if (nK == 0 || nK > 0) // 0 = Group 3, > 0 = Group 3 2D
            tiff.SetField(TiffTag.COMPRESSION, Compression.CCITTFAX3);
        else if (nK < 0) // < 0 = Group 4
            tiff.SetField(TiffTag.COMPRESSION, Compression.CCITTFAX4);
        tiff.SetField(TiffTag.BITSPERSAMPLE, bpc);
        tiff.SetField(TiffTag.SAMPLESPERPIXEL, 1);
        tiff.WriteRawStrip(0, rawBytes, rawBytes.Length); //saving the tiff file using the raw bytes retrieved from the PDF.
        tiff.Close();
    }

    TiffStreamForBytes byteStream = new TiffStreamForBytes(ms.ToArray());

    using (Tiff input = Tiff.ClientOpen("bytes", "r", null, byteStream))
    {
        int stride = input.ScanlineSize();

        Bitmap result = new Bitmap(width, height, pixelFormat);
        ColorPalette palette = result.Palette;
        palette.Entries[0] = System.Drawing.Color.White;
        palette.Entries[1] = System.Drawing.Color.Black;
        result.Palette = palette;

        for (int i = 0; i < height; i++)
        {
            Rectangle imgRect = new Rectangle(0, i, width, 1);
            BitmapData imgData = result.LockBits(imgRect, ImageLockMode.WriteOnly, pixelFormat);

            byte[] buffer = new byte[stride];
            input.ReadScanline(buffer, i);

            System.Runtime.InteropServices.Marshal.Copy(buffer, 0, imgData.Scan0, buffer.Length);
            result.UnlockBits(imgData);
        }
    }
}

/// <summary>
/// Custom read-only stream for byte buffer that can be used
/// with Tiff.ClientOpen method.
/// </summary>
public class TiffStreamForBytes : TiffStream
{
    private byte[] m_bytes;
    private int m_position;

    public TiffStreamForBytes(byte[] bytes)
    {
        m_bytes = bytes;
        m_position = 0;
    }

    public override int Read(object clientData, byte[] buffer, int offset, int count)
    {
        if ((m_position + count) > m_bytes.Length)
            return -1;

        Buffer.BlockCopy(m_bytes, m_position, buffer, offset, count);
        m_position += count;
        return count;
    }

    public override void Write(object clientData, byte[] buffer, int offset, int count)
    {
        throw new InvalidOperationException("This stream is read-only");
    }

    public override long Seek(object clientData, long offset, SeekOrigin origin)
    {
        switch (origin)
        {
            case SeekOrigin.Begin:
                if (offset > m_bytes.Length)
                    return -1;

                m_position = (int)offset;
                return m_position;

            case SeekOrigin.Current:
                if ((offset + m_position) > m_bytes.Length)
                    return -1;

                m_position += (int)offset;
                return m_position;

            case SeekOrigin.End:
                if ((m_bytes.Length - offset) < 0)
                    return -1;

                m_position = (int)(m_bytes.Length - offset);
                return m_position;
        }

        return -1;
    }

    public override void Close(object clientData)
    {
        // nothing to do
        return;
    }

    public override long Size(object clientData)
    {
        return m_bytes.Length;
    }
}