protected override void WriteDocument(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if (File.Exists(FilePath)) { throw new Pdf2KTException("File already exists."); } Document document = null; PdfWriter writer = null; while (Converter.MoveNext()) { if (worker.CancellationPending == true) { e.Cancel = true; break; } BitmapEncoder encoder = BitmapProcessor.GetBitmapEncoder(); BitmapSource processed = BitmapProcessor.Convert(Converter.Current); if (document == null) { document = new Document(new Rectangle(processed.PixelWidth, processed.PixelHeight)); writer = PdfWriter.GetInstance(document, new FileStream(FilePath, FileMode.Create)); document.Open(); document.AddTitle(Converter.Document.Title); document.AddAuthor(Converter.Document.Author); document.AddCreationDate(); document.AddCreator("Pdf2KT"); } document.NewPage(); using (MemoryStream ms = new MemoryStream()) { encoder.Frames.Add(BitmapFrame.Create(processed)); encoder.Save(ms); ms.Position = 0; Image pdfpage = Image.GetInstance(ms); pdfpage.SetAbsolutePosition(0, 0); document.Add(pdfpage); } worker.ReportProgress((int)((Converter.CurrentProcessedPageNumber * 1f) / Converter.PageCount * 100)); } document.Close(); }
/// <summary> /// Write the document. /// </summary> /// <param name="sender">The background worker.</param> /// <param name="e">Event args.</param> /// <exception cref="Pdf2KTException">If the directory already exists.</exception> protected override void WriteDocument(object sender, System.ComponentModel.DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if (Directory.Exists(FilePath)) { throw new Pdf2KTException("Directory already exists."); } Directory.CreateDirectory(FilePath); int pageNum = 1; while (Converter.MoveNext()) { if (worker.CancellationPending == true) { e.Cancel = true; break; } BitmapEncoder encoder = BitmapProcessor.GetBitmapEncoder(); string extension = encoder.CodecInfo.FileExtensions; using (FileStream fs = new FileStream(Path.Combine(FilePath, string.Format("page_{0:0000}{1}", pageNum, extension)), FileMode.Create)) { BitmapSource processed = BitmapProcessor.Convert(Converter.Current); encoder.Frames.Add(BitmapFrame.Create(processed)); encoder.Save(fs); pageNum++; } worker.ReportProgress((int)((Converter.CurrentProcessedPageID * 1f) / Converter.PageCount * 100)); } }