从流中读取 pdf 时未找到 PDF header 签名,
PDF header signature not found while reading pdf from stream,
我正在从 blob 容器下载文件并保存到流中并尝试读取 pdf。
//creating a Cloud Storage instance
CloudStorageAccount StorageAccount = CloudStorageAccount.Parse(connectionstring);
//Creating a Client to operate on blob
CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();
// fetching the container based on name
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
//Get a reference to a blob within the container.
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
var memStream = new MemoryStream();
blob.DownloadToStream(memStream);
try
{
PdfReader reader = new PdfReader(memStream);
}
catch(Exception ex)
{
}
异常:未找到 PDF header 签名。
在通过评论进行故障排除后,原因很明显是这一行:
blob.DownloadToStream(memStream);
将流放在下载内容之后。
然后,在构建 pdf reader 对象时,它希望从当前位置找到 Pdf 文件。
这是处理流时的一个常见问题,首先要向其写入内容,然后再尝试读取该内容,必须记住在必要时重新定位流。
在这种情况下,假设流中只有 pdf,解决方案是在尝试读取 pdf 文件之前将流重新定位回开头:
添加这一行:
memStream.Position = 0;
下载后reader构建重新定位前
以下是此区域中的代码:
blob.DownloadToStream(memStream);
memStream.Position = 0; // <----------------------------------- add this line
try
{
PdfReader reader = new PdfReader(memStream);
我正在从 blob 容器下载文件并保存到流中并尝试读取 pdf。
//creating a Cloud Storage instance
CloudStorageAccount StorageAccount = CloudStorageAccount.Parse(connectionstring);
//Creating a Client to operate on blob
CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();
// fetching the container based on name
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
//Get a reference to a blob within the container.
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
var memStream = new MemoryStream();
blob.DownloadToStream(memStream);
try
{
PdfReader reader = new PdfReader(memStream);
}
catch(Exception ex)
{
}
异常:未找到 PDF header 签名。
在通过评论进行故障排除后,原因很明显是这一行:
blob.DownloadToStream(memStream);
将流放在下载内容之后。
然后,在构建 pdf reader 对象时,它希望从当前位置找到 Pdf 文件。
这是处理流时的一个常见问题,首先要向其写入内容,然后再尝试读取该内容,必须记住在必要时重新定位流。
在这种情况下,假设流中只有 pdf,解决方案是在尝试读取 pdf 文件之前将流重新定位回开头:
添加这一行:
memStream.Position = 0;
下载后reader构建重新定位前
以下是此区域中的代码:
blob.DownloadToStream(memStream);
memStream.Position = 0; // <----------------------------------- add this line
try
{
PdfReader reader = new PdfReader(memStream);