PdfReader按页将PDF切割成多个PDF

时间:2021-06-21 07:19:34
private MemoryStream GetNewPdfByPageNum(PdfReader pdfReader, int pageNum)
{
MemoryStream memoryStream = new MemoryStream();
try
{
iTextSharp.text.Document document = new iTextSharp.text.Document();
PdfCopy copy = new PdfCopy(document, memoryStream) { CloseStream = false };
document.Open();
copy.AddPage(copy.GetImportedPage(pdfReader, pageNum));
document.Close();
}
catch { }
return memoryStream;
}
var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var pdfBytes = new WebClient().DownloadData("http://....pdf");
var pdfReader = new PdfReader(pdfBytes);
for (int i = ; i < pdfReader.NumberOfPages + ; i++)
{
var memoryStream = GetNewPdfByPageNum(pdfReader, i);
FileStream file = new FileStream($"{desktopPath}/page{i}.pdf", FileMode.CreateNew);
memoryStream.WriteTo(file);
file.Dispose();
}
pdfReader.Close();