當前位置: 首頁>>代碼示例>>C#>>正文


C# PdfCopy.Dispose方法代碼示例

本文整理匯總了C#中iTextSharp.text.pdf.PdfCopy.Dispose方法的典型用法代碼示例。如果您正苦於以下問題:C# PdfCopy.Dispose方法的具體用法?C# PdfCopy.Dispose怎麽用?C# PdfCopy.Dispose使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在iTextSharp.text.pdf.PdfCopy的用法示例。


在下文中一共展示了PdfCopy.Dispose方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ExtractPages

        //Thanks for John Atten from code project
        //http://www.codeproject.com/Articles/559380/Splitting-and-Merging-PDF-Files-in-Csharp-Using-iT#iTextExampleExtractPageRange
        private void ExtractPages(string sourcePdfPath, string outputPdfPath, int[] extractThesePages)
        {
            PdfReader reader = null;
            Document sourceDocument = null;
            PdfCopy pdfCopyProvider = null;
            PdfImportedPage importedPage = null;

            try {
                // Intialize a new PdfReader instance with the
                // contents of the source Pdf file:
                reader = new PdfReader(sourcePdfPath);

                // For simplicity, I am assuming all the pages share the same size
                // and rotation as the first page:
                sourceDocument = new Document(reader.GetPageSizeWithRotation(extractThesePages[0]));

                // Initialize an instance of the PdfCopyClass with the source
                // document and an output file stream:
                pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

                sourceDocument.Open();

                // Walk the array and add the page copies to the output file:
                foreach (int pageNumber in extractThesePages) {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, pageNumber);
                    pdfCopyProvider.AddPage(importedPage);
                }

                pdfCopyProvider.Dispose();
                sourceDocument.Close();
                reader.Close();
            } catch (Exception ex) {
                throw ex;
            }
        }
開發者ID:KolololoPL,項目名稱:PDFManager,代碼行數:37,代碼來源:PDFSplitter.cs

示例2: SplitWorkerDoWork

        private void SplitWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            object[] args = (object[])e.Argument;
            PdfFile pdfFile = (PdfFile)args[0];
            PageRangeParser pageRangeParser = (PageRangeParser)args[1];
            string destinationPath = (string)args[2];
            bool overwriteFile = (bool)args[3];
            FileStream outFileStream = null;
            Document destinationDoc = null;
            PdfCopy pdfCopy = null;
            int skippedFiles = 0;
            string exportFileName = string.Empty;
            string errorMsg = string.Empty;
            int exportFileCnt = 0, totalNumberOPages = 0, pageCnt = 0;
            EasySplitAndMergePdf.Helper.PageRange[] pageRanges = null;

            if (pageRangeParser.TryParse(out pageRanges) != Define.Success)
            {
                errorMsg = "An error occurred while parsing PDF page ranges" + pageRangeParser.ErrorMsg;
            }
            else if ((totalNumberOPages = pageRanges.Sum(range => range.PageCount)) < 1)
            {
                errorMsg = "The number of PDF pages to extract from source file is zero.";
            }
            else
            {
                pdfFile.Reader.RemoveUnusedObjects();

                while (exportFileCnt < pageRanges.Length && !splitBackgroundWorker.CancellationPending)
                {
                    exportFileName = destinationPath + (exportFileCnt + 1).ToString("D4") + ".pdf";
                    if (FileHelpers.FileIsAvailable(exportFileName, overwriteFile, out outFileStream, out errorMsg) == Define.Success)
                    {
                        destinationDoc = new Document();
                        pdfCopy = new PdfCopy(destinationDoc, outFileStream);
                        destinationDoc.Open();

                        splitBackgroundWorker.ReportProgress(pageCnt * 100 / totalNumberOPages,
                            string.Format("Creating and processing PDF file: {0}", exportFileName));

                        if (pageRanges[exportFileCnt].Pages != null)
                        {
                            int pageArrayIndex = 0;
                            while (pageArrayIndex < pageRanges[exportFileCnt].Pages.Length &&
                                !splitBackgroundWorker.CancellationPending)
                            {
                                destinationDoc.SetPageSize(pdfFile.Reader.GetPageSizeWithRotation(pageRanges[exportFileCnt].Pages[pageArrayIndex]));
                                destinationDoc.NewPage();
                                pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfFile.Reader, pageRanges[exportFileCnt].Pages[pageArrayIndex]));
                                splitBackgroundWorker.ReportProgress(++pageCnt * 100 / totalNumberOPages);
                                pageArrayIndex++;
                            }
                        }
                        else if (pageRanges[exportFileCnt].PageFrom <= pageRanges[exportFileCnt].PageTo)
                        {
                            int pageNumber = pageRanges[exportFileCnt].PageFrom;
                            while (pageNumber <= pageRanges[exportFileCnt].PageTo &&
                                !splitBackgroundWorker.CancellationPending)
                            {
                                destinationDoc.SetPageSize(pdfFile.Reader.GetPageSizeWithRotation(pageNumber));
                                destinationDoc.NewPage();
                                pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfFile.Reader, pageNumber));
                                splitBackgroundWorker.ReportProgress(++pageCnt * 100 / totalNumberOPages);
                                pageNumber++;
                            }
                        }
                        else if (pageRanges[exportFileCnt].PageFrom > pageRanges[exportFileCnt].PageTo)
                        {
                            int pageNumber = pageRanges[exportFileCnt].PageFrom;
                            while (pageNumber >= pageRanges[exportFileCnt].PageTo &&
                                !splitBackgroundWorker.CancellationPending)
                            {
                                destinationDoc.SetPageSize(pdfFile.Reader.GetPageSizeWithRotation(pageNumber));
                                destinationDoc.NewPage();
                                pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfFile.Reader, pageNumber));
                                splitBackgroundWorker.ReportProgress(++pageCnt * 100 / totalNumberOPages);
                                pageNumber--;
                            }
                        }

                        //Exception on document.Close() when doc is empty
                        //if (pages.Count == 0) { throw new IOException("The document has no pages.") };
                        //When canceling pages.Count could be zero therefore skip cleanup.
                        if (destinationDoc != null &&
                            !splitBackgroundWorker.CancellationPending)
                        {
                            destinationDoc.Close();
                            destinationDoc.Dispose();
                            destinationDoc = null;
                        }

                        if (pdfCopy != null &&
                            !splitBackgroundWorker.CancellationPending)
                        {
                            pdfCopy.Close();
                            pdfCopy.Dispose();
                            pdfCopy = null;
                        }

                        if (outFileStream != null)
//.........這裏部分代碼省略.........
開發者ID:Mohamed1976,項目名稱:EasySplitAndMergePdf,代碼行數:101,代碼來源:SplitPdfViewModel.cs


注:本文中的iTextSharp.text.pdf.PdfCopy.Dispose方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。