Пример #1
0
 private void ImagesToPdf(CancellationToken token)
 {
     using (org.pdfclown.files.File file = new org.pdfclown.files.File())
     {
         var images = Directory.GetFiles(imageTempDir, "*.jpg");
         for (int index = 0; index < images.Length; index++)
         {
             using (var steam = new FileStream($"{imageTempDir}\\output_{index}.jpg", FileMode.Open))
             {
                 var  image = org.pdfclown.documents.contents.entities.Image.Get(steam);
                 var  size  = new SizeF(image.Width, image.Height);
                 Page page  = new Page(file.Document, size);
                 file.Document.Pages.Add(page);
                 PrimitiveComposer primitiveComposer = new PrimitiveComposer(page);
                 primitiveComposer.ShowXObject(image.ToXObject(file.Document), new PointF(0, 0), size);
                 primitiveComposer.Flush();
             }
         }
         if (token.IsCancellationRequested)
         {
             ClearTempImages();
             token.ThrowIfCancellationRequested();
         }
         file.Save(outputFile, SerializationModeEnum.Standard);
     }
     ClearTempImages();
 }
Пример #2
0
        public byte[] MergePdfDocuments(List <byte[]> files)
        {
            if (files == null)
            {
                return(null);
            }
            if (files.Count == 1)
            {
                return(files[0]);
            }
            using (var dest = new org.pdfclown.files.File(new org.pdfclown.bytes.Buffer(files[0])))
            {
                var document = dest.Document;
                var builder  = new org.pdfclown.tools.PageManager(document);
                foreach (var file in files.Skip(1))
                {
                    using (var src = new org.pdfclown.files.File(new org.pdfclown.bytes.Buffer(file)))
                    { builder.Add(src.Document); }
                }

                using (var output = new org.pdfclown.bytes.Buffer())
                {
                    dest.Save(output, SerializationModeEnum.Incremental);
                    return(output.ToByteArray());
                }
            }
        }
Пример #3
0
 public void SaveByteToPdf(byte[] bytes, string src)
 {
     using (var ms = new MemoryStream(bytes))
     {
         using (org.pdfclown.bytes.IInputStream stream = new org.pdfclown.bytes.Stream(ms))
         {
             using (var file = new org.pdfclown.files.File(stream))
             {
                 file.Save(src, SerializationModeEnum.Standard);
             }
         }
     }
 }
Пример #4
0
        public void MergePdfDocuments(string destFile)
        {
            var list = Directory.GetFiles(Path.GetFullPath(_src));

            if (string.IsNullOrWhiteSpace(_src) || string.IsNullOrWhiteSpace(destFile) || list.Length <= 1)
            {
                return;
            }
            var files = list.Select(File.ReadAllBytes).ToList();

            using (var dest = new org.pdfclown.files.File(new org.pdfclown.bytes.Buffer(files[0])))
            {
                var document = dest.Document;
                var builder  = new org.pdfclown.tools.PageManager(document);
                foreach (var file in files.Skip(1))
                {
                    using (var src = new org.pdfclown.files.File(new org.pdfclown.bytes.Buffer(file)))
                    { builder.Add(src.Document); }
                }

                dest.Save(destFile, SerializationModeEnum.Incremental);
            }
        }