private void AppendMain(PdfReader reader, string title, PageRange[] pageRanges, PdfConcatenatorOption option) { bool addOutlines = option.AddOutlines; bool copyOutlines = option.CopyOutlines; int i; bool[] appendOrNots = new bool[reader.NumberOfPages + 1]; foreach (var pr in pageRanges) { for (int j = pr.StartPage; j <= pr.EndPage; j++) { appendOrNots[j] = true; } } int theFirstPage = 0; for (i = 1; i <= reader.NumberOfPages; i++) { if (appendOrNots[i]) { theFirstPage = i; break; } } if (theFirstPage == 0) { // There is no append paes. return; } if (!opened) { opened = true; document = new Document(reader.GetPageSizeWithRotation(theFirstPage)); writer = new PdfCopy(document, outStream); writer.SetMergeFields(); if (ownerPassword != null) { byte[] bytesUserPassword = userPassword == null ? null : Encoding.ASCII.GetBytes(userPassword); writer.SetEncryption(bytesUserPassword, Encoding.ASCII.GetBytes(ownerPassword), permissions, encryptionStrength); } writer.ViewerPreferences = this.viewerPreference; document.Open(); } if (bookmarks == null) { if (addOutlines || copyOutlines) { bookmarks = new List <Dictionary <string, object> >(); } } if (bookmarks != null) { Dictionary <string, object> m = null; if (addOutlines) { m = new Dictionary <string, object>(); m["Title"] = title; m["Action"] = "GoTo"; m["Page"] = currPageNum.ToString() + " " + option.FittingStyle; } if (copyOutlines) { var cpbookmarks = SimpleBookmark.GetBookmark(reader); if (cpbookmarks != null) { int[] elimPages = new int[2]; int[] shiftPages = new int[2]; shiftPages[1] = reader.NumberOfPages; for (int pageIndex = reader.NumberOfPages; pageIndex > 0; --pageIndex) { if (!appendOrNots[pageIndex]) { elimPages[0] = elimPages[1] = pageIndex; shiftPages[0] = pageIndex + 1; SimpleBookmark.EliminatePages(cpbookmarks, elimPages); SimpleBookmark.ShiftPageNumbers(cpbookmarks, -1, shiftPages); } } SimpleBookmark.ShiftPageNumbers(cpbookmarks, currPageNum - 1, null); if (m == null) { foreach (var c in cpbookmarks) { bookmarks.Add(c); } } else { m["Kids"] = cpbookmarks; } } } if (m != null) { bookmarks.Add(m); } } { var pages = new List <int>(); for (int ii = 1; ii < appendOrNots.Length; ii++) { if (appendOrNots[ii]) { pages.Add(ii); currPageNum++; } } reader.SelectPages(pages); writer.AddDocument(reader); } }
private void DelPart(string srcFile, string dstFile, int page_from, int page_to, bool savePermission) { PdfReader basePDFReader = null; string tmpF = (srcFile == dstFile) ? Path.GetTempFileName() : dstFile; FileAttributes fa = FileAttributes.Normal; try { if (srcFile == dstFile) { if (((int)File.GetAttributes(srcFile) & (int)FileAttributes.ReadOnly) == (int)FileAttributes.ReadOnly) { try { fa = File.GetAttributes(srcFile); File.SetAttributes(srcFile, FileAttributes.Normal); } catch (Exception ex) { ErrorShower.OnShowError(null, ex.Message, Environment.StringResources.GetString("DocControl_Page_Error1")); return; } FileStream fs = null; try { fs = File.Open(dstFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); } catch (IOException) { ErrorShower.OnShowError(null, string.Format( Environment.StringResources.GetString("DocControl_PDF_FileBusy"), dstFile), Environment.StringResources.GetString("DocControl_Page_Error1")); return; } catch (UnauthorizedAccessException ue) { ErrorShower.OnShowError(null, ue.Message, Environment.StringResources.GetString("DocControl_Page_Error1")); return; } finally { if (fs != null) { fs.Close(); } } } } using ( var fio = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)) { string password = String.Empty; try { fio.Position = 0; basePDFReader = new PdfReader(fio, Encoding.UTF8.GetBytes(password)); if (!basePDFReader.IsOpenedWithFullPermissions) { throw new BadPasswordException(""); } } catch (BadPasswordException) { while (basePDFReader == null || !basePDFReader.IsOpenedWithFullPermissions) { if (basePDFReader != null) { basePDFReader.Close(); } password = String.Empty; if ( Controls.PdfViewControl.InputBox.Show(Environment.StringResources.GetString("DocControl_PDF_Encrypted"), Environment.StringResources.GetString("DocControl_PDF_EnterPass"), ref password) == DialogResult.Cancel) { return; } try { fio.Position = 0; basePDFReader = new PdfReader(fio, Encoding.UTF8.GetBytes(password)); } catch (BadPasswordException) { } } } if (basePDFReader.NumberOfPages == 0) { throw new Exception("В документе отсутствуют страницы. Операция сохранения невозможна!"); } page_from = (page_from <= 0 || page_from > basePDFReader.NumberOfPages) ? 1 : page_from; page_to = (page_to <= 0 || page_to > basePDFReader.NumberOfPages) ? basePDFReader.NumberOfPages : page_to; if ((page_to - page_from + 1) == basePDFReader.NumberOfPages) { return; } int i = page_from - 1; int j = 1; var pages = new int[basePDFReader.NumberOfPages]; pages = pages.Select(x => x = j++).ToArray(); pages = pages.Where(x => x <page_from || x> page_to).ToArray(); using (var file_stream = new FileStream(tmpF, FileMode.Create, FileAccess.Write, FileShare.None)) using (var document = new iTextSharp.text.Document()) using (var pdfWriter = new PdfCopy(document, file_stream)) { pdfWriter.CompressionLevel = PdfStream.BEST_COMPRESSION; pdfWriter.SetFullCompression(); pdfWriter.RotateContents = true; if (savePermission && password != String.Empty) { pdfWriter.SetEncryption(null, Encoding.UTF8.GetBytes(password), basePDFReader.Permissions, PdfWriter.STANDARD_ENCRYPTION_128); } document.Open(); foreach (int numberPage in pages) { PdfImportedPage pp = pdfWriter.GetImportedPage(basePDFReader, numberPage); pdfWriter.AddPage(pp); } document.Close(); } } if (srcFile == dstFile) { File.Copy(tmpF, dstFile, true); } } catch (Exception ex) { Data.Env.WriteToLog(ex); } finally { if (basePDFReader != null) { basePDFReader.Close(); } if (srcFile == dstFile) { Slave.DeleteFile(tmpF); if (fa != File.GetAttributes(dstFile)) { try { File.SetAttributes(dstFile, fa); } catch (Exception ex) { Data.Env.WriteToLog(ex); } } } } }