/// <summary> /// /// </summary> /// <param name="files"></param> /// <param name="doc"></param> /// <param name="worker"></param> private void AddFileToDoc(IncomingFileViewModel file, PdfDocument doc, bool watermark) { //handle multi page image files (multi frame tiffs) List<BitmapSource> imgs = ImgToPdf.GetImagesFromFile(file.Info.FullName); foreach (BitmapSource bmp in imgs) { //create the new page with the appropriate paper type and rotation PdfPage page = doc.AddPage(); //get the orientation Orientation o = Properties.Settings.Default.Rotation; if (o == Orientation.AutoSelect) //auto select based off image { if (bmp.Height / bmp.Width > bmp.Width / bmp.Height) o = Orientation.Portrait; else o = Orientation.Landscape; } double ratio; if (o == Orientation.Landscape) { page.Rotate = 90; //auto detect paper size on undefined if (Properties.Settings.Default.PaperType == PageSize.Undefined) SetPageSizeByRatio(page, bmp.Height, bmp.Width); else page.Size = Properties.Settings.Default.PaperType; //one paper size for all converted documents... ratio = Math.Min(page.Height.Value / bmp.Width, page.Width.Value / bmp.Height); } else { //auto detect paper size on undefined if (Properties.Settings.Default.PaperType == PageSize.Undefined) SetPageSizeByRatio(page, bmp.Width, bmp.Height); else page.Size = Properties.Settings.Default.PaperType; //one paper size for all converted documents... ratio = Math.Min(page.Width.Value / bmp.Width, page.Height.Value / bmp.Height); } //convert to monochrome or otherwise compress prior to resizing BitmapSource converted = bmp; if (Properties.Settings.Default.ConvertToMonochrome) { converted = new FormatConvertedBitmap(converted, PixelFormats.BlackWhite, BitmapPalettes.BlackAndWhite, 0); } //handle scaling here for better results/speed than the pdf lib scaling (keep aspect ratio) Transform robotInDisguise = new ScaleTransform(ratio, ratio); BitmapSource optimus = new TransformedBitmap(converted, robotInDisguise); if (watermark) { this.AddWaterMark(page, "http://www.8labs.com", new XFont("Arial", 48)); } using (XGraphics gfx = XGraphics.FromPdfPage(page)) using (XImage ximg = XImage.FromBitmapSource(optimus)) { //draw the image full page onto the document (no margins) gfx.DrawImage(ximg, 0, 0, optimus.Width, optimus.Height); //already scaled } if (watermark) { this.AddWaterMark(page, "http://www.8labs.com", new XFont("Arial", 48)); } } }
//comparison for the file view models sorting private static int CompareFileName(IncomingFileViewModel x, IncomingFileViewModel y) { return x.Info.FullName.CompareTo(y.Info.FullName); }