OK,足夠多的人收到此錯誤,我要去發佈一些代碼,我有識別TIFF格式:
private const int kTiffTagLength = 12;
private const int kHeaderSize = 2;
private const int kMinimumTiffSize = 8;
private const byte kIntelMark = 0x49;
private const byte kMotorolaMark = 0x4d;
private const ushort kTiffMagicNumber = 42;
private bool IsTiff(Stream stm)
{
stm.Seek(0);
if (stm.Length < kMinimumTiffSize)
return false;
byte[] header = new byte[kHeaderSize];
stm.Read(header, 0, header.Length);
if (header[0] != header[1] || (header[0] != kIntelMark && header[0] != kMotorolaMark))
return false;
bool isIntel = header[0] == kIntelMark;
ushort magicNumber = ReadShort(stm, isIntel);
if (magicNumber != kTiffMagicNumber)
return false;
return true;
}
private ushort ReadShort(Stream stm, bool isIntel)
{
byte[] b = new byte[2];
_stm.Read(b, 0, b.Length);
return ToShort(_isIntel, b[0], b[1]);
}
private static ushort ToShort(bool isIntel, byte b0, byte b1)
{
if (isIntel)
{
return (ushort)(((int)b1 << 8) | (int)b0);
}
else
{
return (ushort)(((int)b0 << 8) | (int)b1);
}
}
我砍死除了一些更加通用的代碼來獲得這個。
對於PDF,我有一些代碼,看起來像這樣:
public bool IsPdf(Stream stm)
{
stm.Seek(0, SeekOrigin.Begin);
PdfToken token;
while ((token = GetToken(stm)) != null)
{
if (token.TokenType == MLPdfTokenType.Comment)
{
if (token.Text.StartsWith("%PDF-1."))
return true;
}
if (stm.Position > 1024)
break;
}
return false;
}
現在,爲gettoken()是調用成一個標記化流成PDF令牌的掃描儀。這是不平凡的,所以我不會在這裏粘貼它。我使用的分詞器,而不是看着子,以避免這樣的問題:
% the following is a PostScript file, NOT a PDF file
% you'll note that in our previous version, it started with %PDF-1.3,
% incorrectly marking it as a PDF
%
clippath stroke showpage
這段代碼被標記爲不可通過一個上面的代碼片斷PDF,而代碼更簡單的塊會錯誤地標記它作爲PDF。
我還應該指出,目前的ISO規範沒有包含以前Adobe擁有的規範中的實現註釋。從PDF參考1.6版最重要的是:
Acrobat viewers require only that the header appear somewhere within
the first 1024 bytes of the file.
謝謝!我今晚會檢查一下 – eviljack 2010-04-28 19:59:07
真棒,它的工作原理! – eviljack 2010-04-28 20:11:19
stm.Seek(0);我失敗了,沒有編譯。我使用vs 2008,.net 3.5。 – Kiquenet 2010-07-21 20:43:55