PdfReader按页将PDF切割成多个PDF

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 = 1; i < pdfReader.NumberOfPages + 1; i++)
{
    var memoryStream = GetNewPdfByPageNum(pdfReader, i);
    FileStream file = new FileStream($"{desktopPath}/page{i}.pdf", FileMode.CreateNew);
    memoryStream.WriteTo(file);
    file.Dispose();
}
pdfReader.Close();

 

posted @ 2019-04-08 18:09  找不到一个满意的昵称  阅读(547)  评论(0编辑  收藏  举报