C# 提取内存中的页面
有人能帮我解决这个问题吗。如何从pdf中提取一些页面并将其作为字节数组或流返回,而不使用物理文件作为输出 以下是使用filestream执行此操作的方法:C# 提取内存中的页面,c#,C#,有人能帮我解决这个问题吗。如何从pdf中提取一些页面并将其作为字节数组或流返回,而不使用物理文件作为输出 以下是使用filestream执行此操作的方法: public static void ExtractPages(string sourcePdfPath, string outputPdfPath, int startPage, int endPage) { PdfReader reader = null; Document sourceDocument = n
public static void ExtractPages(string sourcePdfPath, string outputPdfPath, int startPage, int endPage)
{
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
try
{
reader = new PdfReader(sourcePdfPath);
sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));
pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
sourceDocument.Open();
for(int i = startPage; i <= endPage; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
}
sourceDocument.Close();
reader.Close();
}
catch(Exception ex)
{
throw ex;
}
}
将新的System.IO.FileStream(outputPdfPath,System.IO.FileMode.Create)替换为
内存流
,并返回它
一些事情(未经测试,但应该有效):
publicstaticbyte[]ExtractPages(字符串sourcePdfPath、int-startPage、int-endPage)
{
PdfReader reader=null;
Document sourceDocument=null;
PdfCopy pdfCopyProvider=null;
PDFIImportedPage importedPage=null;
MemoryStream target=新的MemoryStream();
读卡器=新的PDF读卡器(sourcePdfPath);
sourceDocument=新文档(reader.GetPageSizeWithRotation(startPage));
pdfCopyProvider=新的PdfCopy(源文档,目标);
Open();
对于(int i=起始页;i
public static byte[] ExtractPages(string sourcePdfPath, int startPage, int endPage)
{
....
return byte[];
}
public static byte[] ExtractPages(string sourcePdfPath, int startPage, int endPage)
{
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
MemoryStream target = new MemoryStream();
reader = new PdfReader(sourcePdfPath);
sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));
pdfCopyProvider = new PdfCopy(sourceDocument, target);
sourceDocument.Open();
for(int i = startPage; i <= endPage; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
}
sourceDocument.Close();
reader.Close();
return target.ToArray();
}