How to print PDF Documents in C# and .NET using Adobe, Foxit

  1. Add SautinSoft.Document from Nuget.
  2. Create a new document.
  3. Add a content.
  4. Save the document as PDF.
  5. Print our PDF using the Adobe Reader.

First of all, let's create a simple document with inscription: " Hi Dear Friends" and Save a document as the PDF file:

             // Let's create a simple PDF document.
            DocumentCore dc = new DocumentCore();

            // Add new section.
            Section section = new Section(dc);
            dc.Sections.Add(section);

            // Let's set page size A4.
            section.PageSetup.PaperType = PaperType.A4;

            // Add a paragraph using ContentRange:
            dc.Content.End.Insert("\nHi Dear Friends.", new CharacterFormat() { Size = 25, FontColor = Color.Blue, Bold = true });
            SpecialCharacter lBr = new SpecialCharacter(dc, SpecialCharacterType.LineBreak);
            dc.Content.End.Insert(lBr.Content);
            dc.Content.End.Insert("I'm happy to see you!", new CharacterFormat() { Size = 20, FontColor = Color.DarkGreen, UnderlineStyle = UnderlineType.Single });

            // Save PDF to a file
            dc.Save(pdfPath, new PdfSaveOptions());

The next step: Create a new process: Acrobat Reader. You may change it on Foxit Reader.

                      string processFilename = Microsoft.Win32.Registry.LocalMachine
                     .OpenSubKey("Software")
                     .OpenSubKey("Microsoft")
                     .OpenSubKey("Windows")
                     .OpenSubKey("CurrentVersion")
                     .OpenSubKey("App Paths")
                     .OpenSubKey("AcroRd32.exe")
                     .GetValue(String.Empty).ToString();


And the last step is printing the PDF.

            // Let's transfer our PDF file to the process Adobe Reader
            ProcessStartInfo info = new ProcessStartInfo();
            info.Verb = "print";
            info.FileName = processFilename;
            info.Arguments = String.Format("/p /h {0}", pdfPath);
            info.CreateNoWindow = true;
            
            //(It won't be hidden anyway... thanks Adobe!)
            info.WindowStyle = ProcessWindowStyle.Hidden;
            info.UseShellExecute = false;

            Process p = Process.Start(info);
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

 

Well done!

 

Complete code

using SautinSoft.Document;
using System;
using System.IO;
using System.Text;
using System.Diagnostics;

namespace Example
{
    class Program
    {       
        static void Main(string[] args)
        {
            CreateAndPrintPdf();
        }

        /// <summary>
        /// Creates a new document and saves it into PDF format. Print PDF using your default printer.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/print-document-net-csharp-vb.php
        /// </remarks>
        public static void CreateAndPrintPdf()
        {
            // Set the path to create pdf document.
            string pdfPath = @"Result.pdf";

            // Let's create a simple PDF document.
            DocumentCore dc = new DocumentCore();

            // Add new section.
            Section section = new Section(dc);
            dc.Sections.Add(section);

            // Let's set page size A4.
            section.PageSetup.PaperType = PaperType.A4;

            // Add a paragraph using ContentRange:
            dc.Content.End.Insert("\nHi Dear Friends.", new CharacterFormat() { Size = 25, FontColor = Color.Blue, Bold = true });
            SpecialCharacter lBr = new SpecialCharacter(dc, SpecialCharacterType.LineBreak);
            dc.Content.End.Insert(lBr.Content);
            dc.Content.End.Insert("I'm happy to see you!", new CharacterFormat() { Size = 20, FontColor = Color.DarkGreen, UnderlineStyle = UnderlineType.Single });

            // Save PDF to a file
            dc.Save(pdfPath, new PdfSaveOptions());

            // Create a new process: Acrobat Reader. You may change in on Foxit Reader.
            string processFilename = Microsoft.Win32.Registry.LocalMachine
                     .OpenSubKey("Software")
                     .OpenSubKey("Microsoft")
                     .OpenSubKey("Windows")
                     .OpenSubKey("CurrentVersion")
                     .OpenSubKey("App Paths")
                     .OpenSubKey("Acrobat.exe")
                     .GetValue(String.Empty).ToString();

            // Let's transfer our PDF file to the process Adobe Reader
            ProcessStartInfo info = new ProcessStartInfo();
            info.Verb = "print";
            info.FileName = processFilename;
            info.Arguments = String.Format("/p /h {0}", pdfPath);
            info.CreateNoWindow = true;
            
            //(It won't be hidden anyway... thanks Adobe!)
            info.WindowStyle = ProcessWindowStyle.Hidden;
            info.UseShellExecute = false;

            Process p = Process.Start(info);
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            // Print our PDF
            int counter = 0;
            while (!p.HasExited)
            {
                System.Threading.Thread.Sleep(1000);
                counter += 1;
                if (counter == 5) break;
            }
            if (!p.HasExited)
            {
                p.CloseMainWindow();
                p.Kill();
            }

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(pdfPath) { UseShellExecute = true });
        }
    }
}

Download

Imports Microsoft.VisualBasic
Imports SautinSoft.Document
Imports System
Imports System.IO
Imports System.Text
Imports System.Diagnostics

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			CreateAndPrintPdf()
		End Sub

		''' <summary>
		''' Creates a new document and saves it into PDF format. Print PDF using your default printer.
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/print-document-net-csharp-vb.php
		''' </remarks>
		Public Shared Sub CreateAndPrintPdf()
			' Set the path to create pdf document.
			Dim pdfPath As String = "Result.pdf"

			' Let's create a simple PDF document.
			Dim dc As New DocumentCore()

			' Add new section.
			Dim section As New Section(dc)
			dc.Sections.Add(section)

			' Let's set page size A4.
			section.PageSetup.PaperType = PaperType.A4

			' Add a paragraph using ContentRange:
			dc.Content.End.Insert(vbLf & "Hi Dear Friends.", New CharacterFormat() With {
				.Size = 25,
				.FontColor = Color.Blue,
				.Bold = True
			})
			Dim lBr As New SpecialCharacter(dc, SpecialCharacterType.LineBreak)
			dc.Content.End.Insert(lBr.Content)
			dc.Content.End.Insert("I'm happy to see you!", New CharacterFormat() With {
				.Size = 20,
				.FontColor = Color.DarkGreen,
				.UnderlineStyle = UnderlineType.Single
			})

			' Save PDF to a file
			dc.Save(pdfPath, New PdfSaveOptions())

			' Create a new process: Acrobat Reader. You may change in on Foxit Reader.
			Dim processFilename As String = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("App Paths").OpenSubKey("Acrobat.exe").GetValue(String.Empty).ToString()

			' Let's transfer our PDF file to the process Adobe Reader
			Dim info As New ProcessStartInfo()
			info.Verb = "print"
			info.FileName = processFilename
			info.Arguments = String.Format("/p /h {0}", pdfPath)
			info.CreateNoWindow = True

			'(It won't be hidden anyway... thanks Adobe!)
			info.WindowStyle = ProcessWindowStyle.Hidden
			info.UseShellExecute = False

			Dim p As Process = Process.Start(info)
			p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

			' Print our PDF
			Dim counter As Integer = 0
			Do While Not p.HasExited
				System.Threading.Thread.Sleep(1000)
				counter += 1
				If counter = 5 Then
					Exit Do
				End If
			Loop
			If Not p.HasExited Then
				p.CloseMainWindow()
				p.Kill()
			End If

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(pdfPath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.