private void Xps_Click(object sender, RoutedEventArgs e) { FixedPage page = new FixedPage() { Background = Brushes.White, Width = Dpi * PaperWidth, Height = Dpi * PaperHeight }; TextBlock tbTitle = new TextBlock { Text = "My InkCanvas Sketch", FontSize = 24, FontFamily = new FontFamily("Arial") }; FixedPage.SetLeft(tbTitle, Dpi * 0.75); FixedPage.SetTop(tbTitle, Dpi * 0.75); page.Children.Add((UIElement)tbTitle); var toPrint = myInkCanvasBorder; myGrid.Children.Remove(myInkCanvasBorder); FixedPage.SetLeft(toPrint, Dpi * 2); FixedPage.SetTop(toPrint, Dpi * 2); page.Children.Add(toPrint); Size sz = new Size(Dpi * PaperWidth, Dpi * PaperHeight); page.Measure(sz); page.Arrange(new Rect(new Point(), sz)); page.UpdateLayout(); var filepath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "PrintingTest"); if (!File.Exists(filepath)) { Directory.CreateDirectory(filepath); } var filename = System.IO.Path.Combine(filepath, "myXpsFile.xps"); FixedDocument doc = new FixedDocument(); PageContent pageContent = new PageContent(); ((System.Windows.Markup.IAddChild)pageContent).AddChild(page); doc.Pages.Add(pageContent); XpsDocument xpsd = new XpsDocument(filename, FileAccess.Write); XpsDocument.CreateXpsDocumentWriter(xpsd).Write(doc); //requires System.Printing namespace xpsd.Close(); PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == true) { printDialog.PrintQueue.AddJob("MyInkCanvas print job", filename, true); } page.Children.Remove(toPrint); myGrid.Children.Add(toPrint); }
private async void MainWindow_ContentRendered(object sender, EventArgs e) { await Task.Delay(1000); var pageSize = new Size(1280 * 8, 720 * 8); var document = new FixedDocument(); document.DocumentPaginator.PageSize = pageSize; var fixedPage = new FixedPage { Width = pageSize.Width, Height = pageSize.Height, }; for (int i = 0; i < 100; i++) { fixedPage.Children.Add(new Border { Width = pageSize.Width, Height = pageSize.Height, Background = new VisualBrush { Visual = DemoImage, } }); } fixedPage.Measure(pageSize); fixedPage.Arrange(new Rect(new Point(), pageSize)); fixedPage.UpdateLayout(); // Add page to document var pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(fixedPage); document.Pages.Add(pageContent); // Send to the printer. var pd = new PrintDialog(); pd.PrintDocument(document.DocumentPaginator, "正在打印……"); return; using var _localPrintServer = new LocalPrintServer(); var _currentPrintQueue = _localPrintServer.DefaultPrintQueue; _xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(_currentPrintQueue); //_xpsDocumentWriter.WritingProgressChanged += XpsDocumentWriter_WritingProgressChanged; //_xpsDocumentWriter.WritingPrintTicketRequired += XpsDocumentWriterOnWritingPrintTicketRequired; //_xpsDocumentWriter.WritingCancelled += XpsDocumentWriterOnWritingCancelled; //_xpsDocumentWriter.WritingCompleted += XpsDocumentWriterOnWritingCompleted; _vToXspd = (VisualsToXpsDocument)_xpsDocumentWriter.CreateVisualsCollator(); _vToXspd.BeginBatchWrite(); for (int i = 0; i < 100; i++) { _vToXspd?.WriteAsync(RootPanel); } _vToXspd?.EndBatchWrite(); }
public static PageContent CreatePrintPageContent(Size pageSize, List <PrintVisual> specialTestsCtrls, string heading) { var page = new FixedPage { Width = pageSize.Width, Height = pageSize.Height }; StackPanel stack = new StackPanel(); foreach (PrintVisual pv in specialTestsCtrls) { System.Windows.Controls.Image img = ConvertControlToImage(pv.Control, pv.Size); GroupBox panelGrpBox = new GroupBox(); panelGrpBox.Header = pv.Heading; panelGrpBox.Content = img; stack.Children.Add(panelGrpBox); } GroupBox grpBox = new GroupBox(); grpBox.Header = heading; grpBox.Content = stack; page.Children.Add(grpBox); page.Measure(pageSize); page.Arrange(new Rect(new Point(), pageSize)); page.UpdateLayout(); var pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(page); return(pageContent); }
/// <summary> /// Take a list of <see cref="UIElement"/> and return a <see cref="FixedDocument"/> where each page contains the given <see cref="UIElement"/>. /// </summary> /// <param name="uiElements"><see cref="UIElement"/> derived classes to place each in a <see cref="FixedPage"/>.</param> /// <param name="pageSize">Desired page size of each <see cref="FixedPage"/>. If <see cref="Paginator"/> was used to paginate, this should be the same value given to the Paginate method.</param> /// <returns></returns> public FixedDocument GetFixedDocumentFromPages(List <UIElement> uiElements, Size pageSize) { var document = new FixedDocument(); document.DocumentPaginator.PageSize = pageSize; foreach (var page in uiElements) { var fixedPage = new FixedPage { Width = document.DocumentPaginator.PageSize.Width, Height = document.DocumentPaginator.PageSize.Height }; FixedPage.SetLeft(page, 0); FixedPage.SetTop(page, 0); fixedPage.Children.Add(page); var pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(fixedPage); fixedPage.Measure(pageSize); fixedPage.Arrange(new Rect(new Point(), pageSize)); fixedPage.UpdateLayout(); document.Pages.Add(pageContent); } return(document); }
public static PageContent CreatePrintPageContent(Size pageSize, FrameworkElement tabCtrlCanvas, TabControl tabCtrl, int startIndex, int endIndex, string header) { var page = new FixedPage { Width = pageSize.Width, Height = pageSize.Height }; Size panelSize = new Size { Width = pageSize.Width, Height = pageSize.Height / (endIndex - startIndex + 1) }; StackPanel stack = new StackPanel(); for (int i = startIndex; i <= endIndex; i++) { tabCtrl.SelectedIndex = i; tabCtrl.SelectedItem = tabCtrl.Items[i]; tabCtrl.Measure(tabCtrl.RenderSize); tabCtrl.Arrange(new Rect(tabCtrl.RenderSize)); tabCtrl.UpdateLayout(); System.Windows.Controls.Image img = ConvertControlToImage(tabCtrlCanvas, panelSize); stack.Children.Add(img); } GroupBox grpBox = new GroupBox(); grpBox.Header = header; grpBox.Content = stack; page.Children.Add(grpBox); page.Measure(pageSize); page.Arrange(new Rect(new Point(), pageSize)); page.UpdateLayout(); var pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(page); return(pageContent); }
public static PageContent CreatePrintPageContentOxyPlot(Size pageSize, BithermalCaloricTabVM caloricVM, int width, int height, string header) { var page = new FixedPage { Width = pageSize.Width, Height = pageSize.Height }; BitmapSource plotBmp30 = ImageUtility.ExportToBitmap(caloricVM.Calorigram30, width, height, OxyColors.Transparent, 96); StackPanel caloricPanel = new StackPanel(); System.Windows.Controls.Image img30 = new System.Windows.Controls.Image(); img30.Source = plotBmp30; caloricPanel.Children.Add(img30); BitmapSource plotBmp44 = ImageUtility.ExportToBitmap(caloricVM.Calorigram44, width, height, OxyColors.Transparent, 96); System.Windows.Controls.Image img44 = new System.Windows.Controls.Image(); img44.Source = plotBmp44; caloricPanel.Children.Add(img44); GroupBox grpBox = new GroupBox(); grpBox.Header = header; grpBox.Content = caloricPanel; page.Children.Add(grpBox); page.Measure(pageSize); page.Arrange(new Rect(new Point(), pageSize)); page.UpdateLayout(); var firstPageContent = new PageContent(); ((IAddChild)firstPageContent).AddChild(page); return(firstPageContent); }
}// end:CreateFourthPageContent() //<SnippetXpsSaveCreateFixedPage5> // --------------------- CreateFifthPageContent ----------------------- /// <summary> /// Creates the content for the fifth fixed page.</summary> /// <returns> /// The page content for the fifth fixed page.</returns> private PageContent CreateFifthPageContent() { PageContent pageContent = new PageContent(); FixedPage fixedPage = new FixedPage(); UIElement visual = CreateThirdVisual(false); FixedPage.SetLeft(visual, 0); FixedPage.SetTop(visual, 0); double pageWidth = 96 * 8.5; double pageHeight = 96 * 11; fixedPage.Width = pageWidth; fixedPage.Height = pageHeight; fixedPage.Children.Add((UIElement)visual); Size sz = new Size(8.5 * 96, 11 * 96); fixedPage.Measure(sz); fixedPage.Arrange(new Rect(new Point(), sz)); fixedPage.UpdateLayout(); ((IAddChild)pageContent).AddChild(fixedPage); return(pageContent); }// end:CreateFifthPageContent()
/// <summary> /// PageからFixedPageを作成し、PageContentに追加 /// </summary> /// <param name="sender">Page</param> /// <param name="size">縦横サイズ</param> /// <returns>PageContent</returns> private PageContent CreatePageContent(object sender, Size size) { var pageContent = new PageContent(); var fixedPage = new FixedPage(); if (sender is Page page) { var frame = new Frame { Content = page }; FixedPage.SetLeft(frame, 0d); FixedPage.SetTop(frame, 0d); fixedPage.Children.Add(frame); fixedPage.Width = size.Width; fixedPage.Height = size.Height; fixedPage.Measure(size); fixedPage.Arrange(new Rect(new Point(), size)); fixedPage.UpdateLayout(); pageContent.Child = fixedPage; } return(pageContent); }
//<SnippetXpsSaveCreateFixedPage1> // ----------------------- CreateFirstFixedPage ----------------------- /// <summary> /// Creates the FixedPage for the first page.</summary> /// <returns> /// The FixedPage for the first page.</returns> private FixedPage CreateFirstFixedPage() { FixedPage fixedPage = new FixedPage(); fixedPage.Background = Brushes.LightYellow; UIElement visual = CreateFirstVisual(false); FixedPage.SetLeft(visual, 0); FixedPage.SetTop(visual, 0); double pageWidth = 96 * 8.5; double pageHeight = 96 * 11; fixedPage.Width = pageWidth; fixedPage.Height = pageHeight; fixedPage.Children.Add((UIElement)visual); Size sz = new Size(8.5 * 96, 11 * 96); fixedPage.Measure(sz); fixedPage.Arrange(new Rect(new Point(), sz)); fixedPage.UpdateLayout(); return(fixedPage); }// end:CreateFirstFixedPage()
private void AddThumbnail(object sender, RoutedEventArgs e) { FixedDocumentSequence docSeq = _xpsDocument.GetFixedDocumentSequence(); DocumentPaginator paginator = docSeq.DocumentPaginator; DocumentPage page = paginator.GetPage(0); // Create a rendertarget for the glyphs element. Round up width and height RenderTargetBitmap rt = new RenderTargetBitmap((int)page.Size.Width, (int)page.Size.Height, 96, 96, PixelFormats.Default); FixedPage fixedPage = (FixedPage)page.Visual; fixedPage.Measure(new Size(page.Size.Width, page.Size.Height)); fixedPage.Arrange(new Rect(new Size(page.Size.Width, page.Size.Height))); rt.Render(fixedPage); JpegBitmapEncoder jpegEncoder = new JpegBitmapEncoder(); jpegEncoder.Frames.Add(BitmapFrame.Create((BitmapSource)rt)); Stream stream = new MemoryStream(); jpegEncoder.Save(stream); XpsThumbnail thumbnail = _xpsDocument.AddThumbnail(XpsImageType.JpegImageType); Stream dstStream = thumbnail.GetStream(); byte[] thumbnailData = new byte[stream.Length]; // let's read the whole block into our buffer int totalBytesRead = 0; int requestedCount = (int)stream.Length; stream.Position = 0; while (totalBytesRead < requestedCount) { int bytesRead = stream.Read(thumbnailData, totalBytesRead, requestedCount - totalBytesRead); if (bytesRead == 0) { break; } totalBytesRead += bytesRead; } dstStream.Write(thumbnailData, 0, (int)stream.Length); dstStream.Close(); thumbnail.Commit(); _xpsDocument.Close(); stream.Position = 0; ImageBrush imageBrush = new ImageBrush(BitmapFrame.Create(stream)); imageBrush.Stretch = Stretch.Uniform; thumbnailDisplay.Fill = imageBrush; // Once a thumbnail is set disable the "Add Thumbnail" button. addThumbnailButton.IsEnabled = false; }
//print! /// <summary> /// prints this casefile to paper. /// </summary> public void Print() { //call up a print dialog to figure out what print device //we're using (paper printer, document printer, etc) //and the paper dimentions PrintDialog pd = new PrintDialog(); if (pd.ShowDialog() != true) { return; } //size of the page we're printing too. Size pageSize = new Size(pd.PrintableAreaWidth, pd.PrintableAreaWidth); //use a FixedDocument to make sure we have proper control over the format FixedDocument doc = new FixedDocument(); doc.DocumentPaginator.PageSize = pageSize; //add each report to the document foreach (Report rep in this.elementList) { //...which means adding each report's forms to the print document foreach (Form form in rep.GetForms()) { //each formn needs to go on a page... FixedPage page = new FixedPage(); page.Width = pageSize.Width; page.Height = pageSize.Height; //fetch the form's stackpanel (it's UI element) and format it so it //doesn't run off the page or anything StackPanel stackPanel = (StackPanel)form.UIelement.CloneElement(); stackPanel.Width = pageSize.Width; //we have to do this dumb stuff to make the form display right //It's effectivly magic, don't touch this or you'll break things page.Children.Add(stackPanel); page.Measure(pageSize); page.Arrange(new Rect(new Point(), pageSize)); page.UpdateLayout(); //now that the page is setup, we can add it to the document PageContent content = new PageContent(); ((IAddChild)content).AddChild(page); doc.Pages.Add(content); } } //now that the document's all assembled, we can print it! pd.PrintDocument(doc.DocumentPaginator, "myDocument"); }
public void printInPrinter() { ShowingReport[0].wareHouseName = wareHouseName.wareHouseName; StaticPageForAllData.printStockBalance = ShowingReport; StaticPageForAllData.printNumber = 3; if (ShowingReport.Count % 30 > 0) { StaticPageForAllData.pageNumber = (ShowingReport.Count / 30) + 1; } else { StaticPageForAllData.pageNumber = (ShowingReport.Count / 30); } StaticPageForAllData.PrintedpageNumber = 1; PrintDialog myPrintDialog = new PrintDialog(); var pageSize = new Size(8.26 * 96, 11.69 * 96); var document = new FixedDocument(); document.DocumentPaginator.PageSize = pageSize; for (int i = 0; i < StaticPageForAllData.pageNumber; i++) { PrintCurrentStockBlance print = new PrintCurrentStockBlance(); // Create Fixed Page. var fixedPage = new FixedPage(); fixedPage.Width = pageSize.Width; fixedPage.Height = pageSize.Height; // Add visual, measure/arrange page. fixedPage.Children.Add((UIElement)print); fixedPage.Measure(pageSize); fixedPage.Arrange(new Rect(new Point(), pageSize)); fixedPage.UpdateLayout(); // Add page to document var pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(fixedPage); document.Pages.Add(pageContent); } // Send to the printer. var pd = new PrintDialog(); if (myPrintDialog.ShowDialog() == true) { pd.PrintDocument(document.DocumentPaginator, "My Document"); } //up = new UpdateViewCommand(viewModel); }
/// <summary> /// Renders the current visual as a FixedPage and save it as XPS file. /// </summary> public void SaveXps() { var zipFile = Package.Open(Path.Combine(OutputDirectory, Name + ".xps"), FileMode.Create); XpsDocument xpsDocument = new XpsDocument(zipFile); PageContent pageContent = new PageContent(); FixedPage fixedPage = new FixedPage(); fixedPage.Width = WidthInPU; fixedPage.Height = HeightInPU; fixedPage.Background = Brushes.Transparent; // Visuals needs a UIElement as drawing container VisualPresenter presenter = new VisualPresenter(); presenter.AddVisual(this.visual); FixedPage.SetLeft(presenter, 0); FixedPage.SetTop(presenter, 0); fixedPage.Children.Add(presenter); // Perform layout Size size = new Size(WidthInPU, HeightInPU); fixedPage.Measure(size); fixedPage.Arrange(new Rect(new Point(), size)); fixedPage.UpdateLayout(); ((IAddChild)pageContent).AddChild(fixedPage); FixedDocument fixedDocument = new FixedDocument(); fixedDocument.DocumentPaginator.PageSize = size; fixedDocument.Pages.Add(pageContent); // Save as XPS file XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument); xpsWriter.Write(fixedDocument); xpsDocument.Close(); zipFile.Close(); // Must call at least two times GC.Collect this to get access to the xps file even I write synchronously. This is a bug in WPF. // Vista 64 .NET 3.5 SP1 installed xpsDocument = null; xpsWriter = null; GC.Collect(10, GCCollectionMode.Forced); GC.Collect(10, GCCollectionMode.Forced); //GC.Collect(10, GCCollectionMode.Forced); //GC.Collect(10, GCCollectionMode.Forced); }
private PageContent CreateNewPage(UIElement uie, Size printAreaSize) { var fixedPage = new FixedPage { Width = printAreaSize.Width, Height = printAreaSize.Height }; fixedPage.Children.Add(uie); fixedPage.Measure(printAreaSize); fixedPage.Arrange(new Rect(new Point(), printAreaSize)); fixedPage.UpdateLayout(); var pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(fixedPage); return(pageContent); }
// ---------------------------- SetUpPage ----------------------------- /// <summary> /// Sets the size of canvas and adds the canvas to the FixedPage. /// Performs measuring and arranging to update the FixedPage. /// </summary> /// <param name="fixedPage"> /// The FixedPage to set up </param> /// <param name="canvas1"> /// The canvas to use.</param> static private void SetUpPage(FixedPage fixedPage, Canvas canvas1) { FixedPage.SetLeft(canvas1, 0); FixedPage.SetTop(canvas1, 0); double pageWidth = 96 * 8.5; double pageHeight = 96 * 11; fixedPage.Width = pageWidth; fixedPage.Height = pageHeight; fixedPage.Children.Add(canvas1); Size sz = new Size(8.5 * 96, 11 * 96); fixedPage.Measure(sz); fixedPage.Arrange(new Rect(new Point(), sz)); fixedPage.UpdateLayout(); }
public static FixedDocument CreateSinglePageDocument(ContentControl uielement) { int dpi = 96; FixedDocument fixedDocument = new FixedDocument(); fixedDocument.DocumentPaginator.PageSize = new Size(dpi * 11, dpi * 8.5); try { PageContent page = new PageContent(); FixedPage inner_page = new FixedPage(); inner_page.Width = dpi * 11; inner_page.Height = dpi * 8.5; RenderTargetBitmap rtb = new RenderTargetBitmap((int)uielement.ActualWidth, (int)uielement.ActualHeight, dpi, dpi, PixelFormats.Default); rtb.Render(uielement); Image image = new Image { Source = rtb, Height = uielement.ActualHeight, Width = uielement.ActualWidth, Margin = new Thickness(25) // Margin for the control }; inner_page.Children.Add((UIElement)image); //measure size of the layout Size sz = new Size(dpi * 11, dpi * 8.5); inner_page.Measure(sz); inner_page.Arrange(new Rect(new Point(), sz)); inner_page.UpdateLayout(); ((IAddChild)page).AddChild(inner_page); fixedDocument.Pages.Add(page); } catch (Exception ex) { MessageBox.Show("Issue occurred while Exporting PDF: " + ex.Message); } finally { } return(fixedDocument); }
public FixedPage PrintAnotherPage(ObservableCollection <ObservableCollection <PrintForSteackLadger> > allit, int lastIt, int lastInfo) { pageNo++; // Create Fixed Page. FixedPage fp = new FixedPage(); fp.Width = pageSize.Width; fp.Height = pageSize.Height; // Add visual, measure/arrange page. PrintPeriodicStockLedgerPages print = new PrintPeriodicStockLedgerPages(allit, lastIt, lastInfo, pageNo); fp.Children.Add((UIElement)print); fp.Measure(pageSize); fp.Arrange(new Rect(new Point(), pageSize)); fp.UpdateLayout(); return(fp); }
}// end:CreateThirdPageContent() // --------------------- CreateFourthPageContent ---------------------- /// <summary> /// Creates the content for the fourth fixed page.</summary> /// <returns> /// The page content for the fourth fixed page.</returns> private PageContent CreateFourthPageContent() { PageContent pageContent = new PageContent(); FixedPage fixedPage = new FixedPage(); fixedPage.Background = Brushes.BlanchedAlmond; BitmapImage bitmapImage = new BitmapImage( new Uri(_contentDir + @"\tiger.jpg", UriKind.RelativeOrAbsolute)); Image image = new Image(); image.Source = bitmapImage; Canvas.SetTop(image, 0); Canvas.SetLeft(image, 0); fixedPage.Children.Add(image); Image image2 = new Image(); image2.Source = bitmapImage; image2.Opacity = 0.3; FixedPage.SetTop(image2, 150); FixedPage.SetLeft(image2, 150); fixedPage.Children.Add(image2); ((IAddChild)pageContent).AddChild(fixedPage); double pageWidth = 96 * 8.5; double pageHeight = 96 * 11; fixedPage.Width = pageWidth; fixedPage.Height = pageHeight; Size sz = new Size(8.5 * 96, 11 * 96); fixedPage.Measure(sz); fixedPage.Arrange(new Rect(new Point(), sz)); fixedPage.UpdateLayout(); return(pageContent); }// end:CreateFourthPageContent()
public static PageContent CreatePrintPageContent(Size pageSize, FrameworkElement ctrl, string header) { var page = new FixedPage { Width = pageSize.Width, Height = pageSize.Height }; System.Windows.Controls.Image img = ConvertControlToImage(ctrl, pageSize); GroupBox grpBox = new GroupBox(); grpBox.Header = header; grpBox.Content = img; page.Children.Add(grpBox); page.Measure(pageSize); page.Arrange(new Rect(new Point(), pageSize)); page.UpdateLayout(); var firstPageContent = new PageContent(); ((IAddChild)firstPageContent).AddChild(page); return(firstPageContent); }
public void PrintThisPage() { // Create Fixed Page. fixedPage.Width = pageSize.Width; fixedPage.Height = pageSize.Height; // Add visual, measure/arrange page. PrintStockLadgerDetail print = this; fixedPage.Children.Add(print); fixedPage.Measure(pageSize); fixedPage.Arrange(new Rect(new Point(), pageSize)); fixedPage.UpdateLayout(); // Add page to document var pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(fixedPage); document.Pages.Add(pageContent); }
public static PageContent CreatePrintPageContentOxyPlot(Size pageSize, PlotModel plot, int width, int height, string header) { var page = new FixedPage { Width = pageSize.Width, Height = pageSize.Height }; BitmapSource plotBitmapSrc = ImageUtility.ExportToBitmap(plot, width, height, OxyColors.Transparent, 96); System.Windows.Controls.Image img = new System.Windows.Controls.Image(); img.Source = plotBitmapSrc; GroupBox grpBox = new GroupBox(); grpBox.Header = header; grpBox.Content = img; page.Children.Add(grpBox); page.Measure(pageSize); page.Arrange(new Rect(new Point(), pageSize)); page.UpdateLayout(); var firstPageContent = new PageContent(); ((IAddChild)firstPageContent).AddChild(page); return(firstPageContent); }
private void CalculateSize(FixedPage fixedPage) { PrintQueue printQueue = LocalPrintServer.GetDefaultPrintQueue(); PrintCapabilities capabilities = printQueue.GetPrintCapabilities(); //get scale of the print wrt to screen of WPF visual double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / fixedPage.ActualWidth, capabilities.PageImageableArea.ExtentHeight / fixedPage.ActualHeight); //Transform the Visual to scale fixedPage.LayoutTransform = new ScaleTransform(scale, scale); //get the size of the printer page var sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); //update the layout of the visual to the printer page size. fixedPage.Measure(sz); double x = capabilities.PageImageableArea.OriginWidth; double y = capabilities.PageImageableArea.OriginHeight; fixedPage.Arrange(new Rect(new Point(x, y), sz)); fixedPage.UpdateLayout(); }
public static void PrintModelos(Modelos modelo, PagEncomendasEscolares[] pags) { PrintDialog printDialog = new PrintDialog(); System.Printing.PageMediaSize a4 = new System.Printing.PageMediaSize(System.Printing.PageMediaSizeName.ISOA4); printDialog.PrintTicket.PageMediaSize = a4; if (printDialog.ShowDialog() == true) { var document = new FixedDocument(); document.DocumentPaginator.PageSize = new Size( (double)new LengthConverter().ConvertFromString("21cm"), (double)new LengthConverter().ConvertFromString("29,7cm") ); for (int i = 0; i < pags.Length; i++) { for (int copies = 0; copies < pags[i].Copies; copies++) { // Add visual, measure/arrange page. UserControl control = GetModelo(modelo, pags[i].Escola.Nome, pags[i].SelectAno); Size pageSize = new Size(control.Width, control.Height); FixedPage fixedPage = new FixedPage(); fixedPage.Width = pageSize.Width; fixedPage.Height = pageSize.Height; fixedPage.Children.Add((UIElement)control); fixedPage.Measure(pageSize); fixedPage.Arrange(new Rect(new Point(), pageSize)); fixedPage.UpdateLayout(); // Add page to document var pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(fixedPage); document.Pages.Add(pageContent); } } printDialog.PrintDocument(document.DocumentPaginator, "Mod.2021 - Encomendas Escolares"); } }
/// <summary> /// Print the controls list /// </summary> /// <param name="controls">Visuals to Print</param> /// <param name="paperSize">The Size of the Paper</param> /// <param name="printJobName">Job Description</param> /// <remarks>Waring!: Only print paper size of "A4"</remarks> /// public static void Print(UserControl[] controls, PaperSize paperSize = PaperSize.A4, string printJobName = "A Imprimir Trabalhos Meio Mundo") { PrintDialog printDialog = new PrintDialog(); Paper paper = new Paper(paperSize); System.Printing.PageMediaSize a4 = new System.Printing.PageMediaSize(System.Printing.PageMediaSizeName.ISOA4); printDialog.PrintTicket.PageMediaSize = a4; if (printDialog.ShowDialog() == true) { var document = new FixedDocument(); document.DocumentPaginator.PageSize = new Size( (double)new LengthConverter().ConvertFromString("21cm"), (double)new LengthConverter().ConvertFromString("29,7cm") ); for (int pages = 0; pages < controls.Length; pages++) { Size pageSize = new Size(controls[pages].Width, controls[pages].Height); FixedPage fixedPage = new FixedPage(); fixedPage.Width = pageSize.Width; fixedPage.Height = pageSize.Height; fixedPage.Children.Add((UIElement)controls[pages]); fixedPage.Measure(pageSize); fixedPage.Arrange(new Rect(new Point(), pageSize)); fixedPage.UpdateLayout(); // Add page to document var pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(fixedPage); document.Pages.Add(pageContent); } printDialog.PrintDocument(document.DocumentPaginator, printJobName); } }
private void PrintDocument() { FixedDocument fixedDocument = new FixedDocument(); fixedDocument.DocumentPaginator.PageSize = new Size(96 * 8.5, 96 * 11); foreach (BitmapSource bitmapSource in this.m_BitmapSourceList) { PageContent pageContent = new PageContent(); FixedPage fixedPage = new FixedPage(); fixedPage.Background = Brushes.White; fixedPage.Width = 96 * 8.5; fixedPage.Height = 96 * 11; Image image = new Image(); image.Width = 96 * 8; image.Height = 96 * 10.5; image.Source = bitmapSource; FixedPage.SetLeft(image, 96 * .25); FixedPage.SetTop(image, 96 * .25); fixedPage.Children.Add((UIElement)image); ((IAddChild)pageContent).AddChild(fixedPage); fixedDocument.Pages.Add(pageContent); Size pageSize = new Size(96 * 8.5, 96 * 11); fixedPage.Measure(pageSize); fixedPage.Arrange(new Rect(new Point(), pageSize)); fixedPage.UpdateLayout(); } PrintDialog printDialog = new PrintDialog(); printDialog.ShowDialog(); printDialog.PrintDocument(fixedDocument.DocumentPaginator, "Print TIF Document"); }
ToFixedDocument(this IPaginatable paginatable, Size pageSize) { var document = new FixedDocument(); foreach (var content in paginatable.Paginate(pageSize)) { var presenter = new ContentPresenter() { Content = content, Width = pageSize.Width, Height = pageSize.Height, }; var page = new FixedPage() { Width = pageSize.Width, Height = pageSize.Height, }; page.Children.Add(presenter); // この3つを行わないと DataGrid がページ全体に展開せず、潰れた状態になる。 // これらが実際に何をするかは余裕があったら調べたい。 page.Measure(pageSize); page.Arrange(new Rect(new Point(0, 0), pageSize)); page.UpdateLayout(); var pageContent = new PageContent() { Child = page }; document.Pages.Add(pageContent); } return(document); }
//----------------------------------------------------------------------------- /// <summary> /// Regenerate pages /// </summary> //----------------------------------------------------------------------------- private void RecalculateLayout() { double pageMargin = .5 * _model.DPI; double pageTopAreaHeight = 4 * _model.DPI; int boxesPerRow = 7; int rowsPerPage = 5; if (rowsPerPage * boxesPerRow == 0) { return; } double dateBoxWidth = (_model.DocumentPixelWidth - pageMargin * 2) / boxesPerRow; double dateBoxHeight = (_model.DocumentPixelHeight - pageTopAreaHeight - pageMargin * 2) / rowsPerPage; _printablePages.Clear(); var year = _model.StartDate.Year; var month = _model.StartDate.Month; var endYear = _model.EndDate.Year; var endMonth = _model.EndDate.Month; while (true) { var titleWidth = _model.DocumentWidthInches * _model.DPI - pageMargin * 2; var pageCanvas = new Canvas() { Background = Brushes.White, ClipToBounds = true, }; int y = 0; var date = new DateTime(year, month, 1); var x = (int)date.DayOfWeek; int day = 1; var daysInMonth = DateTime.DaysInMonth(year, month); var rowCount = (daysInMonth - (7 - x)) / 7.0; if (rowCount > 4) { y = -1; titleWidth -= (dateBoxWidth * (7 - x)); } var noteBackgroundBrush = new SolidColorBrush(Color.FromRgb(242, 242, 242)); // Draw a note taking box if there is room at the top if (x > 0 && y > -1) { var noteBox = CreateDateBox(x * dateBoxWidth, dateBoxHeight, null); noteBox.Background = noteBackgroundBrush; pageCanvas.Children.Add(noteBox); Canvas.SetTop(noteBox, pageMargin + pageTopAreaHeight); Canvas.SetLeft(noteBox, pageMargin); } var daylabels = new List <Label>(); for (; y < rowsPerPage; y++) { for (; x < boxesPerRow; x++) { var thisDate = date.AddDays(day - 1); var dateBox = CreateDateBox(dateBoxWidth, dateBoxHeight, thisDate); Canvas.SetTop(dateBox, y * dateBoxHeight + pageMargin + pageTopAreaHeight); Canvas.SetLeft(dateBox, x * dateBoxWidth + pageMargin); pageCanvas.Children.Add(dateBox); if (day >= daysInMonth) { int boxesRemaining = boxesPerRow - x - 1; // draw a note taking box if there is room at the bottom if (boxesRemaining > 0) { var noteBox = CreateDateBox((boxesPerRow - x - 1) * dateBoxWidth, dateBoxHeight, null); noteBox.Background = noteBackgroundBrush; pageCanvas.Children.Insert(0, noteBox); Canvas.SetTop(noteBox, pageMargin + y * dateBoxHeight + pageTopAreaHeight); Canvas.SetLeft(noteBox, pageMargin + (x + 1) * dateBoxWidth); } // Draw Next month mini box var monthBox = CreateMonthbox(dateBoxWidth, dateBoxHeight, date.AddMonths(1)); pageCanvas.Children.Add(monthBox); if (boxesRemaining > 0) { Canvas.SetTop(monthBox, pageMargin + y * dateBoxHeight + pageTopAreaHeight); Canvas.SetLeft(monthBox, pageMargin + 6 * dateBoxWidth); } else { Canvas.SetTop(monthBox, pageMargin + pageTopAreaHeight); Canvas.SetLeft(monthBox, pageMargin + 1 * dateBoxWidth); } // Draw Previous month mini box monthBox = CreateMonthbox(dateBoxWidth, dateBoxHeight, date.AddMonths(-1)); pageCanvas.Children.Add(monthBox); if (boxesRemaining > 1) { Canvas.SetTop(monthBox, pageMargin + y * dateBoxHeight + pageTopAreaHeight); Canvas.SetLeft(monthBox, pageMargin + 5 * dateBoxWidth); } else { Canvas.SetTop(monthBox, pageMargin + pageTopAreaHeight); Canvas.SetLeft(monthBox, pageMargin); } break; } // Draw day labels if (day <= 7) { var dayName = thisDate.ToString("dddd").ToLower(); var dayHeight = dateBoxHeight * .3; var dayLabel = new Label() { Foreground = Brushes.Black, //Background = Brushes.Cyan, Content = dayName, FontFamily = _model.TitleFontFamily, FontSize = (dayHeight / _model.DPI) * .8 * 72, //FontWeight = FontWeights.SemiBold, Width = dateBoxWidth, Height = dayHeight, VerticalContentAlignment = VerticalAlignment.Bottom, HorizontalContentAlignment = HorizontalAlignment.Center, }; var dayY = y; if (dayY > 0) { dayY = 0; } daylabels.Add(dayLabel); Canvas.SetTop(dayLabel, pageMargin + dateBoxHeight * dayY + pageTopAreaHeight - dayHeight * .85); Canvas.SetLeft(dayLabel, pageMargin + dateBoxWidth * x); } day++; } x = 0; } daylabels.ForEach(l => pageCanvas.Children.Add(l)); var titleLabel = new Label() { Foreground = Brushes.Black, //Background = Brushes.Red, Content = " " + date.ToString("MMMM").ToLower() + " ", FontFamily = _model.TitleFontFamily, FontSize = (dateBoxHeight / _model.DPI) * 1.5 * 72, // FontWeight = FontWeights.UltraBold, Width = titleWidth, Height = dateBoxHeight * 2, VerticalContentAlignment = VerticalAlignment.Center, HorizontalContentAlignment = HorizontalAlignment.Center, Margin = new Thickness(0, -3.5 * _model.DPI, 0, 0) }; titleLabel.RenderTransform = new ScaleTransform(1, 1.3); pageCanvas.Children.Add(titleLabel); Canvas.SetTop(titleLabel, pageMargin); Canvas.SetLeft(titleLabel, pageMargin); var yearLabel = new Label() { Foreground = Brushes.Black, //Background = Brushes.Red, Content = " " + date.ToString("yyyy") + " ", FontFamily = _model.NumberFontFamily, FontSize = ((dateBoxHeight) * .25) / _model.DPI * 72, FontWeight = FontWeights.UltraBold, Width = titleWidth, Height = dateBoxHeight * .25, Padding = new Thickness(0), VerticalContentAlignment = VerticalAlignment.Center, HorizontalContentAlignment = HorizontalAlignment.Center, }; pageCanvas.Children.Add(yearLabel); Canvas.SetTop(yearLabel, pageMargin / 2); Canvas.SetLeft(yearLabel, pageMargin); var container = new Grid() { Width = _model.DocumentPixelWidth, Height = _model.DocumentPixelHeight }; container.Children.Add(pageCanvas); _printablePages.Add(container); month++; if (month > 12) { year++; month = 1; } if (year > endYear) { break; } if (year == endYear && month > endMonth) { break; } } var pageSize = new Size(_model.DocumentPixelWidth, _model.DocumentPixelHeight); var document = new FixedDocument(); document.DocumentPaginator.PageSize = pageSize; foreach (var xamlPage in _printablePages) { // Create FixedPage var fixedPage = new FixedPage(); fixedPage.Width = pageSize.Width; fixedPage.Height = pageSize.Height; // Add visual, measure/arrange page. fixedPage.Children.Add((UIElement)xamlPage); fixedPage.Measure(pageSize); fixedPage.Arrange(new Rect(new Point(), pageSize)); fixedPage.UpdateLayout(); // Add page to document var pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(fixedPage); document.Pages.Add(pageContent); } PageContainer.Document = document; }
}// end:CreateSecondPageContent() // --------------------- CreateThirdPageContent ----------------------- /// <summary> /// Creates the content for the third fixed page.</summary> /// <returns> /// The page content for the third fixed page.</returns> public PageContent CreateThirdPageContent() { PageContent pageContent = new PageContent(); FixedPage fixedPage = new FixedPage(); fixedPage.Background = Brushes.White; Canvas canvas1 = new Canvas(); canvas1.Width = 8.5 * 96; canvas1.Height = 11 * 96; // Top-Left TextBlock label = new TextBlock(); label.Foreground = Brushes.Black; label.FontFamily = new System.Windows.Media.FontFamily("Arial"); label.FontSize = 14.0; label.Text = String1; Canvas.SetTop(label, 0); Canvas.SetLeft(label, 0); canvas1.Children.Add(label); label = new TextBlock(); label.Foreground = Brushes.Black; label.FontFamily = new System.Windows.Media.FontFamily("Arial"); label.FontSize = 14.0; label.Text = String2; Canvas.SetTop(label, 20); Canvas.SetLeft(label, 0); canvas1.Children.Add(label); label = new TextBlock(); label.Foreground = Brushes.Black; label.FontFamily = new System.Windows.Media.FontFamily("Arial"); label.FontSize = 14.0; label.Text = String3; Canvas.SetTop(label, 40); Canvas.SetLeft(label, 0); canvas1.Children.Add(label); label = new TextBlock(); label.Foreground = Brushes.Black; label.FontFamily = new System.Windows.Media.FontFamily("Arial"); label.FontSize = 14.0; label.Text = String4; Canvas.SetTop(label, 60); Canvas.SetLeft(label, 0); canvas1.Children.Add(label); label = new TextBlock(); label.Foreground = Brushes.Black; label.FontFamily = new System.Windows.Media.FontFamily("Arial"); label.FontSize = 14.0; label.Text = String5; Canvas.SetTop(label, 80); Canvas.SetLeft(label, 0); canvas1.Children.Add(label); fixedPage.Children.Add(canvas1); double pageWidth = 96 * 8.5; double pageHeight = 96 * 11; fixedPage.Width = pageWidth; fixedPage.Height = pageHeight; Size sz = new Size(8.5 * 96, 11 * 96); fixedPage.Measure(sz); fixedPage.Arrange(new Rect(new Point(), sz)); fixedPage.UpdateLayout(); ((IAddChild)pageContent).AddChild(fixedPage); return(pageContent); }// end:CreateThirdPageContent()
public void printInPrinter() { StaticPageForAllData.printRecieptVoucher = ShowingVoucher; ObservableCollection <ReceiptVoucher> showThisPage = new ObservableCollection <ReceiptVoucher>(); for (int i = 0; i < StaticPageForAllData.printRecieptVoucher.Count; i++) { StaticPageForAllData.printRecieptVoucher[i].SL = i + 1; for (int j = 0; j < StaticPageForAllData.StockData.Count; j++) { if (StaticPageForAllData.printRecieptVoucher[i].itemNumber == StaticPageForAllData.StockData[j].itemNO) { StaticPageForAllData.printRecieptVoucher[i].unit = StaticPageForAllData.StockData[j].unit; } } showThisPage.Add(StaticPageForAllData.printRecieptVoucher[i]); } StaticPageForAllData.printRecieptVoucher = showThisPage; StaticPageForAllData.printNumber = 1; if (ShowingVoucher.Count % 30 > 0) { StaticPageForAllData.pageNumber = (ShowingVoucher.Count / 30) + 1; } else { StaticPageForAllData.pageNumber = (ShowingVoucher.Count / 30); } StaticPageForAllData.PrintedpageNumber = 2; PrintDialog myPrintDialog = new PrintDialog(); var pageSize = new Size(8.26 * 96, 11.69 * 96); var document = new FixedDocument(); document.DocumentPaginator.PageSize = pageSize; for (int i = 0; i < StaticPageForAllData.pageNumber; i++) { // Create Fixed Page. var fixedPage = new FixedPage(); fixedPage.Width = pageSize.Width; fixedPage.Height = pageSize.Height; // Add visual, measure/arrange page. PrintIssueVoucherView print = new PrintIssueVoucherView(); fixedPage.Children.Add((UIElement)print); fixedPage.Measure(pageSize); fixedPage.Arrange(new Rect(new Point(), pageSize)); fixedPage.UpdateLayout(); // Add page to document var pageContent = new PageContent(); ((IAddChild)pageContent).AddChild(fixedPage); document.Pages.Add(pageContent); } // Send to the printer. var pd = new PrintDialog(); if (myPrintDialog.ShowDialog() == true) { pd.PrintDocument(document.DocumentPaginator, "My Document"); } //up = new UpdateViewCommand(viewModel); }
public static void ArrangePage(Size pageSize, FixedPage page) { page.Measure(pageSize); page.Arrange(new Rect(new System.Windows.Point(), pageSize)); page.UpdateLayout(); }