【问题标题】:file does not begin with '%pdf%-'文件不以 '%pdf%-' 开头
【发布时间】:2015-06-17 06:16:13
【问题描述】:

我正在使用 C# 开发 Windows 窗体应用程序,而我正在通过我的 Windows 窗体应用程序运行 PDF。早些时候,我使用的是 MS SQL Server(它工作正常),但现在从 XML 文件中获取数据并将文件从字节转换为 PDF 文件(pdf 格式)。这是我抛出错误的方法:

public static byte[] GetUnCompressedData(byte[] value)
{
    try
    {
        if (value != null)
            using (var zipInputStream = new ZipInputStream(new MemoryStream(value)))
            {
                while ((zipInputStream.GetNextEntry()) != null)
                {
                    using (var zippedInMemoryStream = new MemoryStream())
                    {

                        var data = new byte[2048];
                        while (true)
                        {
                            var size = zipInputStream.Read(data, 0, data.Length);
                            if (size <= 0)
                                break;

                            zippedInMemoryStream.Write(data, 0, size);
                            /// new code added
                            /// 
                            zippedInMemoryStream.Flush();
                        }
                        //zippedInMemoryStream.Close();
                        GC.WaitForPendingFinalizers();
                        GC.Collect();

                        return zippedInMemoryStream.ToArray();
                    }
                }
            }
        return null;
    }
    catch (Exception)
    {
         throw;
    }
}

这是上述方法中抛出错误的那一行:

using (var zippedInMemoryStream = new MemoryStream())

我在 Windows 8/7 中遇到以下错误:

file does not begin with '%pdf%-'

在调试方法时,我还遇到了另一个错误:

Wrong Local header signature: 0x41430A02

工作环境:Visual Studio 2013、.Net Framework 4.5、Acrobat PDF 组件、XML 第三方控制:使用 csharp 代码解压缩/压缩数据,使用 http://icsharpcode.github.io/SharpZipLib/

我还提供了使用以下方法压缩数据的功能:

  public static byte[] GetCompressedData(string fileName, byte[] value)
    {
        try
        {
            // Code for zip file.
            if (value != null && !string.IsNullOrEmpty(fileName))
                using (var zippedMemoryStream = new MemoryStream())
                {
                    // A ZIP stream
                    using (var zipOutputStream = new ZipOutputStream(zippedMemoryStream))
                    {
                        // Highest compression rating 0 - 9.
                        zipOutputStream.SetLevel(9);

                        var entry = new ZipEntry(fileName) { DateTime = DateTime.Now };
                        zipOutputStream.PutNextEntry(entry);

                        zipOutputStream.Write(value, 0, ConvertToInt32(value.Length));

                        zipOutputStream.Finish();
                        zipOutputStream.Close();

                        return zippedMemoryStream.ToArray();
                    }
                }
        }
        catch (Exception)
        {
            throw;
        }
        return null;
    }

【问题讨论】:

  • 尝试捕捉 ZipException 和 IOException 以确定您有什么异常(是 I/O 还是压缩文件时)。
  • 是的,我这样做了,抛出错误 Zip Exception Caught: Wrong Local header signature: 0x44734555
  • 您的文件可能格式不正确。请检查它是一个有效的 PDF 文件,并且您在 byte[] values 中传递了有效数据。
  • 是的,相同的数据与 SQL Server 一起使用,但它工作正常,但是在将 SQL Server 迁移到 XML 格式并尝试使用 XML 中的字节(它只有字符串格式,但 sql 提供不同的数据类型)转换为PDF 文件然后给出错误。
  • 您能尝试使用外部文件解包器解压缩您的value 吗?那么请告诉我们 ZIP 图像无效(压缩问题)还是只有 C# 解压缩无效(解压缩问题)。

标签: c# xml winforms pdf


【解决方案1】:

我结束了这个问题,只是将字节 FromBase64String 转换如下。

 byte[] _imgdata = System.Text.Encoding.UTF8.GetBytes(_obj.FileData);
 _imgdata = Convert.FromBase64String(_obj.FileData);
_pic = clsCommon.ByteToImage(_imgdata);

添加此方法也可以从字节转换为图像

    public static Bitmap ByteToImage(byte[] blob)
    {
        try
        {
            /// Write memory stream
            MemoryStream mStream = new MemoryStream();
            byte[] pData = blob;
            mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
            Bitmap bm = new Bitmap(mStream, false);

            _Height = Convert.ToInt32(bm.Height);
            _Width = Convert.ToInt32(bm.Width);

            mStream.Dispose();
            return bm;
        }
        catch
        {
            throw;
        }
    }

这两天头疼到了上面两条线。

【讨论】:

  • 我也有同样的问题。我也收到相同的错误弹出窗口。但是您的解决方案似乎令人困惑。你能告诉我你是如何解决你的问题的吗?我的问题stackoverflow.com/questions/32260041/…
  • @Ziggler 试过我的回答。我正在添加另一种方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
相关资源
最近更新 更多