public void SplitPDF(string source_file, string result) { string tempPath = result + @"\tmp\"; //Create Folder temporary // If directory does not exist, create it. if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); } PdfCopy copy; PdfReader reader = new PdfReader(source_file); for (int i = 1; i <= reader.NumberOfPages; i++) { Document document = new Document(); copy = new PdfCopy(document, new FileStream(tempPath + i + ".pdf", FileMode.Create)); copy.SetFullCompression(); document.Open(); copy.AddPage(copy.GetImportedPage(reader, i)); document.Close(); copy.Close(); } //X reader.Close(); }
public virtual void TestFullCompression2() { Directory.CreateDirectory("PdfCopyTest/"); String outfile = "PdfCopyTest/out-forms.pdf"; String first = RESOURCES + "subscribe.pdf"; String second = RESOURCES + "filled_form_1.pdf"; FileStream out_ = new FileStream(outfile, FileMode.Create); PdfReader reader = new PdfReader(first); PdfReader reader2 = new PdfReader(second); Document pdfDocument = new Document(); PdfCopy pdfCopy = new PdfCopy(pdfDocument, out_); pdfCopy.SetMergeFields(); pdfCopy.SetFullCompression(); pdfCopy.CompressionLevel = PdfStream.BEST_COMPRESSION; pdfDocument.Open(); pdfCopy.AddDocument(reader); pdfCopy.AddDocument(reader2); pdfCopy.Close(); reader.Close(); reader2.Close(); reader = new PdfReader("PdfCopyTest/out-forms.pdf"); Assert.NotNull(reader.GetPageN(1)); reader.Close(); }
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); } } } } }