简体   繁体   English

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

[英]Separate each page in a PDF File iTextsharp

I'm trying to separate each page into one pdf, for example if the PDF have 10 pages, this will generate 10 pdf files. 我正在尝试将每一页分成一个pdf,例如,如果PDF有10页,则将生成10个pdf文件。

Heres my code, 这是我的代码,

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

the first page runs fine, but when it goes to page 2, in Document.Open() gives this error: "cannot access a closed stream" 第一页运行正常,但是转到Document Document.Open()中的第2页时,出现此错误:“无法访问封闭的流”

Just found it, 刚发现

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM