分隔 PDF 文件中的每一页 iTextsharp

Separate each page in a PDF File iTextsharp

我试图将每一页分成一个 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();
        }

第一页运行正常,但是当转到第 2 页时,在 Document.Open() 中出现此错误:"cannot access a closed stream"

刚找到,

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);
        }