C#: Get The Generated Letter with GetPDF()

Instructions

  1. Create a new Console Application project and add a web reference.
  2. Copy the entire code below, delete anything written in your application and paste the code there.
  3. Replace the values of the Const lines with your details:
    • Replace MyUsername and MyPassword with your PostalMethods user details.
    • Replace "ID" with the LetterID for which you would like to get the PDF.
  4. Start your application. If the response is -3000, you will see the status report. If the response is any other number, check the Web Service Status Codes section.
using System;
using System.IO;
 
namespace ConsoleApplication1
{
 
    class GetPDF
    {
 
        public static void Main()
        {
            const string MyUsername = "MyUsername";
            const string MyPassword = "MyPassword";
            const int ID = 0;
 
            com.postalmethods.api.PostalWS objPM = new com.postalmethods.api.PostalWS();
            com.postalmethods.api.GetPDFResult objResult = default(com.postalmethods.api.GetPDFResult);
 
            //
            // Invoke the GetPDF method
            //
            objResult = objPM.GetPDF(MyUsername, MyPassword, ID);
 
            //
            // Check response status
            //
            if (objResult.ResultCode == -3000)
            {
                //
                // -3000 means that the PDF was successfully retrieved. Enter the full path to save it in e.g. "c:\mypdf.pdf".
                //
                Console.WriteLine("Successfully retrieved PDF. Please enter a path to save it in.");
                string strPath = Console.ReadLine();
 
                //
                //Save the file
                //
                FileStream objFS = new FileStream(strPath, FileMode.Create, FileAccess.Write);
                objFS.Write(objResult.FileData, 0, objResult.FileData.Length);
                objFS.Close();
                objFS = null;
            }
            else
            {
                //
                // A value less than -3000 means an error occurred.
                // See the PostalMethods Status Codes: http://www.postalmethods.com/statuscodes.
                //
                Console.WriteLine("Error is:" + objResult.ResultCode);
            }
 
            // In debug mode, the following lines prevent your console application from closing automatically upon termination
            Console.WriteLine("Hit ENTER to terminate application");
            Console.ReadLine();
 
        }
    }
}