示例#1
0
        public int Attach(string doc, string file)
        {
            var op = new OpenOption {
                SaveMemory = false
            };
            var src  = GetSource(doc);
            var r0   = new DocumentReader(src, "", op);
            var r1   = IO.Get(GetSource(file));
            var dest = Path(Args(r0.File.BaseName, r1.BaseName));

            using (var w = new DocumentWriter())
            {
                w.Add(r0);
                w.Add(r0.Attachments);
                w.Attach(new Attachment(r1.FullName, IO));
                w.Attach(new Attachment(r1.FullName, IO)); // Skip duplicated object.
                w.Save(dest);
            }

            using (var r = new DocumentReader(dest, "", op))
            {
                var items = r.Attachments;
                Assert.That(items.Any(x => x.Name.FuzzyEquals(file)), Is.True);
                foreach (var obj in items)
                {
                    Assert.That(obj.Length, Is.AtLeast(1));
                }
                return(items.Count());
            }
        }
示例#2
0
        /* ----------------------------------------------------------------- */
        ///
        /// Save
        ///
        /// <summary>
        /// Save the PDF document
        /// </summary>
        ///
        /// <param name="src">Facade object.</param>
        /// <param name="dest">Saving file information.</param>
        /// <param name="close">Close action.</param>
        ///
        /* ----------------------------------------------------------------- */
        public static void Save(this MainFacade src, Information dest, Action close)
        {
            var data = src.Bindable;
            var tmp  = data.IO.Combine(dest.DirectoryName, Guid.NewGuid().ToString("D"));

            try
            {
                var reader = data.Source.Value.GetItexReader(data.Query, data.IO);
                data.Set(reader.Metadata, reader.Encryption);

                using (var writer = new DocumentWriter())
                {
                    writer.Add(reader.Attachments);
                    writer.Add(data.Images.Select(e => e.RawObject), reader);
                    writer.Set(data.Metadata);
                    writer.Set(data.Encryption);
                    writer.Save(tmp);
                }

                close();
                src.Backup.Invoke(dest);
                data.IO.Copy(tmp, dest.FullName, true);
            }
            finally { data.IO.TryDelete(tmp); }
        }
示例#3
0
        public int Attach(string doc, string file)
        {
            var src  = GetExamplesWith(doc);
            var r0   = new DocumentReader(src, "", false, IO);
            var r1   = IO.Get(GetExamplesWith(file));
            var dest = Path(Args(r0.File.NameWithoutExtension, r1.NameWithoutExtension));

            using (var w = new DocumentWriter())
            {
                w.Add(r0);
                w.Add(r0.Attachments);
                w.Attach(new Attachment(r1.FullName, IO));
                w.Attach(new Attachment(r1.FullName, IO)); // Skip duplicated object.
                w.Save(dest);
            }

            using (var r = new DocumentReader(dest, "", false, IO))
            {
                var items = r.Attachments;
                Assert.That(items.Any(x => x.Name.FuzzyEquals(file)), Is.True);
                foreach (var obj in items)
                {
                    Assert.That(obj.Length, Is.AtLeast(1));
                }
                return(items.Count());
            }
        }
示例#4
0
        public void Rotate_Failed()
        {
            var src  = GetSource("Sample.pdf");
            var dest = Path(Args("Sample"));
            var op   = new OpenOption {
                SaveMemory = false
            };
            var degree = 90;

            using (var w = new DocumentWriter(IO))
            {
                var r = new DocumentReader(src, "", op);

                w.UseSmartCopy = true;
                w.Set(r.Metadata);
                w.Set(r.Encryption);
                w.Add(Rotate(r.Pages, degree), r);
                w.Save(dest);
            }

            using (var r = new DocumentReader(dest))
            {
                foreach (var page in r.Pages)
                {
                    Assert.That(page.Rotation, Is.Not.EqualTo(degree));
                }
            }
        }
示例#5
0
        public int Merge_Image(string doc, string image, int degree)
        {
            var r0   = new DocumentReader(GetSource(doc), "", false, IO);
            var dest = Path(Args(r0.File.BaseName, IO.Get(image).BaseName));

            using (var w = new DocumentWriter(IO))
                using (var r = new DocumentReader(GetSource(doc), "", false, IO))
                {
                    foreach (var p in r0.Pages)
                    {
                        w.Add(Rotate(p, degree));
                    }
                    w.Add(Rotate(IO.GetImagePages(GetSource(image)), degree));
                    w.Save(dest);
                }
            return(Count(dest, "", degree));
        }
示例#6
0
        public int Merge(string f0, string f1, int degree)
        {
            var r0   = new DocumentReader(GetSource(f0), "", false, IO);
            var r1   = new DocumentReader(GetSource(f1), "", false, IO);
            var dest = Path(Args(r0.File.BaseName, r1.File.BaseName));

            using (var w = new DocumentWriter(IO))
            {
                foreach (var p in r0.Pages)
                {
                    w.Add(Rotate(p, degree), r0);
                }
                w.Add(Rotate(r1.Pages, degree), r1);
                w.Save(dest);
            }
            return(Count(dest, "", degree));
        }
示例#7
0
        /* ----------------------------------------------------------------- */
        ///
        /// Extract
        ///
        /// <summary>
        /// Saves the selected PDF objects as the specified filename.
        /// </summary>
        ///
        /// <param name="src">Source collection.</param>
        /// <param name="dest">Save path.</param>
        ///
        /* ----------------------------------------------------------------- */
        public static void Extract(this ImageCollection src, string dest)
        {
            var pages = GetCopiedIndices(src).OrderBy(i => i).Select(i => src[i].RawObject);

            using (var writer = new DocumentWriter())
            {
                writer.Add(pages);
                writer.Save(dest);
            }
        }
示例#8
0
        /* ----------------------------------------------------------------- */
        ///
        /// Add
        ///
        /// <summary>
        /// Adds the collection of Pages to the specified writer.
        /// </summary>
        ///
        /* ----------------------------------------------------------------- */
        private void Add(DocumentWriter src, string path, SaveOption condition)
        {
            if (Value.SaveOption != condition || !IO.Exists(path))
            {
                return;
            }

            var password = Value.Encryption.Enabled ?
                           Value.Encryption.OwnerPassword :
                           string.Empty;

            src.Add(new DocumentReader(path, password, true, IO));
        }
示例#9
0
        public int Save(string filename, string password, int degree)
        {
            var src  = GetSource(filename);
            var dest = Path(Args(filename));

            using (var w = new DocumentWriter(IO))
                using (var r = new DocumentReader(src, password))
                {
                    w.UseSmartCopy = true;
                    w.Set(r.Metadata);
                    w.Set(r.Encryption);
                    w.Add(Rotate(r.Pages, degree));
                    w.Save(dest);
                }
            return(Count(dest, password, degree));
        }
示例#10
0
        /* ----------------------------------------------------------------- */
        ///
        /// Add
        ///
        /// <summary>
        /// Adds the collection of Pages to the specified writer.
        /// </summary>
        ///
        /* ----------------------------------------------------------------- */
        private void Add(DocumentWriter src, string path, SaveOption so)
        {
            var io    = Settings.IO;
            var value = Settings.Value;

            if (value.SaveOption != so || !io.Exists(path))
            {
                return;
            }

            var password = value.Encryption.Enabled ?
                           value.Encryption.OwnerPassword :
                           string.Empty;

            src.Add(new DocumentReader(path, password, true, io));
        }
示例#11
0
        /* ----------------------------------------------------------------- */
        ///
        /// InvokeItext
        ///
        /// <summary>
        /// Invokes iTextSharp operations.
        /// </summary>
        ///
        /* ----------------------------------------------------------------- */
        private void InvokeItext(string src)
        {
            var tmp = IO.Combine(IO.Get(src).DirectoryName, Guid.NewGuid().ToString("D"));

            using (var writer = new DocumentWriter(IO))
            {
                Value.Encryption.Method = GetEncryptionMethod(Value.Metadata.Version);
                writer.Set(Value.Metadata);
                writer.Set(Value.Encryption);
                Add(writer, Value.Destination, SaveOption.MergeTail);
                writer.Add(new DocumentReader(src, string.Empty, false, IO));
                Add(writer, Value.Destination, SaveOption.MergeHead);
                writer.Save(tmp);
            }

            IO.Move(tmp, src, true);
        }
示例#12
0
        public int Overwrite(string filename, string password, int degree)
        {
            var dest = Path(Args(filename));

            IO.Copy(GetSource(filename), dest, true);

            var r = new DocumentReader(dest, password, false, IO);

            using (var w = new DocumentWriter(IO))
            {
                w.UseSmartCopy = false;
                w.Set(r.Metadata);
                w.Set(r.Encryption);
                w.Add(Rotate(r.Pages, degree), r);
                w.Save(dest);
            }
            return(Count(dest, password, degree));
        }
示例#13
0
        public void SetEncryption(EncryptionMethod method, long permission)
        {
            var src  = GetSource("Sample.pdf");
            var dest = Path(Args(method, permission));
            var op   = new OpenOption {
                SaveMemory = false
            };
            var cmp = new Encryption
            {
                OwnerPassword    = "******",
                UserPassword     = "******",
                OpenWithPassword = true,
                Method           = method,
                Enabled          = true,
                Permission       = new Permission(permission),
            };

            using (var w = new DocumentWriter(IO))
            {
                w.Set(cmp);
                w.Add(new DocumentReader(src, "", op));
                w.Save(dest);
            }

            using (var r = new DocumentReader(dest, cmp.OwnerPassword))
            {
                Assert.That(r.Encryption.Enabled, Is.True);
                Assert.That(r.Encryption.OwnerPassword, Is.EqualTo(cmp.OwnerPassword));
                Assert.That(r.Encryption.Method, Is.EqualTo(cmp.Method));

                var x = r.Encryption.Permission;
                var y = cmp.Permission;
                Assert.That(x.Print, Is.EqualTo(y.Print), nameof(x.Print));
                Assert.That(x.CopyContents, Is.EqualTo(y.CopyContents), nameof(x.CopyContents));
                Assert.That(x.ModifyContents, Is.EqualTo(y.ModifyContents), nameof(x.ModifyContents));
                Assert.That(x.ModifyAnnotations, Is.EqualTo(y.ModifyAnnotations), nameof(x.ModifyAnnotations));
                Assert.That(x.InputForm, Is.EqualTo(y.InputForm), nameof(x.InputForm));
                Assert.That(x.Accessibility, Is.EqualTo(y.Accessibility), nameof(x.Accessibility));
            }
        }
示例#14
0
        public void SetMetadata(string value)
        {
            var src  = GetSource("Sample.pdf");
            var dest = Path(Args(value));
            var op   = new OpenOption {
                SaveMemory = false
            };
            var cmp = new Metadata
            {
                Title    = value,
                Author   = value,
                Subject  = value,
                Keywords = value,
                Creator  = value,
                Producer = value,
                Version  = new PdfVersion(1, 5),
                Options  = ViewerOption.TwoColumnLeft,
            };

            using (var w = new DocumentWriter(IO))
            {
                w.Set(cmp);
                w.Add(new DocumentReader(src, "", op));
                w.Save(dest);
            }

            using (var r = new DocumentReader(dest, "", op))
            {
                var m = r.Metadata;
                Assert.That(m.Title, Is.EqualTo(cmp.Title), nameof(m.Title));
                Assert.That(m.Author, Is.EqualTo(cmp.Author), nameof(m.Author));
                Assert.That(m.Subject, Is.EqualTo(cmp.Subject), nameof(m.Subject));
                Assert.That(m.Keywords, Is.EqualTo(cmp.Keywords), nameof(m.Keywords));
                Assert.That(m.Creator, Is.EqualTo(cmp.Creator), nameof(m.Creator));
                Assert.That(m.Producer, Does.StartWith("iTextSharp"));
                Assert.That(m.Version.Major, Is.EqualTo(cmp.Version.Major));
                Assert.That(m.Version.Minor, Is.EqualTo(cmp.Version.Minor));
                Assert.That(m.Options, Is.EqualTo(cmp.Options));
            }
        }
示例#15
0
        /* ----------------------------------------------------------------- */
        ///
        /// InvokeItext
        ///
        /// <summary>
        /// Invokes iTextSharp operations.
        /// </summary>
        ///
        /* ----------------------------------------------------------------- */
        private void InvokeItext(string src)
        {
            var io    = Settings.IO;
            var value = Settings.Value;
            var tmp   = io.Combine(io.Get(src).DirectoryName, Guid.NewGuid().ToString("D"));

            using (var writer = new DocumentWriter(io))
            {
                value.Encryption.Method = GetEncryptionMethod(value.Metadata.Version);
                writer.Set(value.Metadata);
                writer.Set(value.Encryption);
                Add(writer, value.Destination, SaveOption.MergeTail);
                var options = new OpenOption {
                    IO = io, SaveMemory = false
                };
                writer.Add(new DocumentReader(src, string.Empty, options));
                Add(writer, value.Destination, SaveOption.MergeHead);
                writer.Save(tmp);
            }

            io.Move(tmp, src, true);
        }
示例#16
0
        /* ----------------------------------------------------------------- */
        ///
        /// InvokeItext
        ///
        /// <summary>
        /// iTextSharp による処理を実行します。
        /// </summary>
        ///
        /* ----------------------------------------------------------------- */
        private void InvokeItext(string src)
        {
            var tmp = IO.Combine(IO.Get(src).DirectoryName, Guid.NewGuid().ToString("D"));

            using (var writer = new DocumentWriter(IO))
            {
                var v = Value.FormatOption.GetVersion();
                Value.Metadata.Version  = v;
                Value.Encryption.Method = v.Minor >= 7 ? EncryptionMethod.Aes256 :
                                          v.Minor >= 6 ? EncryptionMethod.Aes128 :
                                          v.Minor >= 4 ? EncryptionMethod.Standard128 :
                                          EncryptionMethod.Standard40;

                writer.Set(Value.Metadata);
                writer.Set(Value.Encryption);
                Add(writer, Value.Destination, SaveOption.MergeTail);
                writer.Add(new DocumentReader(src, string.Empty, false, IO));
                Add(writer, Value.Destination, SaveOption.MergeHead);
                writer.Save(tmp);
            }

            IO.Move(tmp, src, true);
        }