private static string FlowDocumentToXps(PrintSettings settings, out List <Exception> exceptions) { exceptions = new List <Exception>(); var tempFile = Path.Combine(Path.GetTempPath(), "Helper.xps"); var tempFlow = settings.Document;//.CopyWithTickness(new Thickness(0)); tempFlow.PagePadding = PagePadding; try { CreateXps(tempFlow, tempFile, settings); } catch (Exception exception) { exceptions.Add(exception); } return(tempFile); }
private static void CreateXps(FlowDocument tempFlow, string tempFile, PrintSettings settings) { //flowDocument, out exceptions, CheckGreyMark(mark, blackAndWhite), showNumber, face, blackAndWhite ? Brushes.Black : brush, useLeading, fontSize, from, till, useOldNumbering, blackAndWhite ? Brushes.Transparent : backBrush // var stream = new MemoryStream(); using (Package package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite)) { XpsDocument xpsDoc; using (xpsDoc = new XpsDocument(package, CompressionOption.Maximum)) { var rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false); DocumentPaginator paginator = ((IDocumentPaginatorSource)tempFlow).DocumentPaginator; paginator = new DocumentPaginatorWrapper(paginator) { ContentPageSize = new Size(paginator.PageSize.Width, paginator.PageSize.Height), ContentMargin = new Size(48, 48), Mark = settings.UseWatermark ? CheckGreyMark(settings.Watermark, settings.UseBlackAndWhite):null, FooterTypeface = new Typeface(settings.PageCountElement.FontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), ForegroundBrush = settings.UseBlackAndWhite ? Brushes.Black : settings.PageCountElement.ForgroundBrush, ShowNumbering = settings.ShowPageNumber, UseLeadingZero = settings.PageCountElement.UseLeadingZero, FooterFontSize = settings.PageCountElement.FontSize, FirstPritedPage = settings.From, LastPrintedPage = settings.Till, UseOldNumbering = settings.UseOldNumbering, BackgroundBrush = settings.UseBlackAndWhite ? Brushes.White : settings.BackgroundBrush }; ((DocumentPaginatorWrapper)paginator).ComputeValues(); rsm.SaveAsXaml(paginator); rsm.Commit(); } } stream.Position = 0; using (var fileStream = new FileStream(tempFile, FileMode.CreateNew, FileAccess.ReadWrite)) { stream.CopyTo(fileStream); } }
public static bool PrintTextWithDialog(RichTextBox box, out List <Exception> exceptions, ObservableCollection <Color> predefColors, ref PrintSettings settings, string defaultFilename, ObservableCollection <Character> characters) { exceptions = new List <Exception>(); var flowDocument = box.Document.CopyWithTickness(new Thickness(0)); flowDocument.HidePageBreaker(); var print = new PrintDialogHelper(flowDocument, characters, settings.FontFamily, settings.FontSize, settings.UseCharacters) { Owner = MainWindow.Global, CanUseWaterMark = settings.Watermark != null, CanUseCharacters = characters != null && characters.Any(), PageCountElement = settings.PageCountElement, PredefinedColors = predefColors, BackgroundBrush = settings.BackgroundBrush, FontSizeInc = settings.FontSizeModifier, KeepOldNumbering = settings.UseOldNumbering, IsBlackAndWhite = settings.UseBlackAndWhite, UseWaterMark = settings.UseWatermark, UseCharacters = settings.UseCharacters, ShowPageCount = settings.ShowPageNumber }; print.ShowDialog(); if (print.Result == MessageBoxResult.Cancel) { return(false); } settings.UseBlackAndWhite = print.IsBlackAndWhite; settings.ShowPageNumber = print.ShowPageCount; settings.UseOldNumbering = print.KeepOldNumbering; settings.UseWatermark = print.UseWaterMark; settings.UseCharacters = print.UseCharacters; settings.PageCountElement = print.PageCountElement; settings.BackgroundBrush = print.BackgroundBrush; settings.FontSizeModifier = print.FontSizeInc; if (print.Result == MessageBoxResult.Yes) { return(true); } var defaultName = !string.IsNullOrEmpty(defaultFilename)? defaultFilename: MainWindow.GetDefaultName(box.GetText()); string pdfFilename = GetFileName(defaultName); settings.PdfName = pdfFilename; settings.Document = print.Document; settings.From = print.From; settings.Till = print.Till; UiServices.SetBusyState(); exceptions = PrintText(settings); settings.PageCountElement = print.PageCountElement; return(!exceptions.Any()); }