在PDF文件iTextsharp中分隔每个页面

本文关键字:分隔 PDF 文件 iTextsharp | 更新日期: 2023-09-27 18:01:08

我试图将每个页面分离成一个pdf,例如,如果pdf有10个页面,这将生成10个pdf文件。

这是我的代码,

//Document document = null;
        PdfCopy pdfCopyProvider = null;
        PdfImportedPage importedPage = null;
        MemoryStream target = new MemoryStream();
        try
        {
            int TotalPages = 0;
            MssCountPagesPDF(ssPDF.ssSTPDF.ssBinaryData, out TotalPages, out ssErrors);
            if (TotalPages == 0)
                throw new Exception("The PDF don't have any page!");
            for (int i = 1; i <= TotalPages; i++)
            {
                PdfReader reader = new PdfReader(ssPDF.ssSTPDF.ssBinaryData, System.Text.ASCIIEncoding.ASCII.GetBytes(ssPDF.ssSTPDF.ssPDFPassword));
                // Capture the correct size and orientation for the page:
                Document document = new Document(reader.GetPageSizeWithRotation(i));
                // Initialize an instance of the PdfCopyClass with the source 
                // document and an output file stream:
                pdfCopyProvider = new PdfCopy(document, target);
                document.Open();
                // Extract the desired page number:
                importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                pdfCopyProvider.AddPage(importedPage);
                //close the document
                document.Close();
                reader.Close();
                //Append PDF to the RecordList
                RCPDFRecord rcPDF = new RCPDFRecord();
                rcPDF.ssSTPDF.ssBinaryData = target.ToArray();
                ssPagesPDF.Append(rcPDF);
            }
        }
        catch (Exception exception)
        {
            ssErrors = exception.ToString();
            throw new Exception("There has an unexpected exception" +
                  " occured during the pdf creation process.", exception);
        }
        finally
        {
            target.Close();
        }

第一页运行良好,但当它转到Document.Open((中的第2页时,会出现以下错误:"无法访问关闭的流">

在PDF文件iTextsharp中分隔每个页面

刚刚找到,

ssPagesPDF = new RLPDFRecordList(null);
        ssErrors = "";
        //Document document = null;
        PdfImportedPage importedPage = null;
        try
        {
            int TotalPages = 0;
            MssCountPagesPDF(ssPDF.ssSTPDF.ssBinaryData, out TotalPages, out ssErrors);
            if (TotalPages == 0)
                throw new Exception("The PDF don't have any page!");
            for (int i = 1; i <= TotalPages; i++)
            {
                PdfReader reader = new PdfReader(ssPDF.ssSTPDF.ssBinaryData, System.Text.ASCIIEncoding.ASCII.GetBytes(ssPDF.ssSTPDF.ssPDFPassword));
                // Capture the correct size and orientation for the page:
                using (MemoryStream target = new MemoryStream())
                {
                    using (Document document = new Document(reader.GetPageSizeWithRotation(i)))
                    {
                        using (PdfCopy pdfCopyProvider = new PdfCopy(document, target))
                        {
                            document.Open();
                            // Extract the desired page number:
                            importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                            pdfCopyProvider.AddPage(importedPage);
                            //close the document
                            document.Close();
                            reader.Close();
                            //Append PDF to the RecordList
                            RCPDFRecord rcPDF = new RCPDFRecord();
                            rcPDF.ssSTPDF.ssBinaryData = target.ToArray();
                            ssPagesPDF.Append(rcPDF);
                        }
                    }
                }
            }
        }
        catch (Exception exception)
        {
            ssErrors = exception.ToString();
            throw new Exception("There has an unexpected exception" +
                  " occured during the pdf creation process.", exception);
        }