RichTextBox.Print() customize to set File Path?

4 posts, 0 answers
  1. Emin Sadykhov
    Emin Sadykhov avatar
    8 posts
    Member since:
    May 2010

    Posted 25 Jun Link to this post

    Hello,

    Thank you for your good control and service.

     

    As I know RichTextBox still doesn`t support export to Pdf with Track Changes

    By this reason I try to use RichTextBox.Print() function to export content with Track Changes to pdf ("Microsoft Print to Pdf" or any analog)

    The problem is just I can`t set exact filename path or at least propose recommended file name to user, before print. Here is my code, which is like your RichTextBox/CustomizingPrint from GitHub:

    (Editor is my RichTextBox)

    private void Print_Click(object sender, RoutedEventArgs e)
    {
     
        RadDocument document = Editor.Document;
     
        PrintSettings settings = new PrintSettings()
        {
            DocumentName = "Output_20200625.pdf", // Nope, I need Path to save
            PrintMode = PrintMode.Native,
            PrintScaling = PrintScaling.None,
            UseDefaultPrinter = true               
        };
     
        if (document != null)
        {
            PrintDialog printDialog = new PrintDialog();
            PrintQueue printQueue = FindPrintQueueByName("pdf");
            if (printQueue == null)
            {
                printQueue = printDialog.PrintQueue;
            }
            int pagesCount = document.FirstLayoutBox.Children.Count;
            printQueue.DefaultPrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISODLEnvelope);
            printDialog.PrintQueue = printQueue;
            printDialog.MinPage = 1;
            printDialog.MaxPage = (uint)pagesCount;
            //DocumentPrintPresenter documentPrintPresenter = new DocumentPrintPresenter(settings); // RadDocument Print Document?
     
            Editor.Print(printDialog, settings);               
        }
    }
     
    private PrintQueue FindPrintQueueByName(string name)
    {
        PrintServer server = new PrintServer();
        foreach (PrintQueue queue in server.GetPrintQueues(new EnumeratedPrintQueueTypes[] { EnumeratedPrintQueueTypes.Connections, EnumeratedPrintQueueTypes.Local }))
        {
            if (queue.Name.ToLowerInvariant().Contains(name))
            {
                return queue;
            }
        }
        return null;
    }

     

    Is any idea how can I make it, or any alternate method? May be Xps, then Pdf? 

    Thank you!

  2. Dimitar
    Admin
    Dimitar avatar
    2908 posts

    Posted 29 Jun Link to this post

    Hello Emin,

    I was unable to find a way to achieve this with the WPF print dialog. Please note that this does not depend on our code in this case. It depends on how the WPF API prints. In our code, we are calling the PrintDialog.PrintDocument method. 

    It seems that it is possible to achieve it in the WinForms suite. Please note that the RichTextBox is similar there. Here is an example:

    private void radButton1_Click(object sender, EventArgs e)
    {
        RadPrintDocument pdoc = new RadPrintDocument();
        pdoc.PrinterSettings.PrinterName = "Microsoft Print to PDF";
        pdoc.PrinterSettings.PrintFileName = @"D:\test1.pdf";
        pdoc.PrinterSettings.PrintToFile = true;
        pdoc.AssociatedObject = radRichTextEditor1;
        pdoc.Print();
    }
    

    I hope this helps. Should you have any other questions do not hesitate to ask.

    Regards,
    Dimitar
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  3. Emin Sadykhov
    Emin Sadykhov avatar
    8 posts
    Member since:
    May 2010

    Posted 29 Jun Link to this post

    Hello and thank you for your reply.

    But I wanted WPF and without additional dialogs. I found the way to use third party apps and publish WPF solution. First of all CutePDF + Ghostscript must be installed as prerequisites on the client machine. The following code will create a Pdf file without any dialogs:

    private void Print_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            string filePath = "C:\test1.pdf";
            string fileName = "";
     
            RadDocument document = Editor.Document;
     
            PrintSettings settings = new PrintSettings()
            {
                DocumentName = fileName,
                PrintMode = PrintMode.Native,
                PrintScaling = PrintScaling.None,
                UseDefaultPrinter = true
            };
     
            if (document != null)
            {
                PrintDialog printDialog = new PrintDialog();
                PrintQueue printQueue = FindPrintQueueByName("cutepdf");
                if (printQueue == null)
                {
                    //ErrorLog.ShowException("CutePDF virtual printer isn`t installed. Please install it or contact with administrator.");
                    return;
                }
                int pagesCount = document.FirstLayoutBox.Children.Count;
                printQueue.DefaultPrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISODLEnvelope);
                printDialog.PrintQueue = printQueue;
                printDialog.MinPage = 1;
                printDialog.MaxPage = (uint)pagesCount;
     
                if (!string.IsNullOrEmpty(filePath))
                {
                    RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software", true);
                    key.CreateSubKey(@"CutePDF Writer");
                    key = key.OpenSubKey(@"CutePDF Writer", true);
                    key.SetValue("BypassSaveAs", "1");
                    key.Close();
     
                    key = Registry.CurrentUser.OpenSubKey(@"Software", true);
                    key.CreateSubKey(@"CutePDF Writer");
                    key = key.OpenSubKey(@"CutePDF Writer", true);
                    key.SetValue("OutputFile", filePath);
                    key.Close();
                }
     
                Editor.Print(printDialog, settings);
     
                // check that file created...
            }
        }
        catch (Exception ex)
        {
           //error processing
        }
         
    }
     
    private PrintQueue FindPrintQueueByName(string name)
    {
        PrintServer server = new PrintServer();
        foreach (PrintQueue queue in server.GetPrintQueues(new EnumeratedPrintQueueTypes[] { EnumeratedPrintQueueTypes.Connections, EnumeratedPrintQueueTypes.Local }))
        {
            if (queue.Name.ToLowerInvariant().Contains(name))
            {
                return queue;
            }
        }
        return null;
    }

     

  4. Dimitar
    Admin
    Dimitar avatar
    2908 posts

    Posted 29 Jun Link to this post

    Hello Emin,

    Thank you for sharing your solution I have updated your Telerik points. 

    Regards,
    Dimitar
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
Back to Top