3

I am printing my canvas with Microsoft Print to PDF. Now i have a requirement that when Microsoft Print to PDF is selected from available printers list then a FileSaveDialog is shown itself i do not want to show it rather than i want to save that PDF file on predefined path. Below is my code which i am using to print my canvas:

 public void RenderCanvasImage(int maxRight, int maxBottom, Canvas surface, Transform transform)
        {
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap(maxRight, maxBottom, 96d, 96d, PixelFormats.Pbgra32);
            renderBitmap.Render(surface);
            PrintDialog dialog = new PrintDialog();
            if (dialog.ShowDialog() != true)
            {
                return;
            }
            dialog.PrintQueue = new System.Printing.PrintQueue(new System.Printing.PrintServer() {  }, "Microsoft Print to PDF");

            Grid grid = new Grid();
            Image myImage = new Image();
            myImage.Source = renderBitmap;
            myImage.Stretch = Stretch.Uniform;
            grid.Children.Add(myImage);
            Grid.SetColumn(myImage, 0);
            Grid.SetRow(myImage, 1);
            var pqueue = dialog.PrintQueue;
            grid.Measure(new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight));
            grid.Arrange(new Rect(new Point(0, 0), grid.DesiredSize));
            dialog.PrintVisual(grid, "My Canvas");
            surface.LayoutTransform = transform;

        }

Can anyone suggest any solution how can i save file on a predefined path.

Thanks in Advance

Pankaj Bhatia
  • 202
  • 1
  • 11
  • 1
    check this : https://stackoverflow.com/questions/31896952/how-to-programmatically-print-to-pdf-file-without-prompting-for-filename-in-c-sh – Gaurang Dave Feb 27 '18 at 05:37
  • Thanks @Gaurang Infact i am already working on this solution but what i am not able to find in this is that where it is giving the image/file/anything which pdf will be generated. This code is generating pdf on specified path but how to print with some content? – Pankaj Bhatia Feb 27 '18 at 09:17
  • @GaurangDave It's for Windows Forms not WPF. – SHIN JaeGuk Feb 09 '19 at 17:09

0 Answers0